【Troubleshooting】外键不建索引易导致死锁

1.外键不建索引,更新外键字段会锁住父表,易导致死锁

父表:P_TABLE

子表:F_TABLE

外键字段:object_id

 

未建索引:

session1:

SQL>update f_table set object_id='10000' where object_id=116053;

 

1 rowupdated.

 

session2:

SQL>update f_table set object_id='10001' where object_id=116054;

 

1 rowupdated.

 

session1:

SQL>update p_table set object_id='10000' where object_id=116053;

hang住!

 

查看锁情况:

[EPDEV]/u01/oracle$orawholockwho

 

USERNAME          SID   SERIAL# 'ISBLOCKING' USERNAME          SID    SERIAL#

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

XQW               302      18695 is blocking  XQW               177      28312

 

[EPDEV]/u01/oracle$oralock

 

       SID TYPE         HOLD         REQUEST             ID1        ID2     CTIME BLOCK_OTHERS

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

       177 DML          Row-X (SX)   None             257638          0        113 Not Blocking

       177 DML          Row-X (SX)   S/Row-X(SSX)     257637          0        66 Not Blocking

       177 Transaction  Exclusive   None             262176     311407         66 Not Blocking

       302 DML          Row-X (SX)   None             257637          0         57 Blocking

       302 DML          Row-S (SS)   None            257638          0         57 Not Blocking

       302 Transaction  Exclusive   None             655375     322355         57 Not Blocking

session1需要S/Row-X(SSX)锁,而这个锁为session2所拥有,因此session2阻塞了session1,session1  hang住。

 

session2:

SQL>update p_table set object_id='10001' where object_id=116054;

hang住!

此时就产生了死锁,到session1发现报错:

session1:

SQL>update p_table set object_id='10000' where object_id=116053;

updatep_table set object_id='10000' where object_id=116053

       *

ERROR atline 1:

ORA-00060: deadlock detected while waiting for resource

 

查看锁情况:

[EPDEV]/u01/oracle$ora wholockwho

 

USERNAME          SID   SERIAL# 'ISBLOCKING' USERNAME          SID    SERIAL#

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

XQW               177      28312 is blocking  XQW               302      18695

 

[EPDEV]/u01/oracle$oralock

       SID TYPE         HOLD         REQUEST             ID1        ID2     CTIME BLOCK_OTHERS

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

       177 DML          Row-X (SX)   None             257638          0        144 Not Blocking

       177 DML          Row-X (SX)   None             257637          0         97 Blocking

       177 Transaction  Exclusive   None             262176     311407         97 Not Blocking

       302 DML          Row-X (SX)   S/Row-X(SSX)     257637          0         88 Not Blocking

       302 DML          Row-S (SS)   None             257638          0         88 Not Blocking

       302 Transaction  Exclusive   None             655375     322355         88 Not Blocking

 

 

建了索引:

SQL>create index idx_ftable on f_table(object_id);

 

Indexcreated.

 

session1:

SQL>update f_table set object_id='10000' where object_id=116053;

 

1 rowupdated.

 

session2:

SQL>update f_table set object_id='10001' where object_id=116054;

 

1 rowupdated.

 

session1:

SQL>update p_table set object_id='10000' where object_id=116053;

updatep_table set object_id='10000' where object_id=116053

*

ERROR atline 1:

ORA-00001:unique constraint (XQW.PK_PTABLE) violated

 

session2:

SQL>update p_table set object_id='10001' where object_id=116054;

updatep_table set object_id='10001' where object_id=116054

*

ERROR atline 1:

ORA-00001:unique constraint (XQW.PK_PTABLE) violated

 

没有hang住!

 

看下锁的情况:

[EPDEV]/u01/oracle$ora lock

 

       SID TYPE         HOLD         REQUEST             ID1        ID2     CTIME BLOCK_OTHERS

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

       216 DML          Row-S (SS)   None             257638          0         18 Not Blocking

       216 DML          Row-X (SX)   None             257637          0        136 Not Blocking

       216 Transaction  Exclusive   None             458779    310843         70 Not Blocking

       302 Transaction  Exclusive   None             131077     332185         45 Not Blocking

       302 DML          Row-S (SS)   None             257638          0         45 Not Blocking

       302 DML          Row-X (SX)   None             257637          0         45 Not Blocking

 

 

2.外键不建索引,删除父表会锁住子表,易导致死锁

未建索引:

session1:

--执行一个删除操作,这时候在子表和父表上都加了一个Row-S(SX)锁

SQL>delete from f_table where object_id=100;

 

1 rowdeleted.

 

SQL>delete from p_table where object_id=100;

 

1 rowdeleted.

 

[EPDEV]/u01/oracle$ora lock 216

 

Sessionaltered.

 

 

       SID TYPE         HOLD         REQUEST             ID1        ID2     CTIME BLOCK_OTHERS

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

       216 DML          Row-X (SX)   None             257638          0          3 Not Blocking

       216 DML          Row-X (SX)   None             257637          0         30 Not Blocking

       216 Transaction  Exclusive   None             196616     423931         30 Not Blocking

 

session2:

--执行另一个删除操作,发现这时候第二个删除语句等待

SQL>delete from f_table where object_id=200;

 

1 rowdeleted.

 

SQL>delete from p_table where object_id=200;

hang住!

 

session1:

--死锁马上发生

SQL> deletefrom p_table where object_id=100;

hang住!

 

查看锁的情况:

[EPDEV]/u01/oracle$orawholockwho

 

USERNAME          SID   SERIAL# 'ISBLOCKING' USERNAME          SID    SERIAL#

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

XQW               216      49878 is blocking  XQW               302      18695

 

[EPDEV]/u01/oracle$oralock

 

       SID TYPE         HOLD         REQUEST             ID1        ID2     CTIME BLOCK_OTHERS

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

       216 DML          Row-X (SX)   None             257637          0         21Blocking

       216 DML          Row-X (SX)   None             257638          0         21 Not Blocking

       216 Transaction  Exclusive   None             655371     322311         21 Not Blocking

       302 Transaction  Exclusive   None              65583     323836         14 Not Blocking

       302 DML          Row-X (SX)   S/Row-X(SSX)     257637          0         14 Not Blocking                                 

       302 DML          Row-S (SS)   None             257638          0         14 Not Blocking

 

session2:

SQL>delete from p_table where object_id=200;

deletefrom p_table where object_id=200

            *

ERROR atline 1:

ORA-00060: deadlock detected while waiting for resource

 

查看锁情况:

[EPDEV]/u01/oracle$orawholockwho

 

USERNAME          SID   SERIAL# 'ISBLOCKING' USERNAME          SID    SERIAL#

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

XQW               302      18695 is blocking  XQW               216      49878

 

[EPDEV]/u01/oracle$oralock

 

       SID TYPE         HOLD         REQUEST             ID1        ID2     CTIME BLOCK_OTHERS

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

       216 Transaction  None        Exclusive        458775     312402         48 Not Blocking

       216 Transaction  Exclusive   None             524309     396893         69 Not Blocking

       216 DML          Row-X (SX)   None             257638          0         66 Not Blocking

       216 DML          Row-X (SX)   None             257637          0         66 Not Blocking

       302 DML          Row-X (SX)   None             257637          0         60 Not Blocking

       302 DML          Row-S (SS)   None             257638          0         60 Not Blocking

       302 Transaction  Exclusive   None             458775     312402         60 Blocking

 

建了索引:

SQL>create index idx_ftable on f_table(object_id);

 

Indexcreated.

 

session1:

SQL>delete from f_table where object_id=100;

 

1 rowdeleted.

 

SQL>delete from p_table where object_id=100;

 

1 rowdeleted.

 

session2:

SQL>delete from f_table where object_id=200;

 

1 rowdeleted.

 

SQL>delete from p_table where object_id=200;

 

1 rowdeleted.

 

 

查看锁的情况:

[EPDEV]/u01/oracle$oralock

 

       SID TYPE         HOLD         REQUEST             ID1        ID2     CTIME BLOCK_OTHERS

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

       216 DML          Row-X (SX)   None             257638          0        135 Not Blocking

       216 DML          Row-X (SX)   None             257637          0        136 Not Blocking

       216 Transaction  Exclusive   None             589868     400648        136 Not Blocking

       302 DML          Row-X (SX)   None             257638          0        193 Not Blocking

       302 DML          Row-X (SX)   None             257637          0        196 Not Blocking

       302 Transaction  Exclusive   None             524321     396932        196 Not Blocking

 

回到session1:

SQL>delete from f_table where object_id=100;

 

0 rowsdeleted.

没有hang住,没有产生死锁

 

--当对子表的外键列添加索引后,死锁被消除,因为这时删除父表记录不需要对子表加表级锁。

 

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

转载须注明出处!

http://blog.csdn.net/sharqueen_wu/article/details/39183189

转载须注明出处!

http://blog.csdn.net/sharqueen_wu/article/details/39183189

转载须注明出处!

http://blog.csdn.net/sharqueen_wu/article/details/39183189




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值