Analyzing Transaction Lock Waits in MySQL


Performance is a pivotal aspect of any database system. When users experience slowdowns or blocks, one common culprit in transactional systems like InnoDB (the default MySQL storage engine) is lock contention. This happens when one transaction holds a lock and another is waiting for it. The provided SQL query aids in identifying and analyzing such waiting transactions in a MySQL environment, specifically targeting scenarios where transactions are in a ‘LOCK WAIT’ state.

Using the SQL for Performance Troubleshooting

SQL Breakdown

The query retrieves various details about transactions from the INFORMATION_SCHEMA.INNODB_TRX table, which provides information about current InnoDB transactions.

  • trx_id: The unique ID of the transaction.
  • trx_state: The current state of the transaction. The query specifically filters out transactions that are in the ‘LOCK WAIT’ state.
  • trx_started: The timestamp when the transaction started.
  • trx_requested_lock_id: ID of the lock the transaction is waiting for.
  • trx_wait_started: Timestamp of when the transaction started waiting for the lock.
  • wait_duration: This calculated column gives the duration (in seconds) for which the transaction has been waiting. It’s the difference between the current time and the trx_wait_started timestamp.
  • trx_rows_locked: Number of rows locked by the transaction.
  • trx_rows_modified: Number of rows modified by the transaction.
  • trx_mysql_thread_id: The MySQL thread ID handling this transaction. Useful to correlate with the MySQL process list.
  • trx_query: The SQL query associated with the transaction, which can be very helpful to understand what the transaction is trying to achieve.

How to Use the Results?

  1. Identify Long Wait Durations: The longer a transaction has been waiting (wait_duration), the more immediate the contention issue. Focus on transactions with long wait times first.
  2. Examine the Queries: Review the trx_query field to understand which SQL operations are causing the contention. This can shed light on potential optimization areas.
  3. Check Rows Locked/Modified: Transactions that have locked or modified a large number of rows can be indicative of broad or unoptimized operations that might be affecting performance.
  4. Correlate with Thread IDs: If you have other monitoring tools or logs available, use trx_mysql_thread_id to correlate information from those sources for a holistic view of what’s happening.
  5. Further Diagnostics: Use trx_requested_lock_id to investigate which other transactions are holding the required locks. You might need to dive deeper into the INFORMATION_SCHEMA tables for this.


Lock waits can be a significant cause of performance degradation in MySQL. By leveraging insights from the INFORMATION_SCHEMA.INNODB_TRX table, administrators can obtain a clear picture of transactions in the ‘LOCK WAIT’ state, helping pinpoint potential problem areas. However, while identifying is the first step, resolving lock contention often involves a mix of optimizing queries, adjusting transaction isolation levels, or even rethinking application logic. Always ensure that any optimization step is tested in a non-production environment before applying it to the live system.

About Shiv Iyer 456 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.