Oracle enq: TX contention 和 enq: TM contention 等待事件说明

Oracle性能优化密切相关的一些知识参考如下Blog

Oracle 常见的33个等待事件

http://blog.csdn.net/tianlesoftware/archive/2010/08/12/5807800.aspx

死锁 阻塞 Latch 等待 详解

http://blog.csdn.net/tianlesoftware/archive/2010/08/20/5822674.aspx

Oracle Latch 说明

http://blog.csdn.net/tianlesoftware/archive/2010/01/27/5263238.aspx

在网上搜到一篇介绍enq: TX enq:TM的文章。原文地址如下:

http://aprakash.wordpress.com/2011/01/17/enq-tx-row-lock-contention-and-enqtm-contention/

Enqueue 这个词其实是lock 的另一种描述语。当我们在AWR 报告中发现长时间的enqueue 等待事件时,说明数据库中出现了阻塞和等待,可以关联AWR报告中的enqueue activity部分来确定是哪一种锁定出现了长时间等待。

关于Enqueue的所有类型参考《Oracle 常见的33个等待事件》中的Equeue说明。 在这里,我们看一下enq:TX enqTM

. enq: TX row lock contention

enq:Tx 会在模式64 下出现。

Oracle enqueue 包含以下模式:

模式代码

类型

解释

1

Null

Null mode

2

SS

Sub-Share

3

SX

Sub-Exclusive

4

S

Share

5

SSX

Share/Sub-Exclusive

6

X

Exclusive

1.1 enq” TX row lock contention - Mode 6 (Exclusive)

Session 1
==========

ANAND@MATRIX> select * from sa;

A B

---------- ----------

1 1

2 2

3 3

4 4

5 5

Elapsed: 00:00:00.03

ANAND@MATRIX> delete from sa where A=5;

1 row deleted.

Elapsed: 00:00:00.00

From session 2
=====================

ANAND@MATRIX> delete from sa where a=5;

Checking from sys session (session # 3)

Session 3
============

SYS@MATRIX> select addr,xidusn,xidslot,xidsqn,ubafil,ubablk,ubasqn,ses_addr,xid from v$transaction;

addr xidusn xidslot xidsqn ubafil ubablk ubasqn ses_addr xid

-------- ---------- ---------- ---------- ---------- ---------- ---------- -------- ----------------

218B6D98 10 29 848 3 2661 339 232C8B5C 0A001D0050030000

Elapsed: 00:00:00.01

SYS@MATRIX> @lock_sid

Enter value for sid: 135

old 82: where sid = &sid

new 82: where sid = 135

sid lock type id1 id2 lock held lock request time held status

----- ---------------------------------------- --------- --------- -------------- ----------------

135 ???? 65921 1 Row Exclu(3) None(0) 5079.000 Not Blocking

135 DML enqueue 75404 0 Row Exclu(3) None(0) 49.000 Not Blocking

135 ???? 100 0 Share(4) None(0) 5224.000 Not Blocking

135 Transaction 655389 848 Exclusive(6) None(0) 49.000 Blocking

SYS@MATRIX> @sw 135

old 38: sid IN (&1)

new 38: sid IN (135)

sid state event seq# sec_in_wait p1 p2 p3 p1transl

------- ------- ---------------------------------------- ---------- ----------- ------------------ ------------------ ------------------ ------------------------------------------

135 WAITING SQL*Net message from client 193 131 driver id= #bytes= 1 0

0x0000000042455100

SYS@MATRIX> @sw 10

old 38: sid IN (&1)

new 38: sid IN (10)

sid state event seq# sec_in_wait p1 p2 p3 p1transl

------- ------- ---------------------------------------- ---------- ----------- ------------------

10 WAITING enq: TX - row lock contention 35 146 name|mode= usn<

SYS@MATRIX>@enqueue

INST_ID SESS ID1 ID2 Lock Held Lock Requested Lock Type

---------- ------------------------------------------------ -------- -------- --------- --------------

1 Holder: 135 655389 848 6 0 TX

1 Waiter: 10 655389 848 0 6 TX

Back to session 1
==================

ANAND@MATRIX> commit;

Commit complete.

Elapsed: 00:00:00.01

Session 2
===============

22:22:52 ANAND@MATRIX> delete from sa where a=5;

0 rows deleted.

Elapsed: 00:05:07.70

1.2. enq: TX row lock contention – Mode 4

The common cause for mode 4, enq: TX row lock contention are

1Unique Index
2
Foreign key
3Bitmap indexes

1.2.1 Unique Index

Session 1
============

ANAND@MATRIX> alter table sa add constraint pk_sa primary key (B);

Table altered.

Elapsed: 00:00:00.29

ANAND@MATRIX> desc sa

Name Null? Type

----------------------------------------------------------------------------------------------

A NUMBER

B NOT NULL NUMBER

ANAND@MATRIX> select * from sa;

A B

---------- ----------

1 1

2 2

3 3

4 4

Elapsed: 00:00:00.04

ANAND@MATRIX> insert into sa values (5,5);

1 row created.

Elapsed: 00:00:00.01

Didn’t commit. Check session 2

Session 2
===============

ANAND@MATRIX> insert into sa values (5,5);

The session hangs.Check the session waits and enqueues.

Session 3
==============

SYS@MATRIX> @lock_sid

Enter value for sid: 135

old 82: where sid = &sid

new 82: where sid = 135

SID Lock Type ID1 ID2 Lock Held Lock Request Time Held STATUS

----- ---------------------------------------- --------- --------- -------------- ----------------

135 ???? 65921 1 Row Exclu(3) None(0) 5779.000 Not Blocking

DML enqueue 75404 0 Row Exclu(3) None(0) 134.000 Not Blocking

???? 100 0 Share(4) None(0) 5924.000 Not Blocking

Transaction 393229 1069 Exclusive(6) None(0) 134.000 Blocking

Elapsed: 00:00:00.03

SYS@MATRIX> @enqueue

INST_ID SESS ID1 ID2 Lock Held Lock Requested Lock Type

---------- ------------------------------------------------ -------- -------- --------- --------------

1 Holder: 135 393229 1069 6 0 TX

1 Waiter: 10 393229 1069 0 4 TX

Elapsed: 00:00:00.04

SYS@MATRIX> @sw 135

old 38: sid IN (&1)

new 38: sid IN (135)

sid state event seq# sec_in_wait p1 p2 p3 p1transl

------- ------- ---------------------------------------- ---------- ----------- ------------------

135 WAITING SQL*Net message from client 229 45 driver id= #bytes= 1 0

0x0000000042455100

Elapsed: 00:00:00.01

SYS@MATRIX> @sw 10

old 38: sid IN (&1)

new 38: sid IN (10)

sid state event seq# sec_in_wait p1 p2 p3 p1transl

------- ------- ---------------------------------------- ---------- ----------- ------------------

10 WAITING enq: TX - row lock contention 44 26 name|mode= usn<<16 | slot= sequence= 1069 0x54580004: TX mode 4 0x0000000054580004 393229

After commit on session1 , the session 2 throws Unique constraint violated error

Session 2
============

ANAND@MATRIX> insert into sa values (5,5);

insert into sa values (5,5)

*

ERROR at line 1:

ORA-00001: unique constraint (ANAND.PK_SA) violated

Elapsed: 00:02:18.10

1.2.2 Foreign key

Session 1
===========

ANAND@MATRIX> create table sa_child (id number references sa(B) ,name varchar2(10));

Table created.

Elapsed: 00:00:00.25

ANAND@MATRIX> select constraint_name,constraint_type,table_name,r_constraint_name,status from user_constraints where table_name in ('sa','sa_child');

CONSTRAINT_NAME C TABLE_NAME R_CONSTRAINT_NAME STATUS

------------------------------ - ------------------------------ ------------------------------ --------

PK_SA P SA ENABLED

SYS_C0011533 R SA_CHILD PK_SA ENABLED

Elapsed: 00:00:00.18

ANAND@MATRIX> insert into sa values (6,6);

1 row created.

Elapsed: 00:00:00.03

Lets insert the same value 6 in SA_CHILD from Session 2

Session 2
===========

ANAND@MATRIX> insert into sa_child values (6,'DANNY');

Session 2 hangs.

Session 3
===========

SYS@MATRIX> @enqueue

INST_ID SESS ID1 ID2 Lock Held Lock Requested Lock Type

---------- ------------------------------------------------ --------- --------- --------- -------------- -----------

1 Holder: 135 655360 884 6 0 TX

1 Waiter: 10 655360 884 0 4 TX

SYS@MATRIX> @lock_sid

Enter value for sid: 135

old 82: where sid = &sid

new 82: where sid = 135

SID Lock Type ID1 ID2 Lock Held Lock Request Time Held STATUS

----- ---------------------------------------- --------- --------- -------------- ---------------- ----------- --------------------

135 DML enqueue 75594 0 Row Exclu(3) None(0) 64.000 Not Blocking

DML enqueue 75404 0 Row Exclu(3) None(0) 64.000 Not Blocking

???? 100 0 Share(4) None(0) 183421.000 Not Blocking

Transaction 655360 884 Exclusive(6) None(0) 64.000 Blocking

6 rows selected.

Elapsed: 00:00:00.09

SYS@MATRIX> @sw 10

old 38: sid IN (&1)

new 38: sid IN (10)

SID STATE EVENT SEQ# SEC_IN_WAIT P1 P2 P3 P1TRANSL

------- ------- ---------------------------------------- ---------- ----------- ------------------ ------------------ ------------------ -------------------------------

10 WAITING enq: TX - row lock contention 58 27 name|mode= usn<<16 | slot= sequence= 884 0x54580004: TX mode 4 0x0000000054580004 655360

Elapsed: 00:00:00.03

SYS@MATRIX> @sw 135

old 38: sid IN (&1)

new 38: sid IN (135)

SID STATE EVENT SEQ# SEC_IN_WAIT P1 P2 P3 P1TRANSL

------- ------- ---------------------------------------- ---------- ----------- ------------------

135 WAITING SQL*Net message from client 686 59 driver id= #bytes= 1 0 0x0000000042455100

So, the session 2 is waiting for enq: TX row lock contention as before inserting the row it needs check for the value in the parent table.If the row is not there , then it would throw erroe “ORA-02291: integrity constraint (ANAND.SYS_C0011533) violated – parent key not found”.

After commit on session1 ,

Session 2
===========

ANAND@MATRIX> insert into sa_child values (6,'DANNY');

1 row created.

Elapsed: 00:05:02.03

1.2.3 Bitmap Index

Session 1
==========

ANAND@MATRIX> select * from sa;

A B C

---------- ---------- ----------

1 1 99

2 2 99

3 3 99

4 4 99

6 6 99

5 5 99

7 7 99

7 rows selected.

Elapsed: 00:00:00.06

ANAND@MATRIX> create bitmap index sa_c_bit_indx on sa(C);

Index created.

Elapsed: 00:00:00.10

ANAND@MATRIX> insert into sa values (8,8,99);

1 row created.

Elapsed: 00:00:00.03

Lets try deleteing a row from sa table from Session 2

Session 2
=============

ANAND@MATRIX> delete from sa where A=1;

Session 2 hangs.

Session 3
===========

SYS@MATRIX> @lock_sid

Enter value for sid: 135

old 82: where sid = &sid

new 82: where sid = 135

SID Lock Type ID1 ID2 Lock Held Lock Request Time Held STATUS

----- ---------------------------------------- --------- --------- -------------- ---------------- ----------- ----------------------------------------

135 DML enqueue 75404 0 Row Exclu(3) None(0) 87.000 Not Blocking

DML enqueue 75594 0 Row Exclu(3) None(0) 87.000 Not Blocking

???? 100 0 Share(4) None(0) 185049.000 Not Blocking

Transaction 589847 1046 Exclusive(6) None(0) 87.000 Blocking

8 rows selected.

Elapsed: 00:00:00.07

SYS@MATRIX> @sw 135

old 38: sid IN (&1)

new 38: sid IN (135)

SID STATE EVENT SEQ# SEC_IN_WAIT P1 P2 P3 P1TRANSL

------- ------- ---------------------------------------- ---------- ----------- ------------------ ------------------ ------------------ -------------------------------------

135 WAITING SQL*Net message from client 796 71 driver id= #bytes= 1 0 0x0000000042455100

Elapsed: 00:00:00.03

SYS@MATRIX> @sw 10

old 38: sid IN (&1)

new 38: sid IN (10)

SID STATE EVENT SEQ# SEC_IN_WAIT P1 P2 P3 P1TRANSL

------- ------- ---------------------------------------- ---------- ----------- ------------------

10 WAITING enq: TX - row lock contention 101 37 name|mode= usn<<16 | slot= sequence= 1046 0x54580004: TX mode 4 0x0000000054580004 589847

Elapsed: 00:00:00.03

SYS@MATRIX> @enqueue

INST_ID SESS ID1 ID2 Lock Held Lock Requested Lock Type

---------- ------------------------------------------------ --------- 

1 Holder: 135 589847 1046 6 0 TX

1 Waiter: 10 589847 1046 0 4 TX

Elapsed: 00:00:00.04

As soon as session 1, the transaction in session2 completes.

Session 2
=============

ANAND@MATRIX> delete from sa where a=1;

1 row deleted.

Elapsed: 00:10:00.78

So, those were the few common causes of enq: TX contention that i have encountered till now.

. enq: TM – contention
=======================

Coming on to enq: TM contention, its a table level lock and the locked resource is database objects like table,index, partitions..
The most most common cause of enq:TM contention that i have seen is unindexed foreign keys.But, few days back i came to know about one more which was the main reason behind writing this blog.

2.1 Unindexed Foreign Keys

Session 1
==========

ANAND@MATRIX> create table parent_tab as select distinct object_type from dba_objects;

Table created.

Elapsed: 00:00:05.82

ANAND@MATRIX> create table child_tab

2 as

3 select object_id, object_type, object_name

4 from all_objects;

Table altered.

ANAND@MATRIX>alter table parent_tab add constraint pk_parent_tab primary key (object_type);

ANAND@MATRIX> alter table child_tab add constraint pk_child_tab primary key (object_id);

ANAND@MATRIX> alter table child_tab add constraint fk_child_parent_tab

2 foreign key (object_type) references parent_tab on delete cascade;

Table altered.

Elapsed: 00:00:00.07

ANAND@MATRIX> select count(*), object_type from child_tab group by object_type;

COUNT(*) OBJECT_TYPE

---------- -------------------

1 EDITION

39 SEQUENCE

454 TABLE

491 INDEX

5 SYNONYM

10 CLUSTER

6 rows selected.

Elapsed: 00:00:00.04

ANAND@MATRIX> delete from parent_tab where object_type = 'SYNONYM';

1 row deleted.

Elapsed: 00:00:00.01

Session 2
============

ANAND@MATRIX> delete from parent_tab where object_type = 'OPERATOR';

The Session 2 hangs. Lets check the wait events

Session 3
==============

SYS@MATRIX> @locks

SESS ID1 ID2 Lock Held REQUEST Lock Type

------------------------------------------------ --------- --------- --------- ---------- -----------

Holder: 134 75723 0 3 0 TM

Waiter: 13 75723 0 0 5 TM

Elapsed: 00:00:00.04

SYS@MATRIX> @sw 13

old 38: sid IN (&1)

new 38: sid IN (13)

SID STATE EVENT SEQ# SEC_IN_WAIT P1 P2 P3 P1TRANSL

------- ------- ---------------------------------------- ---------- ----------- ------------------

13 WAITING enq: TM - contention 37 15 name|mode= object #= 75723 table/partition= 0 0x544D0005: TM mode 5 0x00000000544D0005

Elapsed: 00:00:00.03

SYS@MATRIX> select object_name,object_type from dba_objects where object_id=75723;

OBJECT_NAME OBJECT_TYPE

-------------------------------------------------------------------------------------------------------------------------------- ------

CHILD_TAB TABLE

SYS@MATRIX> @lock_sid

Enter value for sid: 134

old 82: where sid = &sid

new 82: where sid = 134

SID Lock Type ID1 ID2 Lock Held Lock Request Time Held STATUS

----- ---------------------------------------- --------- --------- -------------- ---------------- ----------- ------------------------------------

134 Transaction 262171 982 Exclusive(6) None(0) 25.000 Not Blocking

DML enqueue 75722 0 Row Exclu(3) None(0) 25.000 Not Blocking

???? 100 0 Share(4) None(0) 10303.000 Not Blocking

DML enqueue 75723 0 Row Exclu(3) None(0) 25.000 Blocking

The simple way to resolve is, create an index on the foreign key of child table.

Tom 同学关于Unindexed Foreign Keys的说明:

From

http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteunindex.html

Having Unindexed foreign keys can be a performance issue. There are two issues associated with unindexed foreign keys. The first is the fact that a table lock will result if you update the parent records primary key (very very unusual) or if you delete the parent record and the child's foreign key is not indexed.

The second issue has to do with performance in general of a parent child relationship. Consider that if you have an on delete cascade and have not indexed the child table (eg: EMP is child of DEPT. Delete deptno = 10 should cascade to EMP. If deptno in emp is not indexed -- full table scan). This full scan is probably undesirable and if you delete many rows from the parent table, the child table will be scanned once for each parent row deleted.


Also consider that for most (not all, most) parent child relationships, we query the objects from the 'master' table to the 'detail' table. The glaring exception to this is a code table (short code to long description). For master/detail relationships, if you do not index the foreign key, a full scan of the child table will result.

Tom 同学提供的一个查询没有见外键索引的表的SQL:

/* Formatted on 2011/6/4 15:39:24 (QP5 v5.163.1008.3004) */

SELECT table_name,

constraint_name,

cname1

|| NVL2 (cname2, ',' || cname2, NULL)

|| NVL2 (cname3, ',' || cname3, NULL)

|| NVL2 (cname4, ',' || cname4, NULL)

|| NVL2 (cname5, ',' || cname5, NULL)

|| NVL2 (cname6, ',' || cname6, NULL)

|| NVL2 (cname7, ',' || cname7, NULL)

|| NVL2 (cname8, ',' || cname8, NULL)

columns

FROM ( SELECT b.table_name,

b.constraint_name,

MAX (DECODE (position, 1, column_name, NULL)) cname1,

MAX (DECODE (position, 2, column_name, NULL)) cname2,

MAX (DECODE (position, 3, column_name, NULL)) cname3,

MAX (DECODE (position, 4, column_name, NULL)) cname4,

MAX (DECODE (position, 5, column_name, NULL)) cname5,

MAX (DECODE (position, 6, column_name, NULL)) cname6,

MAX (DECODE (position, 7, column_name, NULL)) cname7,

MAX (DECODE (position, 8, column_name, NULL)) cname8,

COUNT (*) col_cnt

FROM (SELECT SUBSTR (table_name, 1, 30) table_name,

SUBSTR (constraint_name, 1, 30) constraint_name,

SUBSTR (column_name, 1, 30) column_name,

position

FROM user_cons_columns) a,

user_constraints b

WHERE a.constraint_name = b.constraint_name

AND b.constraint_type = 'R'

GROUP BY b.table_name, b.constraint_name) cons

WHERE col_cnt >

ALL ( SELECT COUNT (*)

FROM user_ind_columns i

WHERE i.table_name = cons.table_name

AND i.column_name IN

(cname1,

cname2,

cname3,

cname4,

cname5,

cname6,

cname7,

cname8)

AND i.column_position <= cons.col_cnt

GROUP BY i.index_name)

/

2.2 Direct path load or insert /*+ append */

Session 1
===========

ANAND@MATRIX> desc sa

Name Null? Type

-----------------------------------------------------------------------------------------------

A NUMBER

B NUMBER

C NUMBER

ANAND@MATRIX> select constraint_name,constraint_type from user_constraints where table_name='SA';

no rows selected

Elapsed: 00:00:00.15

ANAND@MATRIX> insert /*+ APPEND */ into sa select * from sa;

11 rows created.

Elapsed: 00:00:00.06

Session 2
===========

ANAND@MATRIX> insert /*+ APPEND */ into sa select * from sa;

Session 2 hangs.

Session 3
===============

SYS@MATRIX> @locks

SESS ID1 ID2 Lock Held REQUEST Lock Type

------------------------------------------------ --------- --------- --------- ----------

Holder: 69 75726 0 6 0 TM

Waiter: 13 75726 0 0 6 TM

Elapsed: 00:00:0

SYS@MATRIX> @lock_sid

Enter value for sid: 69

old 82: where sid = &sid

new 82: where sid = 69

SID Lock Type ID1 ID2 Lock Held Lock Request Time Held STATUS

----- ---------------------------------------- --------- --------- -------------- ---------------- ----------- ---------------

69 Transaction 458754 968 Exclusive(6) None(0) 55.000 Not Blocking

69 ???? 100 0 Share(4) None(0) 930.000 Not Blocking

69 DML enqueue 75726 0 Exclusive(6) None(0) 55.000 Blocking

Elapsed: 00:00:00.04

SYS@MATRIX> @sw 13

old 38: sid IN (&1)

new 38: sid IN (13)

SID STATE EVENT SEQ# SEC_IN_WAIT P1 P2 P3 P1TRANSL

------- ------- ---------------------------------------- ---------- ----------- ------------------

13 WAITING enq: TM - contention 43 35 name|mode= object #= 75726 table/partition= 0 0x544D0006: TM mode 6 0x00000000544D0006

Elapsed:00:00:00.03

SYS@MATRIX> select object_name,object_type from dba_objects where object_id=75726;

OBJECT_NAM OBJECT_TYPE

---------- -------------------

SA TABLE

Elapsed: 00:00:00.00

So, direct mode insert, or insert /*+ append */ will take TM enqueue in exclusive mode. And if the transaction doesn’t commit, we can see lots of bunch of sessions stack up behind it and that is what happened in my case.

2个等待事件也算比较常见的,了解这些对DB 优化还是有帮助的。

-------------------------------------------------------------------------------------------------------

Blog http://blog.csdn.net/tianlesoftware

Email: [email protected]

DBA1 群:62697716(); DBA2 群:62697977() DBA3 群:62697850()

DBA 超级群:63306533(); DBA4 群: 83829929 DBA5群: 142216823

DBA6 群:158654907 聊天 群:40132017 聊天2群:69087192

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

你可能感兴趣的:(content)