Oracle外鍵不加索引引起死鎖示例
更新時間:2014年05月15日 16:22:23 作者:
這篇文章主要介紹了Oracle外鍵不加索引引起死鎖的情況及解決,需要的朋友可以參考下
--創(chuàng)建一個表,此表作為子表
create table fk_t as select *from user_objects;
delete from fk_t where object_id is null;
commit;
--創(chuàng)建一個表,此表作為父表
create table pk_t as select *from user_objects;
delete from pk_t where object_id is null;
commit;
--創(chuàng)建父表的主鍵
alter table PK_t add constraintpk_pktable primary key (OBJECT_ID);
--創(chuàng)建子表的外鍵
alter table FK_t addconstraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);
--session1:執(zhí)行一個刪除操作,這時候在子表和父表上都加了一個Row-S(SX)鎖
delete from fk_t whereobject_id=100;
delete from pk_t where object_id=100;
--session2:執(zhí)行另一個刪除操作,發(fā)現(xiàn)這時候第二個刪除語句等待
delete from fk_t whereobject_id=200;
delete from pk_t whereobject_id=200;
--回到session1:死鎖馬上發(fā)生
delete from pk_t whereobject_id=100;
session2中報錯:
SQL> delete from pk_table where object_id=200;
delete from pk_table where object_id=200
*
第 1 行出現(xiàn)錯誤:
ORA-00060: 等待資源時檢測到死鎖
當(dāng)對子表的外鍵列添加索引后,死鎖被消除,因為這時刪除父表記錄不需要對子表加表級鎖。
--為外鍵建立索引
create index ind_pk_object_id on fk_t(object_id) nologging;
--重復(fù)上面的操作session1
delete from fk_t whereobject_id=100;
delete from pk_t whereobject_id=100;
--session2
delete from fk_t whereobject_id=200;
delete from pk_t whereobject_id=200;
--回到session1不會發(fā)生死鎖
delete from pk_t whereobject_id=100;
create table fk_t as select *from user_objects;
delete from fk_t where object_id is null;
commit;
--創(chuàng)建一個表,此表作為父表
create table pk_t as select *from user_objects;
delete from pk_t where object_id is null;
commit;
--創(chuàng)建父表的主鍵
alter table PK_t add constraintpk_pktable primary key (OBJECT_ID);
--創(chuàng)建子表的外鍵
alter table FK_t addconstraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);
--session1:執(zhí)行一個刪除操作,這時候在子表和父表上都加了一個Row-S(SX)鎖
delete from fk_t whereobject_id=100;
delete from pk_t where object_id=100;
--session2:執(zhí)行另一個刪除操作,發(fā)現(xiàn)這時候第二個刪除語句等待
delete from fk_t whereobject_id=200;
delete from pk_t whereobject_id=200;
--回到session1:死鎖馬上發(fā)生
delete from pk_t whereobject_id=100;
session2中報錯:
SQL> delete from pk_table where object_id=200;
delete from pk_table where object_id=200
*
第 1 行出現(xiàn)錯誤:
ORA-00060: 等待資源時檢測到死鎖
當(dāng)對子表的外鍵列添加索引后,死鎖被消除,因為這時刪除父表記錄不需要對子表加表級鎖。
--為外鍵建立索引
create index ind_pk_object_id on fk_t(object_id) nologging;
--重復(fù)上面的操作session1
delete from fk_t whereobject_id=100;
delete from pk_t whereobject_id=100;
--session2
delete from fk_t whereobject_id=200;
delete from pk_t whereobject_id=200;
--回到session1不會發(fā)生死鎖
delete from pk_t whereobject_id=100;
相關(guān)文章
EF 配置Oracle數(shù)據(jù)庫數(shù)據(jù)庫連接字符串的實例
下面小編就為大家?guī)硪黄狤F 配置Oracle數(shù)據(jù)庫數(shù)據(jù)庫連接字符串的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Oracle進(jìn)階DECODE函數(shù)使用詳解
這篇文章介紹了Oracle進(jìn)階DECODE函數(shù)的使用方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12Oracle 數(shù)據(jù)庫優(yōu)化實戰(zhàn)心得總結(jié)
優(yōu)化sql語句、優(yōu)化io、表設(shè)計優(yōu)化、充分利用系統(tǒng)cpu資源、優(yōu)化數(shù)據(jù)庫連接、充分利用數(shù)據(jù)的后臺處理方案減少網(wǎng)絡(luò)流量,實施系統(tǒng)資源管理分配計劃等等,感興趣的朋友可以參考下哈2013-06-06Oracle數(shù)據(jù)庫 DGbroker三種保護(hù)模式的切換
這篇文章主要介紹了Oracle數(shù)據(jù)庫 DGbroker三種保護(hù)模式的切換 的相關(guān)資料,需要的朋友可以參考下2015-12-12WINDOWS下使用DOS命令行連接oracle數(shù)據(jù)庫
本文講述了通過windows下的DOS命令連接oracle數(shù)據(jù)庫并進(jìn)行簡單操作的方法2018-03-03常見數(shù)據(jù)庫系統(tǒng)比較 Oracle數(shù)據(jù)庫
常見數(shù)據(jù)庫系統(tǒng)比較 Oracle數(shù)據(jù)庫...2007-03-03