oracle外键有什么用,深入理解Oracle索引(20):外键是否应该加索引

先表明我的立场、我是绝对支持外键一定要加索引!

虽然在高版本的Oracle里、对这个要求有所降低、但依然有如下原因:

① 死锁

外键未加索引是导致死锁的最主要原因、因为

无论更新父表主键、或者删除一个父表记录、都会在子表加一个表锁

这就会不必要的锁定更多的行、从而影响并发性

② ON DELETE CASCADE

对于删除的每一个父行、都会把子表全表扫描一次

如:

EMP是DEPT的子表

DELETE dept WHERE deptno=10 会级联至EMP

③ 从父表查询子表

如:

EMP是DEPT的子表

SELECT *

FROM dept,emp

WHERE emp.deptno=dept.deptno and

dept.dname= :X

另外、证明子表由于外键未加索引而被锁住、可经由下列方法:

ALTER TABLE DISABLE TABLE LOCK;

那么、对父表的可能导致表锁的任何 UPDATE 或 DELETE 都会收到如下错误:

ERROR at line 1:

ORA-00069: cannot acquire lock -- table locks disable for

以下做个简单的外键未加索引的测试:

建立表:

hr@ORCL> create table t_father (id number,name varchar2(25),primary key(id));

hr@ORCL> create table t_sun (fid number,name varchar2(25),foreign key(fid) references t_father(id));

hr@ORCL> select table_name,CONSTRAINT_NAME,STATUS,R_CONSTRAINT_NAME from user_constraints where owner=\'HR\' and table_name in (\'T_FATHER\',\'T_SUN\');

TABLE_NAME CONSTRAINT_NAME STATUS R_CONSTRAINT_NAME

------------------------------ ------------------------------ -------- ------------------------------

T_FATHER SYS_C005495 ENABLED

T_SUN SYS_C005497 ENABLED SYS_C005495

倒入数据并分析表:

hr@ORCL> insert into t_father select rownum,rownum||\'a\' from dual connect by rownum<1000;

hr@ORCL> insert into t_sun select rownum,rownum||\'b\' from dual connect by rownum<1000;

hr@ORCL> commit;

hr@ORCL> exec dbms_stats.gather_table_stats(ownname=>\'HR\',tabname=>\'T_FATHER\');

hr@ORCL> exec dbms_stats.gather_table_stats(ownname=>\'HR\',tabname=>\'T_SUN\');

用以下 TOM 给出的脚本检查外键无索引的表:

COLUMN COLUMNS format a30 word_wrapped

COLUMN tablename format a15 word_wrapped

COLUMN constraint_name format a15 word_wrapped

SELECT TABLE_NAME,

CONSTRAINT_NAME,

CNAME1 || NVL2(CNAME2, \',\' || CNAME2, NULL) ||

NVL2(CNAME3, \',\' || CNAME3, NULL) ||

NVL2(CNAME4, \',\' || CNAME4, NULL) ||

NVL2(CNAME5, \',\' || CNAME5, NULL) ||

NVL2(CNAME6, \',\' || CNAME6, NULL) ||

NVL2(CNAME7, \',\' || CNAME7, NULL) ||

NVL2(CNAME8, \',\' || CNAME8, NULL) COLUMNS

FROM (SELECT B.TABLE_NAME,

B.CONSTRAINT_NAME,

MAX(DECODE(POSITION, 1, COLUMN_NAME, NULL)) CNAME1,

MAX(DECODE(POSITION, 2, COLUMN_NAME, NULL)) CNAME2,

MAX(DECODE(POSITION, 3, COLUMN_NAME, NULL)) CNAME3,

MAX(DECODE(POSITION, 4, COLUMN_NAME, NULL)) CNAME4,

MAX(DECODE(POSITION, 5, COLUMN_NAME, NULL)) CNAME5,

MAX(DECODE(POSITION, 6, COLUMN_NAME, NULL)) CNAME6,

MAX(DECODE(POSITION, 7, COLUMN_NAME, NULL)) CNAME7,

MAX(DECODE(POSITION, 8, COLUMN_NAME, NULL)) CNAME8,

COUNT(*) COL_CNT

FROM (SELECT SUBSTR(TABLE_NAME, 1, 30) TABLE_NAME,

SUBSTR(CONSTRAINT_NAME, 1, 30) CONSTRAINT_NAME,

SUBSTR(COLUMN_NAME, 1, 30) COLUMN_NAME,

POSITION

FROM USER_CONS_COLUMNS) A,

USER_CONSTRAINTS B

WHERE A.CONSTRAINT_NAME = B.CONSTRAINT_NAME

AND B.CONSTRAINT_TYPE = \'R\'

GROUP BY B.TABLE_NAME, B.CONSTRAINT_NAME) CONS

WHERE COL_CNT > ALL

(SELECT COUNT(*)

FROM USER_IND_COLUMNS I

WHERE I.TABLE_NAME = CONS.TABLE_NAME

AND I.COLUMN_NAME IN (CNAME1, CNAME2, CNAME3, CNAME4, CNAME5,

CNAME6, CNAME7, CNAME8)

AND I.COLUMN_POSITION <= CONS.COL_CNT

GROUP BY I.INDEX_NAME)

/

hr@ORCL> /

TABLE_NAME CONSTRAINT_NAME COLUMNS

------------------------------ --------------- ------------------------------

T_SUN SYS_C005497 FID

以下进行测试:

1)Session_A:

hr@ORCL> select sid from v$session where sid in (select sid from v$mystat where rownum=1);

SID

----------

159

hr@ORCL> delete t_sun where fid=998;

1 row deleted.

2)Session_B:

hr@ORCL> select sid from v$session where sid in (select sid from v$mystat where rownum=1);

SID

----------

142

hr@ORCL> delete t_sun where fid=123;

1 row deleted.

3)Session_A:

hr@ORCL> delete t_father where id=555;

----请求子表的表锁却不可得之、被hang住了

用下面脚本查询数据库锁情况:

SELECT a.sid ||

decode(request,

0,

\' :holder\',\' :Waiter\') sess_id,blocking_session blocker,

lmode,

request,

a.type,

c.object_name,

decode(row_wait_obj#,

-1,

\'Holder of Lock !!!\',

dbms_rowid.rowid_create(1,

row_wait_obj#,

row_wait_file#,

row_wait_block#,

row_wait_row#)) row_id,

nvl(SQL_FULLTEXT, \'Holder of Lock !!!\') sqltext

FROM V$LOCK A, V$LOCKED_OBJECT B, ALL_OBJECTS C, V$SESSION D, V$SQL E

WHERE (id1, id2, a.type) in

(select id1, id2, type from v$lock where request > 0)

AND a.sid = b.session_id

AND b.object_id = c.object_id

AND d.sid = a.sid

AND d.sql_hash_value = e.hash_value(+)

sys@ORCL> /

SESS_ID BLOCKER LMODE REQUEST TY OBJECT_NAME ROW_ID SQLTEXT

------------------------------------------------ ---------- ---------- ---------- -- ------------------------------ ------------------ --------------------------------------------------------------------------------

159 :Waiter 142 3 5 TM T_SUN Holder of Lock !!! delete t_father where id=555

159 :Waiter 142 3 5 TM T_FATHER Holder of Lock !!! delete t_father where id=555

142 :holder 3 0 TM T_SUN Holder of Lock !!! Holder of Lock !!!

142 :holder 3 0 TM T_FATHER Holder of Lock !!! Holder of Lock !!!

By David Lin

2013-06-07

Good Luck

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441320.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 1027B 】Numbers on the Chessboard (没有营养的找规律题,无聊题)

题干&#xff1a; You are given a chessboard of size nnnn. It is filled with numbers from 11 to n2n2 in the following way: the first ⌈n22⌉⌈n22⌉ numbers from 11 to ⌈n22⌉⌈n22⌉ are written in the cells with even sum of coordinates from left to right f…

php mysql int 日期格式化 string,MYSQL int类型字段的时间存放和显示 和 php的时间存放函数...

mysql&#xff1a;int类型字段的时间存放UPDATE tablename SET add_time UNIX_TIMESTAMP(NOW())int类型字段的时间显示SELECT FROM_UNIXTIME(add_time) FROM tablenamephp时间戳函数&#xff1a;time() 获取当前时间戳 结果&#xff1a;1232553600strtotime() 转换为时间戳da…

【CodeForces - 1060C】Maximum Subrectangle (思维,预处理前缀和,dp,枚举长度)

题干&#xff1a; You are given two arrays aa and bb of positive integers, with length nn and mmrespectively. Let cc be an nmnm matrix, where ci,jai⋅bjci,jai⋅bj. You need to find a subrectangle of the matrix cc such that the sum of its elements is at m…

oracle按照指定顺序读取,oracle按照指定顺序进行排序

之前在网上查了下按照指定顺序进行排序的方法&#xff0c;根据charindex来处理排序&#xff0c;但是在oracle发现不行&#xff0c;因为oracle没有charindex函数&#xff0c;然后使用instr代替了charindex&#xff0c;然后又在网上搜了另外一种方实验如下&#xff1a;1.新建表CR…

【Codeforces 631C 】Report(单调栈,思维模拟)

题干&#xff1a; Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that d…

oracle开放视图,Oracle视图

Oracle视图在Oracle中&#xff0c;视图是实际上并不存在的虚拟表。它存储在Oracle数据字典中&#xff0c;不存储任何数据。可以在调用时执行。通过连接一个或多个表的查询创建视图。Oracle创建视图句法&#xff1a;参数&#xff1a;view_name&#xff1a;它指定要创建的Oracle …

【CodeForces - 214C 】Game (拓扑排序,思维)

题干&#xff1a; Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of n parts and to complete each part a player may probably need to complete some other ones. We know that th…

php$this-conn可以不先定义吗,CodeIgniter 是不是支持PDO 查询?还是本来就不支持

CodeIgniter 是否支持PDO 查询&#xff1f;还是本来就不支持&#xff1f;本帖最后由 default7 于 2014-11-15 19:34:55 编辑配置CodeIgniter 的database 连接方式为PDO 类型&#xff0c;但是怎么都查询不到数据&#xff0c;却可以查处有多少记录&#xff01;自带代码仔细跟踪代…

【ZOJ - 2836 】Number Puzzle (容斥原理)

题干&#xff1a; Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list. Input The input contains several test cases. …

轮番滑动PHP,touch事件之滑动判断(左右上下方向)

touch事件之滑动判断(左右上下方向)作者: 2020.03.11 本文发布于18天前 分类:$("body").on("touchstart", function(e) {// 判断默认行为是否可以被禁用if (e.cancelable) {// 判断默认行为是否已经被禁用if (!e.defaultPrevented) {e.preventDefault();}}…

【CodeForces - 215A】Bicycle Chain (水题)

题干&#xff1a; Vasyas bicycle chain drive consists of two parts: n stars are attached to the pedal axle, m stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the i-th sta…

ubuntu 在线安装php,ubuntu在线安装LNMP

一直以来个人安装lamp环境都是源码编译的&#xff0c;这个过程呢其实也要去经历的&#xff0c;但是毕竟占用时间久&#xff0c;有些时候在做一些测试环境的时候&#xff0c;可以在线安装比较快源码编译nginx可看往期&#xff1a;Nginx的安装对于lnmp的在线安装&#xff0c;如下…

【CodeForces - 215B 】Olympic Medal (数学,公式推导)

题干&#xff1a; The World Programming Olympics Medal is a metal disk, consisting of two parts: the first part is a ring with outer radius of r1 cm, inner radius of r2 cm, (0 < r2 < r1)made of metal with density p1 g/cm3. The second part is an i…

oracle的分支语句,oracle中的分支与循环语句

分支语句 if的三种写法一, if 2 < 1 thendbms_output.put_line(‘条件成立‘);end if;二, if 2 < 1 thendbms_output.put_line(‘条件成立‘);elsedbms_output.put_line(‘条件不成立‘);end if;三, if 2 < 1 thendbms_output.put_line(‘条件成立‘);elsif 4 > 3 …

【CodeForces - 215C 】Crosses (思维,图形题)

题干&#xff1a; There is a board with a grid consisting of n rows and m columns, the rows are numbered from 1 from top to bottom and the columns are numbered from 1 from left to right. In this grid we will denote the cell that lies on row number i and co…

Scaffold php,GitHub - yiiplus/scaffold: scaffold是一个基于Yii2高级项目模版工程化实现的应用程序...

Yii 2 Scaffold Project Kit易加-脚手架(scaffold)是一个基于Yii2高级项目模版工程化实现的应用程序&#xff0c;它将更加高效、规范和工程化的满足项目开发的需求。DIRECTORY STRUCTUREcommonconfig/ contains shared configurationsmail/ contains view files for e-mailsmod…

*【CodeForces - 214D 】Numbers (dp,组合数学)

题干&#xff1a; Furik loves writing all sorts of problems, especially such that he cant solve himself. Youve got one of his problems, the one Furik gave to Rubik. And Rubik asks you to solve it. There is integer n and array a, consisting of ten integers…

oracle修改某个数据类型,Oracle 修改某个字段的数据类型三种方式

1.将该列设置为null,再修改其类型(这样会丢失数据)2.最简单的方法&#xff1a;假设你的表名为 tab_targetcreate table test as select * from tab_target whre 12;alter table test modify (col_name number(5));insert into test select * from tab_target;drop table tab_t…

【EOJ Monthly 2018.10 - B】 莫干山奇遇 (思维构造,数学,数组,贪心)(总结)

题干&#xff1a; Time limit per test: 2.0 seconds Memory limit: 512 megabytes 出题人当然是希望出的题目有关 oxx&#xff0c;于是想方设法给题目配上一些有关 oxx 的背景故事&#xff0c;使得它看起来不那么无趣。但有的时候却无法引入合适的小姐姐&#xff0c;使得 o…

有奶瓶的linux系统,用U盘启动BEINI(奶瓶)系统

用U盘启动&#xff1a;奶瓶(beini)这个系统&#xff0c;是一款基于Tiny Core Linux 搭建的无线网络安全测试系统&#xff0c;当然由于它是用来安全测试的系统&#xff0c;因此在安全方面自然有着强大的功能。而且&#xff0c;这个系统非常简便易学&#xff0c;因此现在已经逐渐…