欢迎来到代码驿站!

Oracle

当前位置:首页 > 数据库 > Oracle

Oracle外键不加索引引起死锁示例

时间:2021-03-12 09:52:32|栏目:Oracle|点击:
--创建一个表,此表作为子表

create table fk_t as select *from user_objects;

delete from fk_t where object_id is null;

commit;

--创建一个表,此表作为父表

create table pk_t as select *from user_objects;

delete from pk_t where object_id is null;

commit;

--创建父表的主键

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;

上一篇:简单实例解释Oracle分页查询

栏    目:Oracle

下一篇:Oracle解析复杂json的方法实例详解

本文标题:Oracle外键不加索引引起死锁示例

本文地址:http://www.codeinn.net/misctech/78922.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有