// SQL snippet

Summarize current lock waits in MySQL

Who is waiting, on which table and index, blocked by whom and for how long: one sys schema view answers all of it.

Main statement
MySQL

The task

In MySQL 8.0 and later, sys.innodb_lock_waits joins performance_schema.data_lock_waits and data_locks with information_schema.INNODB_TRX into one report: each waiting InnoDB transaction, the table and index it is stuck on, the lock modes on both sides, the blocking transaction, both statements, and the processlist ids you would pass to KILL.

The snippet

Written for MySQL.

MySQL
SELECT wait_age_secs,
       locked_table, locked_index, locked_type,
       waiting_pid, waiting_lock_mode, waiting_query,
       blocking_pid, blocking_lock_mode, blocking_query, blocking_trx_started,
       sql_kill_blocking_query, sql_kill_blocking_connection
FROM sys.innodb_lock_waits
ORDER BY wait_age_secs DESC;

工作原理

Each row is one waiting lock request paired with a lock that blocks it. wait_age_secs is how long it has waited. locked_table and locked_index say where the contention is; locked_index = PRIMARY means rows of the clustered index. The lock modes show the conflict, for example X,REC_NOT_GAP waiting on X,REC_NOT_GAP for two updates of the same row, or a GAP lock where an insert hits another transaction's range lock. blocking_trx_started shows whether the blocker is an old transaction. The last two columns are ready-made KILL QUERY and KILL statements; KILL QUERY only stops the current statement, while KILL ends the connection and rolls back its whole transaction. innodb_lock_wait_timeout (50 seconds by default) ends the waiter with an error if nobody acts.

Variations

Raw data_lock_waits pairs without the sys schema

SELECT w.REQUESTING_ENGINE_TRANSACTION_ID AS waiting_trx,
       w.BLOCKING_ENGINE_TRANSACTION_ID AS blocking_trx,
       wt.PROCESSLIST_ID AS waiting_pid, bt.PROCESSLIST_ID AS blocking_pid,
       r.OBJECT_SCHEMA, r.OBJECT_NAME AS locked_table, r.INDEX_NAME AS locked_index,
       r.LOCK_MODE AS waiting_mode, b.LOCK_MODE AS blocking_mode
FROM performance_schema.data_lock_waits w
JOIN performance_schema.data_locks r ON r.ENGINE_LOCK_ID = w.REQUESTING_ENGINE_LOCK_ID
JOIN performance_schema.data_locks b ON b.ENGINE_LOCK_ID = w.BLOCKING_ENGINE_LOCK_ID
JOIN performance_schema.threads wt ON wt.THREAD_ID = w.REQUESTING_THREAD_ID
JOIN performance_schema.threads bt ON bt.THREAD_ID = w.BLOCKING_THREAD_ID
ORDER BY waiting_trx;

Questions that come up

A blocker shows no query. What does that mean?

blocking_query is NULL when the blocking connection is idle: its transaction is open but it is not running a statement. That is usually an application that did some writes and never committed, or a person who ran BEGIN in a client and walked away. blocking_trx_started gives its age. To see what it ran, look up its thread in performance_schema.events_statements_history, which keeps the last few statements per thread by default.

How do I reduce lock waits overall?

Commit sooner, and never wait on a user or a remote call inside a transaction. Update rows in a consistent order, such as by primary key, so transactions queue instead of deadlocking. Index the columns in UPDATE and DELETE WHERE clauses: InnoDB locks every index record it scans, so an unindexed UPDATE can lock most of the table. Under REPEATABLE READ (the default), range conditions also take gap locks that block inserts; READ COMMITTED takes far fewer of them, if the application can live with its semantics.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.