[转帖]等待事件 enq:TX - row lock contention分析与解决

等待,事件,enq,tx,row,lock,contention,分析,解决 · 浏览次数 : 0

小编点评

**排版内容** **1. 概述** * 2014年4月25日,12:23:53,用户54提交事务。 * 15分钟后,用户54提交事务再次。 * 16:23:53,用户54再次提交事务。 **2. 等待事件** * V$session中找到BLOCK=1的用户,ID为54。 * 54用户提交事务,等待锁。 **3. 阻塞锁** * V$lock中找到锁对象,ID为183598。 * 1用户锁定该对象。 **4. 等待结束** * V$session中找到阻塞锁的用户,ID为54。 * 54用户提交事务成功。 **5. 锁对象释放** * V$lock中找到锁对象,ID为183598。 * 1用户释放锁。 **6. 应用影响** * 由于存在等待锁,用户54提交事务最终无法成功。 * 必须检查应用是否导致等待锁。

正文

6月30日,数据库发生了大量锁表。大概持续1小时,并且越锁越多。后来通过业务人员停掉程序,并kill掉会话后解决。

几天后再EM上查看CPU占用:
在这里插入图片描述
CPU发生了明显等待。
在这里插入图片描述
主要是由于enq:TX - row lock contention等待事件造成。

等待事件—enq:TX - row lock contention

enq是一种保护共享资源的锁定机制,一个排队机制,先进先出(FIFO)

发生TX锁的原因一般有几个:
1.不同的session更新或删除同一个记录。
2.唯一索引有重复索引
3.位图索引多次更新
4.同时对同一个数据块更新
5.等待索引块分裂

官网上关于TX - row lock contention的内容:

Oracle官网上关于TX - row lock contention的内容

10.3.7.2.4 TX enqueue

These are acquired exclusive when a transaction initiates its first change and held until the transaction does a COMMIT or ROLLBACK.
Waits for TX in mode 6: occurs when a session is waiting for a row level lock that is already held by another session. This occurs when one user is updating or deleting a row, which another session wishes to update or delete. This type of TX enqueue wait corresponds to the wait event enq: TX - row lock contention.
The solution is to have the first session already holding the lock perform a COMMIT or ROLLBACK.
Waits for TX in mode 4 can occur if the session is waiting for an ITL (interested transaction list) slot in a block. This happens when the session wants to lock a row in the block but one or more other sessions have rows locked in the same block, and there is no free ITL slot in the block. Usually, Oracle dynamically adds another ITL slot. This may not be possible if there is insufficient free space in the block to add an ITL. If so, the session waits for a slot with a TX enqueue in mode 4. This type of TX enqueue wait corresponds to the wait event enq: TX - allocate ITL entry.
The solution is to increase the number of ITLs available, either by changing the INITRANS or MAXTRANS for the table (either by using an ALTER statement, or by re-creating the table with the higher values).
Waits for TX in mode 4 can also occur if a session is waiting due to potential duplicates in UNIQUE index. If two sessions try to insert the same key value the second session has to wait to see if an ORA-0001 should be raised or not. This type of TX enqueue wait corresponds to the wait event enq: TX - row lock contention.
The solution is to have the first session already holding the lock perform a COMMIT or ROLLBACK.
Waits for TX in mode 4 is also possible if the session is waiting due to shared bitmap index fragment. Bitmap indexes index key values and a range of ROWIDs. Each 'entry' in a bitmap index can cover many rows in the actual table. If two sessions want to update rows covered by the same bitmap index fragment, then the second session waits for the first transaction to either COMMIT or ROLLBACK by waiting for the TX lock in mode 4. This type of TX enqueue wait corresponds to the wait event enq: TX - row lock contention.
Waits for TX in Mode 4 can also occur waiting for a PREPARED transaction.
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.

10.3.7 enqueue (enq:) waits

Enqueues are locks that coordinate access to database resources. This event indicates that the session is waiting for a lock that is held by another session.

The name of the enqueue is included as part of the wait event name, in the form enq: enqueue_type - related_details. In some cases, the same enqueue type can be held for different purposes, such as the following related TX types:

enq: TX - allocate ITL entry

enq: TX - contention

enq: TX - index contention

enq: TX - row lock contention

The V$EVENT_NAME view provides a complete list of all the enq: wait events.

You can check the following V$SESSION_WAIT parameter columns for additional information:

P1 - Lock TYPE (or name) and MODE

P2 - Resource identifier ID1 for the lock

P3 - Resource identifier ID2 for the lock

    通过awr报告查看:
    SQL> @?/rdbms/admin/awrrpt.sql
    
    • 1

    1.这2个小时进行AWR的收集和分析,首先,从报告头中看到DB Time达到2176分钟,(DB Time)/Elapsed=18,这个比值偏高:
    在这里插入图片描述
    2.再看TOP 10事件:
    看到排在第一位的是enq: TX - row lock contention事件,也就是说系统中在这一个小时里产生了较为严重的行级锁等待事件。
    在这里插入图片描述
    3. 同时,从段的统计信息章节中,也看到下面的信息:
    在这里插入图片描述
    看到row lock waits发生在表上。

    通过命令查看

    那么,究竟是什么操作导致了这个enq: TX - row lock contention等待事件呢? 查看系统中,当前有哪些会话产生了enq: TX - row lock contention等待事件?

    现在已经解锁了,所以无法查询

    SQL> select event,sid,p1,p2,p3 from v$session_wait where event='enq: TX - row lock contention';
    

    未选定行

      如果正在锁着,可以参考enq: TX - row lock contention等待事件

      查看系统中的当前会话,是在哪个对象上产生了enq: TX - row lock contention等待事件?

      • 查看引起enq: TX - row lock contention等待事件的object_id对象号
      SQL> select ROW_WAIT_OBJ#,ROW_WAIT_FILE#,ROW_WAIT_BLOCK#,ROW_WAIT_ROW#  from v$session  where event='enq: TX - row lock contention';
      

        那么这个数据库对象为ROW_WAIT_OBJ#的对象究竟是什么呢?

        • 查看ROW_WAIT_OBJ#对应的对象名称
        SQL> select  object_name,object_idfrom  dba_objects  where object_id=【ROW_WAIT_OBJ#】;
        
          • 通过对象名称查看对象的owner及类型
          SQL> select OWNER,OBJECT_NAME,OBJECT_ID,DATA_OBJECT_ID, OBJECT_TYPEfrom dba_objectswhereobject_name='【OBJECT_NAME】';
          

            定位到的结果应该同AWR报告中段统计信息吻合。

            通过查询gv$session找到当前的等待事件

            SQL> select event,count(*) from gv$session group by event;
            

            EVENT COUNT(*)


            SQLNet message from client 3205
            wait for unread message on broadcast channel 5
            Streams AQ: waiting for messages in the queue 4
            ASM background timer 2
            ges remote message 2
            gcs remote message 8
            LNS ASYNC end of log 2
            pmon timer 2
            rdbms ipc message 60
            jobq slave wait 4
            smon timer 2
            gc cr request 1
            Streams AQ: qmn slave idle wait 2
            class slave wait 12
            SQL
            Net message to client 1
            Space Manager: slave idle wait 9
            GCR sleep 2
            VKTM Logical Idle Wait 2
            Streams AQ: waiting for time management or cleanup tasks 2
            Streams AQ: qmn coordinator idle wait 2
            PX Deq: Execution Msg 1
            PX Deq Credit: send blkd 1
            DIAG idle wait 4
            PING 2
            PX Deq: Execute Reply 1

            已选择25行。

              因为当前已经没有这个等待事件了,可以查看GV_$ACTIVE_SESSION_HISTORY

              SQL> select SAMPLE_TIME,SESSION_ID,USER_ID,SQL_ID,EVENT,CURRENT_OBJ#,CURRENT_FILE#,CURRENT_BLOCK#  from GV_$ACTIVE_SESSION_HISTORY
              where event like 'enq: TX%' and  SAMPLE_TIME like '30-6月%' and module='JDBC Thin Client' and rownum<=500;
              
              • 1
              • 2

              在这里插入图片描述
              结果发现很多的enq: TX - row lock contention等待事件。

              定位具体SQL:

              • 通过sql_ID字段,查询
              SQL> select INST_ID,SQL_TEXT from GV_$SQL where sql_id='f9kdn3mdv252a';
              

              SQL> select * from dba_hist_sqltext where sql_id='f9kdn3mdv252a';

                • 查看是哪个用户的SQL
                  GV_$ACTIVE_SESSION_HISTORY中还有一个USER_ID=54
                SQL> select USERNAME,USER_ID,CREATED from dba_users where USER_ID='54' ;
                
                  • 查看到底是那个表出现了锁等待
                  SQL> select * from dba_objects where object_id='183598';
                  OWNER  OBJECT_NAME              SUBOBJECT_NAME    OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE  CREATED                 LAST_DDL_TIME          TIMESTAMP           STATUS  T G S  NAMESPACE EDITION_NAME
                  ------------------------------ -----------------  --------- -------------- ------------ ----------------------- ---------------------- ------------------- ------- - - - ---------- -----------
                  XX     NODETITLEREPORT_TASK_PRJ                   183598    183598         TABLE        25-4月 -2014 12:23:53   30-6月 -2020 15:42:53   2014-04-25:12:23:53 VALID   N N N            1
                  

                    解决办法

                    如果正在等待
                    1.通过v$session找到BLOCK=1的用户,告知用户提交事务

                    SQL> select SID,TYPE,ID1,ID2,LMODE,REQUEST,CTIME,BLOCK from V$lock where block=1 or request<>0;
                    

                      2.通过sid找到pid,kill掉该进程

                      3.更改sql语句,

                      SQL> SELECT * FROM QRTZ_LOCKS WHERE LOCK_NAME = :1 FOR UPDATE no wait
                      

                        加nowait的意思是得到或者得不到,不会等待

                        一般如果是现网中出现了大量类似的问题,排除人为原因,那么就要检查应用了。

                        参考:
                        初次遇见等待事件:enq;tx-row lock contention

                        enq: TX - row lock contention 等待事件

                        </article>
                        

                        与[转帖]等待事件 enq:TX - row lock contention分析与解决相似的内容:

                        [转帖]等待事件 enq:TX - row lock contention分析与解决

                        6月30日,数据库发生了大量锁表。大概持续1小时,并且越锁越多。后来通过业务人员停掉程序,并kill掉会话后解决。 几天后再EM上查看CPU占用: CPU发生了明显等待。 主要是由于enq:TX - row lock contention等待事件造成。 等待事件—enq:TX - row lock

                        [转帖]ORACLE等待事件:enq: TX - row lock contention

                        https://www.cnblogs.com/kerrycode/p/5887150.html enq: TX - row lock contention等待事件,这个是数据库里面一个比较常见的等待事件。enq是enqueue的缩写,它是一种保护共享资源的锁定机制,一个排队机制,先进先出(FIFO

                        [转帖]ORACLE等待事件:enq: TX - row lock contention

                        https://www.cnblogs.com/kerrycode/p/5887150.html enq: TX - row lock contention等待事件,这个是数据库里面一个比较常见的等待事件。enq是enqueue的缩写,它是一种保护共享资源的锁定机制,一个排队机制,先进先出(FIFO

                        [转帖]模拟enq: TX - row lock contention争用

                        https://www.modb.pro/db/623036 enq: TX - row lock contention它表示一个事务正在等待另一个事务释放被锁定的行。这种等待事件通常发生在并发访问数据库时,多个事务试图同时修改同一行数据时会发生行级锁争用。 以下是可能导致 “enq: TX - r

                        [转帖]事务上的等待事件 —— enq: TX - contention

                        TX锁是保护事务的,事务结束时便会释放。因此,为获得TX锁为等待的会话,要等到拥有锁的会话的事务结束为止。 SQL> select name,parameter1,parameter2,parameter3 from v$event_name where name like '%enq: TX%';

                        [转帖]事务上的等待事件 —— enq: TM - contention

                        执行DML期间,为防止对与DML相关的对象进行修改,执行DML的进程必须对该表获得TM锁。若在获得TM锁的过程中发生争用,则等待enq: HW - contention 事件。 SQL> select name,parameter1,parameter2,parameter3 from v$even

                        [转帖]oracle 11.2.0.4 rac集群等待事件enq: TM - contention

                        近期,一金融客户oracle 11.2.0.4 rac集群delete不当导致等待事件enq: TM - contention严重引起大范围会话堆积,记录的相关分析工作如下。 1、登录集群任意节点,查看集群全局等待事件 SQL> select event,count(*) from gv$sessi

                        [转帖]global cache cr request等待事件分析及优化

                        在RAC环境中,和全局调整缓存相关的最常见的等待事件无非就是:global cache cr request,global cache busy和equeue 在XX电信做了一次数据库巡检中发现,spreport中的top 5 wait events中出现了global cache cr reque

                        [转帖]db file sequential read-数据文件顺序读取

                        https://www.cnblogs.com/xibuhaohao/p/9959593.html 等待事件: "db file sequential read" Reference Note (文档 ID 34559.1) 这种等待事件是一种IO读请求相关的等待。与”db file scatter

                        [转帖]3.3.6. 活跃会话历史报告SYS_KSH

                        https://help.kingbase.com.cn/v8/perfor/performance-optimization/performance-optimization-6.html#sys-ksh sys_stat_activity 里记录的等待事件是瞬时信息,没有对等待事件的时间进行累计