关于enq: TX - index contention 等待的探讨与测试

关于enq: TX - index contention 等待的探讨与测试

最近生产库上遭遇短时间的enq: TX - index contention 等待,导致数据库hang住:
这个等待事件解释如下:
Waits for TX in mode 4 also occur when a transaction inserting a row in an index has to wait for the end of an index block split being done by another transaction. This type of TX enqueue wait corresponds to the wait event enq: TX - index contention.

可以认为一个session在向一个索引块中执行插入时产生了索引块的split,而其它的session也要往该索引块中插入数据,此时,其它session必须要等待split完成,由此引发了该等待事件。

这个等待事件可以进行模拟:

创建测试表t1,并创建索引:

create table t1(x number,y char(20),z date,q varchar2(4000)) tablespace users;

create index t1_idx1 on t1(q,z) tablespace users;

用2个session对表t1进行并发insert:

session 1:

SQL> select sid from v$mystat where rownum=1;


----------
        50

SQL>
SQL> begin
2    for x in 1..5000 loop
3        insert into t1 values(1162,'1060000abcdefg', sysdate, rpad('x',2000,'x'));
4    end loop;
5    end;
6    /

PL/SQL procedure successfully completed.


session 2:
SQL> select sid from v$mystat where rownum=1;

       SID
----------
        32

SQL> begin
2    for x in 1..5000 loop
3        insert into t1 values(1162,'1060000abcdefg', sysdate, rpad('x',2000,'x'));
4    end loop;
5    end;
6    /

PL/SQL procedure successfully completed.


session 3:

插入前:

SQL> select sid,event,time_waited from v$session_event where event ='enq: TX - index contention' and sid in (50,32);

no rows selected

插入后:

SQL> select sid,event,time_waited from v$session_event where event ='enq: TX - index contention' and sid in (50,32);

       SID EVENT                                    TIME_WAITED
---------- ---------------------------------------- -----------
        32 enq: TX - index contention                       102
        50 enq: TX - index contention                        49

从抓取的ash报告来看,产生等待的是一条insert语句,而该sql要插入数据的表是一个每天需要进行频繁delete的表,该等待事件的产生与频繁的大批量delete是具有紧密联系的。厂商最后给出的建议是定期对该表进行rebuild,并加大索引的pctfree。



你可能感兴趣的:(sql,数据库,session,测试,delete,insert)