mysql8定位被锁语句

  • Post author:
  • Post category:mysql


线上mysql版本为8.0.11,对于最近的几个版本越来越像Orale了,有了较为丰富的系统字典,也可以查看系统的等待事件,现在线上出现被锁语句的情况也可以通过数据字典直接查询出来了,在此记录下以供参考。

首先查看系统锁情况,下面这条语句可以看到持有锁的线程以及线程ID,这里需要注意下,session_id也就是show processlist查询出来的链接ID,在这个表里表现为PID,同时该表也记录了trx_id,最后还给出了解决方案也就是如何kill持有锁的进程。

mysql> SELECT * FROM sys.`innodb_lock_waits` \G
*************************** 1. row ***************************
                wait_started: 2019-01-28 14:27:03
                    wait_age: 00:00:03
               wait_age_secs: 3
                locked_table: `test`.`t`
         locked_table_schema: test
           locked_table_name: t
      locked_table_partition: NULL
   locked_table_subpartition: NULL
                locked_index: GEN_CLUST_INDEX
                 locked_type: RECORD
              waiting_trx_id: 19758723
         waiting_trx_started: 2019-01-28 14:27:03
             waiting_trx_age: 00:00:03
     waiting_trx_rows_locked: 1
   waiting_trx_rows_modified: 0
                 waiting_pid: 4202
               waiting_query: update t set id=8
             waiting_lock_id: 19758723:4998:4:2
           waiting_lock_mode: X
             blocking_trx_id: 19748728
                blocking_pid: 4200
              blocking_query: NULL
            blocking_lock_id: 19748728:4998:4:2
          blocking_lock_mode: X
        blocking_trx_started: 2019-01-28 10:58:27
            blocking_trx_age: 03:28:39
    blocking_trx_rows_locked: 2
  blocking_trx_rows_modified: 1
     sql_kill_blocking_query: KILL QUERY 4200
sql_kill_blocking_connection: KILL 4200
1 row in set (0.02 sec

查看当前被锁的语句

##等待锁的语句
SELECT * FROM performance_schema.events_statements_history WHERE thread_id IN(
SELECT b.`THREAD_ID` FROM sys.`innodb_lock_waits` AS a , performance_schema.threads AS b
WHERE a.waiting_pid = b.`PROCESSLIST_ID`)
ORDER BY timer_start ASC;

查看持有锁的语句可以通过如下方式

##持有锁的语句
SELECT * FROM performance_schema.events_statements_history WHERE thread_id IN(
SELECT b.`THREAD_ID` FROM sys.`innodb_lock_waits` AS a , performance_schema.threads AS b
WHERE a.`blocking_pid` = b.`PROCESSLIST_ID`)
ORDER BY timer_start ASC;

======================================================================================

查看未提交事务的会话状态,并查询未提交的SQL

SELECT 
  p.ID AS conn_id,
  P.USER AS login_user,
  P.HOST AS login_host,
  p.DB AS database_name,
  P.TIME AS trx_sleep_seconds,
  TIME_TO_SEC(TIMEDIFF(NOW(), T.trx_started)) AS trx_open_seconds,
  T.trx_started,
  T.trx_isolation_level,
  T.trx_tables_locked,
  T.trx_rows_locked,
  t.trx_state,
  p.COMMAND AS process_state 
FROM
  `information_schema`.`INNODB_TRX` t 
  INNER JOIN `information_schema`.`PROCESSLIST` p 
    ON t.trx_mysql_thread_id = p.id 
WHERE t.trx_state = 'RUNNING' 
  AND p.COMMAND = 'Sleep' 
  AND P.TIME > 1 
ORDER BY T.trx_started ASC ;


SELECT * FROM performance_schema.events_statements_history WHERE thread_id=(
SELECT processlist_id FROM performance_schema.threads WHERE processlist_id=127);



版权声明:本文为lihm0_1原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。