enq: TM - contention TM 等待事件的原因及模擬
客戶反映資料庫在執行遠端INSERT的時候應用反映很慢,在做AWR後發現enq: TM - contention等待事件很高,應該是外來鍵約束上沒有建立索引的問題。
下面我來做個實驗還原一下當時的現象。
CREATE TABLE supplier
( supplier_id number(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
INSERT INTO supplier VALUES (1, 'Supplier 1', 'Contact 1');
INSERT INTO supplier VALUES (2, 'Supplier 2', 'Contact 2');
COMMIT;
CREATE TABLE product
( product_id number(10) not null,
product_name varchar2(50) not null,
supplier_id number(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE
);
INSERT INTO product VALUES (1, 'Product 1', 1);
INSERT INTO product VALUES (2, 'Product 2', 1);
INSERT INTO product VALUES (3, 'Product 3', 2);
COMMIT;
然後去執行幾條相關的語句
User 1: DELETE supplier WHERE supplier_id = 1;
User 2: DELETE supplier WHERE supplier_id = 2;(現象HANG住)
User 3: INSERT INTO supplier VALUES (5, 'Supplier 5', 'Contact 5');(現象HANG住)
現在檢查鎖的情況
col event format a20
col type format a10
col object_name a15
col object_type a15
SELECT l.sid, s.blocking_session blocker, s.event, l.type, l.lmode, l.request, o.object_name, o.object_type
FROM v$lock l, dba_objects o, v$session s
WHERE UPPER(s.username) = UPPER('&User')
AND l.id1 = o.object_id (+)
AND l.sid = s.sid
ORDER BY sid, type;
輸入 user 的值: test
原值 3: WHERE UPPER(s.username) = UPPER('&User')
新值 3: WHERE UPPER(s.username) = UPPER('test')
SID BLOCKER EVENT TYPE LMODE REQUEST
---------- ---------- -------------------- ---------- ---------- ----------
OBJECT_NAME OBJECT_TYPE
--------------- ---------------
144 158 enq: TM - contention TM 3 0
SUPPLIER TABLE
144 158 enq: TM - contention TM 0 2
PRODUCT TABLE
153 SQL*Net message from TM 3 0
client
SUPPLIER TABLE
SID BLOCKER EVENT TYPE LMODE REQUEST
---------- ---------- -------------------- ---------- ---------- ----------
OBJECT_NAME OBJECT_TYPE
--------------- ---------------
153 SQL*Net message from TM 3 0
client
PRODUCT TABLE
153 SQL*Net message from TX 6 0
client
SID BLOCKER EVENT TYPE LMODE REQUEST
---------- ---------- -------------------- ---------- ---------- ----------
OBJECT_NAME OBJECT_TYPE
--------------- ---------------
158 153 enq: TM - contention TM 0 5
PRODUCT TABLE
158 153 enq: TM - contention TM 3 0
SUPPLIER TABLE
已選擇7行。
SQL>
能看到enq: TM - contention TM 等待事件
檢查沒有索引的外來鍵
SELECT * FROM (
SELECT c.table_name, cc.column_name, cc.position column_position
FROM user_constraints c, user_cons_columns cc
WHERE c.constraint_name = cc.constraint_name
AND c.constraint_type = 'R'
MINUS
SELECT i.table_name, ic.column_name, ic.column_position
FROM user_indexes i, user_ind_columns ic
WHERE i.index_name = ic.index_name
)
ORDER BY table_name, column_position;
TABLE_NAME
------------------------------
COLUMN_NAME
--------------------------------------------------------------------------------
COLUMN_POSITION
---------------
PRODUCT
SUPPLIER_ID
1
建立索引後:
CREATE INDEX fk_supplier ON product (supplier_id);
INSERT INTO supplier VALUES (6, 'Supplier 6', 'Contact 6');
INSERT INTO supplier VALUES (7, 'Supplier 7', 'Contact 7');
User 1: DELETE supplier WHERE supplier_id = 6;
User 2: DELETE supplier WHERE supplier_id = 7;
User 3: INSERT INTO supplier VALUES (8, 'Supplier 8', 'Contact 8');
相關的等待事件消失。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/628922/viewspace-1424083/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- enq:TM-contention事件等待ENQ事件
- Oracle等待事件之enq: TM – contentionOracle事件ENQ
- zt_Oracle enq: TX contention 和 enq: TM contention 等待事件OracleENQ事件
- enq: TM - contentionENQ
- enq:TM contentionENQ
- oracle 11.2.0.4 rac叢集等待事件enq: TM - contentionOracle事件ENQ
- enq: WF - contention等待事件ENQ事件
- enq: CF - contention 等待事件ENQ事件
- enq: TS - contention 等待事件ENQ事件
- awr報告中顯示enq: TM - contention 處理方法ENQ
- 等待事件之enq: HW - contention事件ENQ
- 消除 enq: DX - contention 等待事件ENQ事件
- 等待事件enq: TX - row lock contention事件ENQ
- oracle等待事件之enq: CF – contentionOracle事件ENQ
- 【等待事件】-enq: TX - row lock contention事件ENQ
- 事務上的等待事件 —— enq: UL - contention事件ENQ
- 故障排除 | enq:TX - index contention等待事件ENQIndex事件
- 等待事件enq TX row lock contention分析事件ENQ
- 如何診斷等待事件 enq: HW - contention事件ENQ
- 【效能調整】等待事件 enq: SQ - contention事件ENQ
- 一次TM ENQ故障處理ENQ
- enq: SQ - contention 等待事件處理辦法ENQ事件
- enq: TX - row lock contention等待事件處理ENQ事件
- enq: TM - contention解決之道——外來鍵無索引導致鎖爭用ENQ索引
- enq: TM - contention解決之道——外來鍵無索引導致鎖爭用(上)ENQ索引
- enq: TM - contention解決之道——外來鍵無索引導致鎖爭用(下)ENQ索引
- enq: TX - index contention等待ENQIndex
- [20161208]等待事件enq: HW - contention事件ENQ
- [20140311]等待事件enq HW - contention事件ENQ
- [20220518]enq FU - contention等待事件.txtENQ事件
- 大量insert引起的enq: HW - contention等待ENQ
- TM LOCK MODE
- TM表鎖各種mode的實驗及2-6 的TM鎖相互間的互斥示例
- 資料庫出現很高的enq: DX - contention 等待資料庫ENQ
- library cache pin等待事件的模擬事件
- read by other session等待事件模擬Session事件
- oracle的TM鎖、TX鎖Oracle
- 故障處理】佇列等待之enq: US - contention案例佇列ENQ