Lock Monitoring Scripts | Database Journal

Lock Monitoring Scripts

Mar 7, 2017
1 minute read

>>Script Language and Platform: SQL / MySQL
This sequence of scripts can help identify tables that are being blocked and quantify how many locks are happening in your database.

Author: Carlos Victor

	-- Who is blocking (BLOCKING_TRX_ID | BLOCKING_LOCK_ID)

SELECT  TRX.TRX_MYSQL_THREAD_ID,
 TRX.TRX_ISOLATION_LEVEL,
 TRX.TRX_STARTED,
 TRX.TRX_STATE,
 PROCESSLIST.USER,
 PROCESSLIST.HOST,
 PROCESSLIST.DB,
 PROCESSLIST.COMMAND,
 PROCESSLIST.TIME,
 PROCESSLIST.STATE,
 LOCK_WAITS.REQUESTING_TRX_ID,
 LOCK_WAITS.BLOCKING_TRX_ID,
 LOCK_WAITS.BLOCKING_LOCK_ID
FROM INFORMATION_SCHEMA.INNODB_LOCKS LOCKS,
     INFORMATION_SCHEMA.INNODB_TRX TRX,
     INFORMATION_SCHEMA.PROCESSLIST PROCESSLIST,
     INFORMATION_SCHEMA.INNODB_LOCK_WAITS LOCK_WAITS WHERE LOCKS.LOCK_TRX_ID = TRX.TRX_ID
  AND TRX.TRX_MYSQL_THREAD_ID = PROCESSLIST.ID;

-- Query #2

SELECT r.trx_id waiting_trx_id, r.trx_mysql_thread_id waiting_thread, r.trx_query waiting_query, b.trx_id blocking_trx_id, b.trx_mysql_thread_id blocking_thread, b.trx_query blocking_query, bl.lock_id blocking_lock_id, bl.lock_mode blocking_lock_mode, bl.lock_type blocking_lock_type, bl.lock_table blocking_lock_table, bl.lock_index blocking_lock_index, rl.lock_id waiting_lock_id, rl.lock_mode waiting_lock_mode, rl.lock_type waiting_lock_type, rl.lock_table waiting_lock_table, rl.lock_index waiting_lock_index FROM information_schema.INNODB_LOCK_WAITS w INNER JOIN information_schema.INNODB_TRX b ON b.trx_id = w.blocking_trx_id INNER JOIN information_schema.INNODB_TRX r ON r.trx_id = w.requesting_trx_id INNER JOIN information_schema.INNODB_LOCKS bl ON bl.lock_id = w.blocking_lock_id INNER JOIN information_schema.INNODB_LOCKS rl ON rl.lock_id = w.requested_lock_id; 

-- Query #3

SELECT blocking_trx_id,COUNT(*), b.trx_query blocking_query, MIN(r.trx_wait_started) FROM information_schema.INNODB_LOCK_WAITS w INNER JOIN information_schema.INNODB_TRX b ON b.trx_id = w.blocking_trx_id INNER JOIN information_schema.INNODB_TRX r ON r.trx_id = w.requesting_trx_id INNER JOIN information_schema.INNODB_LOCKS bl ON bl.lock_id = w.blocking_lock_id INNER JOIN information_schema.INNODB_LOCKS rl ON rl.lock_id = w.requested_lock_id GROUP BY blocking_trx_id ORDER BY COUNT(*) DESC;


-- A list of blocking transactions

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS WHERE LOCK_TRX_ID IN (SELECT BLOCKING_TRX_ID FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS);

OR

SELECT INNODB_LOCKS.*
FROM INFORMATION_SCHEMA.INNODB_LOCKS
JOIN INFORMATION_SCHEMA.INNODB_LOCK_WAITS
  ON (INNODB_LOCKS.LOCK_TRX_ID = INNODB_LOCK_WAITS.BLOCKING_TRX_ID);


Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided “as -is”, without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose… Disclaimer Continued

Back to Database Journal Home

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.