Kill Long Running and Locked SQL Statements in MySQL
Kill Long Running Queries
Query information_schema.innodb_trx
- trx_mysql_thread_id: thread ID
SELECT trx_mysql_thread_id, trx_state, trx_started, trx_query |
After killed the query thread. The client receive a error message 2013 - Lost connection to server during query
.
Query information_schema.processlist
- id: thread ID
- time: cost time in seconds
- state: Sending Data, executing
SELECT * |
Kill Locked SQL Statements
An Example of Locked SQL Statements
Create a table for test
CREATE TABLE `t_user` ( |
Executing the SQL statement 1 to lock the table
SET autocommit = 0; |
Executing the SQL statement 2, which will wait for the lock.
-- Temporarily set the lock wait timeout to 10 minutes. By default, it is 50 seconds. We need a longer timeout to find out the locked SQL statements. |
If waiting for lock is timeout (by default, it is 50 seconds), SQL statement 2 will receive a error message
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction |
After finished the lock test, you can COMMIT or ROLLBACK the transaction of SQL statement 1 and set autocommit
to 1.
COMMIT; |
Get thread IDs and SQL statements of lock-holding and lock-waiting executing SQLs
Query whether some SQL statement threads are waiting for lock
SELECT * |
- trx_state: LOCK WAIT
- trx_started: 2022-10-21 14:13:38
- trx_mysql_thread_id: 17 (thread ID)
- trx_query: the executing SQL statement
- trx_requested_lock_id: 1207867061312:1021:4:2:1207832760632
- trx_wait_started: 2022-10-21 14:13:38
Query whether some SQL statement treads are running with lock
SELECT * |
- trx_state: RUNNING
- trx_started: 2022-10-21 14:09:57
- trx_mysql_thread_id: 16 (thread ID)
- trx_query: the executing SQL statement
- trx_tables_locked: 1
- trx_lock_structs: 2
- trx_rows_locked: 3
- trx_row_modified: 2
Get More Locked Information
Query what table is locked
show open tables where in_use > 0; |
Query Lock Information
Get transaction IDs and real Thread IDs of lock-holding and lock-waiting executing SQL.
SHOW ENGINE INNODB STATUS; |
To find “TRANSACTION xxx, ACTIVE xxx sec” in the result text
lock-holding transaction Information
---TRANSACTION 580438, ACTIVE 1862 sec |
lock-waiting transaction Information
---TRANSACTION 580444, ACTIVE 36 sec starting index read |
There are only one lock-holding transaction and one lock-waiting transaction. So we can guess that thread 16 block thread 17, or that transaction 580438 block transaction 580444.
Check lock dependency - what blocks what
MySQL 5.x
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS; |
MySQL 8
Query lock dependency
SELECT * FROM performance_schema.data_lock_waits; |
BLOCKING_ENGINE_TRANSACTION_ID
: lock-holding transaction IDREQUESTING_ENGINE_TRANSACTION_ID
: lock-waiting transaction ID
The result is BLOCKING_ENGINE_TRANSACTION_ID
blocked REQUESTING_ENGINE_TRANSACTION_ID
. We can confirm that transaction 580438 blocked transaction 580444. You can get the real thread IDs from the result of SHOW ENGINE INNODB STATUS;
. Therefore, we can confirm that thread 16 blocked thread 17.
Query lock-holding and lock-waiting transaction information
SELECT * FROM performance_schema.data_locks; |
- ENGINE_TRANSATION_ID: transation_id in
SHOW ENGINE INNODB STATUS;
- OBJECT_NAME: table name
- LOCK_STATUS: “WATING”/“GRANT”
Kill the Locked Tread
kill {thread_ID}; |