If index not exists on foreign key column of a child table then it will create locks on the parent table when DML operation performed on child table.
Demonstration:
Create following sample tables. order_header is parent and order_detail is child table.
create table order_header (header_id number, customer varchar2(20))
alter table order_header add constraint pk_order_header primary key (header_id)
create table order_detail (header_id number references order_header (header_id) on delete cascade , line_id number);
insert into order_header values (1,'a1');
insert into order_header values (2,'a2');
insert into order_header values (3,'a3');
insert into order_detail values (1,1);
insert into order_detail values (2,1);
commit;
Now, open two sessions to the database,
From session-1, delete a record from child table.
From session-2, delete a record from parent table. This transaction is going to be locked until the transaction in session-1 is either committed or rolled back.
To avoid this lock it is recommend to create a index on child table column.
create index order_detail_idx on order_detail (header_id);
Now, if we try the same delete statements, we wont see locking any more....
Demonstration:
Create following sample tables. order_header is parent and order_detail is child table.
create table order_header (header_id number, customer varchar2(20))
alter table order_header add constraint pk_order_header primary key (header_id)
create table order_detail (header_id number references order_header (header_id) on delete cascade , line_id number);
insert into order_header values (1,'a1');
insert into order_header values (2,'a2');
insert into order_header values (3,'a3');
insert into order_detail values (1,1);
insert into order_detail values (2,1);
commit;
Now, open two sessions to the database,
From session-1, delete a record from child table.
From session-2, delete a record from parent table. This transaction is going to be locked until the transaction in session-1 is either committed or rolled back.
To avoid this lock it is recommend to create a index on child table column.
create index order_detail_idx on order_detail (header_id);
Now, if we try the same delete statements, we wont see locking any more....
I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.mobile phone repair in Novi
ReplyDeleteiphone repair in Novi
cell phone repair in Novi
phone repair in Novi
tablet repair in Novi
ipad repair in Novi
mobile phone repair Novi
iphone repair Novi
cell phone repair Novi
phone repair Novi
great post. Full Stack Course In Pune
ReplyDelete