如何找出長時間未提交的事務session ID

收到報警某臺mysql數據庫慢查詢數量超過5,登陸上去看,發現阻塞的SQL所有是update,處於Updating狀態mysql

+---------+------+-----------+------+---------+------+----------+------------------------------------+
| ID      | USER | HOST      | DB   | COMMAND | TIME | STATE    | INFO                               |
+---------+------+-----------+------+---------+------+----------+------------------------------------+
| 4364109 | root | localhost | test | Query   |   39 | Updating | update T_1 set col3='' where FID=3 |
+---------+------+-----------+------+---------+------+----------+------------------------------------+

查看系統狀態,各項都正常,但在InnoDB Row Lock Time值上有個較大的毛刺,基本得出是鎖等待致使的。show processlist看到的線程裏面,活動狀態的沒有發現執行慢的SQL,都是經過主鍵update,極有多是某個事務對這個表作了dml操做沒有提交commit,致使一直持有鎖未釋放,後面的session等待X鎖。根據這個思路,在show engine innodb status的事務欄裏面找到ACTIVE狀態的,執行時間最長的那個session,發現是sleep狀態的,kill掉這個session,阻塞現象消失。sql

---TRANSACTION 21427F91, ACTIVE 1421 sec, process no 69177, OS thread id 139633152562944
2 lock struct(s), heap size 376, 1 row lock(s), undo log entries 1
MySQL thread id 4364618, query id 282683146 localhost root

至於爲何會出現未提交SQL,再和開發一塊兒去排查數據庫

經過show engine innodb status去查session比較麻煩,簡單的SQL以下session

mysql> select a.trx_mysql_thread_id,b.TIME from information_schema.INNODB_TRX a inner join information_schema.processlist b on a.trx_mysql_thread_id=b.id where a.trx_state='RUNNING' and b.time>10 and b.COMMAND='Sleep';
+---------------------+------+
| trx_mysql_thread_id | TIME |
+---------------------+------+
|             4364618 |  614 |
+---------------------+------+
1 row in set (0.00 sec)

事務ID=4364618,事務時長:614秒。spa

相關文章
相關標籤/搜索