Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
SQL Scripts & Samples
Tips
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

SQL Scripts & Samples

Posted March 7, 2017

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Lock Monitoring Scripts

>>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



SQL Scripts & Samples Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM