Oracle外键不加索引引起死锁示例
网络编程 2021-07-05 16:08www.168986.cn编程入门
这篇文章主要介绍了Oracle外键不加索引引起死锁的情况及解决,需要的朋友可以参考下
--创建一个表,此表作为子表
create table fk_t as select from user_objects;
delete from fk_t where object_id is null;
mit;
--创建一个表,此表作为父表
create table pk_t as select from user_objects;
delete from pk_t where object_id is null;
mit;
--创建父表的主键
alter table PK_t add constraintpk_pktable primary key (OBJECT_ID);
--创建子表的外键
alter table FK_t addconstraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);
--session1:执行一个删除操作,这时候在子表和父表上都加了一个Row-S(SX)锁
delete from fk_t whereobject_id=100;
delete from pk_t where object_id=100;
--session2:执行另一个删除操作,发现这时候第二个删除语句等待
delete from fk_t whereobject_id=200;
delete from pk_t whereobject_id=200;
--回到session1:死锁马上发生
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 行出现错误:
ORA-00060: 等待资源时检测到死锁
当对子表的外键列添加索引后,死锁被消除,因为这时删除父表记录不需要对子表加表级锁。
--为外键建立索引
create index ind_pk_object_id on fk_t(object_id) nologging;
--重复上面的操作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不会发生死锁
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;
mit;
--创建一个表,此表作为父表
create table pk_t as select from user_objects;
delete from pk_t where object_id is null;
mit;
--创建父表的主键
alter table PK_t add constraintpk_pktable primary key (OBJECT_ID);
--创建子表的外键
alter table FK_t addconstraint fk_fktable foreign key (OBJECT_ID) references pk_t (OBJECT_ID);
--session1:执行一个删除操作,这时候在子表和父表上都加了一个Row-S(SX)锁
delete from fk_t whereobject_id=100;
delete from pk_t where object_id=100;
--session2:执行另一个删除操作,发现这时候第二个删除语句等待
delete from fk_t whereobject_id=200;
delete from pk_t whereobject_id=200;
--回到session1:死锁马上发生
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 行出现错误:
ORA-00060: 等待资源时检测到死锁
当对子表的外键列添加索引后,死锁被消除,因为这时删除父表记录不需要对子表加表级锁。
--为外键建立索引
create index ind_pk_object_id on fk_t(object_id) nologging;
--重复上面的操作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不会发生死锁
delete from pk_t whereobject_id=100;
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南