insert,update和delete造成阻塞的示例
阿新 • • 發佈:2019-02-02
1.update 產生鎖示例:
session 1:
SQL> drop table t purge;
SQL> create table t(id int primary key);
Table created.
SQL> select sid from v$mystat where rownum=1; SID
----------
854
SQL> insert into t values(1); 1 row created. SQL> commit; Commit complete. SQL> update t set id=2 where id=1; --更新未提交 1 row updated. session 2:
SQL> select sid from v$mystat where rownum=1; SID
----------
834 SQL> update t set id=3 where id=1; --此時更新同條記錄時SQL產生等待 session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2; SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 655407 1648480 0 6 0
854 TM 91874 0 3 0 0
854 TX 655407 1648480 6 0 1 SQL> select sid,event from v$session_wait where sid in (834,854); SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client
說明:
這裡sid=854是session1,sid=834是session2;
TM是一個表級鎖(段鎖),表示此表的記錄在修改時,不允許對錶進行DDL操作;
TX是一個行級鎖(事務鎖),表示不允許對錶修改的記錄進行DML操作;
當TY=TM and ID2=0時,ID1的值為此表(段)的object_id;
當TY=TX and ID2<>0時,ID1+ID2構成了這個事務在回滾段中的位置;
LMODE=3表示一個表級共享鎖,LMODE=6行級的排他鎖(最高模式的鎖);
REQUEST=6 表示當前會話正待等待一個LMODE=6 的鎖,表明這個會話正在被阻塞;
block=1是表示這個會話正在阻塞其它會話;
2.delete 產生鎖示例:
session 1:
SQL> select * from t; ID
----------
1 SQL> delete from t where id=1; --刪除SQL未提交 1 row deleted. session 2:
SQL> delete from t where id=1; --此時刪除同條記錄時SQL產生等待 session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2; SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 655364 1648669 0 6 0
854 TM 91874 0 3 0 0
854 TX 655364 1648669 6 0 1 SQL> select sid,event from v$session_wait where sid in (834,854); SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client 說明:
同update一樣
3.insert 產生鎖示例: session 1:
SQL> drop table t purge;
SQL> create table t(id int primary key);
Table created. SQL> insert into t values(1); --插入未提交
1 row created. session 2:
SQL> insert into t values(1); --此時插入有同樣主鍵記錄時SQL產生等待 session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2; SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 262174 192335 6 0 0
834 TX 458776 193901 0 4 0
854 TM 91874 0 3 0 0
854 TX 458776 193901 6 0 1 SQL> select sid,event from v$session_wait where sid in (834,854); SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client
說明:
這裡和update產生鎖的情況不一樣的是,會話2在持有一個LMODE=6的排他鎖和等待一個LMODE=4的鎖。
因插入的並不是同一條記錄,會話2插入時的記錄沒被阻塞,而是對錶的資料塊頭修改的阻塞,所以會話在請求一個LMODE=4的鎖。
SQL> drop table t purge;
SQL> create table t(id int primary key);
Table created.
SQL> select sid from v$mystat where rownum=1; SID
----------
854
SQL> insert into t values(1); 1 row created. SQL> commit; Commit complete. SQL> update t set id=2 where id=1; --更新未提交 1 row updated. session 2:
SQL> select sid from v$mystat where rownum=1; SID
----------
834 SQL> update t set id=3 where id=1; --此時更新同條記錄時SQL產生等待 session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2; SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 655407 1648480 0 6 0
854 TM 91874 0 3 0 0
854 TX 655407 1648480 6 0 1 SQL> select sid,event from v$session_wait where sid in (834,854); SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client
說明:
TM是一個表級鎖(段鎖),表示此表的記錄在修改時,不允許對錶進行DDL操作;
TX是一個行級鎖(事務鎖),表示不允許對錶修改的記錄進行DML操作;
當TY=TM and ID2=0時,ID1的值為此表(段)的object_id;
當TY=TX and ID2<>0時,ID1+ID2構成了這個事務在回滾段中的位置;
LMODE=3表示一個表級共享鎖,LMODE=6行級的排他鎖(最高模式的鎖);
REQUEST=6 表示當前會話正待等待一個LMODE=6 的鎖,表明這個會話正在被阻塞;
block=1是表示這個會話正在阻塞其它會話;
2.delete 產生鎖示例:
SQL> select * from t; ID
----------
1 SQL> delete from t where id=1; --刪除SQL未提交 1 row deleted. session 2:
SQL> delete from t where id=1; --此時刪除同條記錄時SQL產生等待 session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2; SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 655364 1648669 0 6 0
854 TM 91874 0 3 0 0
854 TX 655364 1648669 6 0 1 SQL> select sid,event from v$session_wait where sid in (834,854); SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client 說明:
3.insert 產生鎖示例: session 1:
SQL> drop table t purge;
SQL> create table t(id int primary key);
Table created. SQL> insert into t values(1); --插入未提交
1 row created. session 2:
SQL> insert into t values(1); --此時插入有同樣主鍵記錄時SQL產生等待 session 3:
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where sid in (834,854) order by 1,2; SID TY ID1 ID2 LMODE REQUEST BLOCK
---------- -- ---------- ---------- ---------- ---------- ----------
834 TM 91874 0 3 0 0
834 TX 262174 192335 6 0 0
834 TX 458776 193901 0 4 0
854 TM 91874 0 3 0 0
854 TX 458776 193901 6 0 1 SQL> select sid,event from v$session_wait where sid in (834,854); SID EVENT
---------- ----------------------------------------------------------------
834 enq: TX - row lock contention
854 SQL*Net message from client
說明:
這裡和update產生鎖的情況不一樣的是,會話2在持有一個LMODE=6的排他鎖和等待一個LMODE=4的鎖。
因插入的並不是同一條記錄,會話2插入時的記錄沒被阻塞,而是對錶的資料塊頭修改的阻塞,所以會話在請求一個LMODE=4的鎖。