Wednesday, April 27, 2016

Finding Blocking in Mysql

I’m primarily a SQL Server guy, but I’ve been lucky enough to have a handful of MYSQL databases to manage, and outside of our NDB cluster, they’ve been designed very simple and therefore stable, so my interaction is limited.  As the company has grown we’ve acquired more companies based in MYSQL and although my team doesn’t manage these directly, we’re often brought in to help during crises modes.  Of course it’s not optimal to dust off your skillset during a crises mode, but a b-tree is a b-tree, and a nested loop, scan, and other fundamentals of a RMDB don't change from one system to another… mostly.  What is different are the tools at getting at the data.  In my latest experience I came across a DB riddled with locking, which of course was leading to blocking.  The problem was trying to find what calls were blocking what, and causing the issue.  Lucky the innodb engine has some great real-time tables for accessing intervals.  Below is a query I wrote to get a view of the blocking going on real-time.  I’m sure I’m not the first person to do something similar, but I just couldn’t find it. 



select T.trx_state as Blocking_trx_state,
T.trx_started as Blocking_trx_started,
T.trx_requested_lock_id as Blocking_trx_requested_lock_id,
T.trx_wait_started as Blocking_trx_wait_started,
T.trx_weight as Blocking_trx_weight,
T.trx_mysql_thread_id as Blocking_trx_mysql_thread_id,
T.trx_query as Blocking_trx_query,
T.trx_operation_state as Blocking_trx_operation_state,
T.trx_tables_in_use as Blocking_trx_tables_in_use,
T.trx_tables_locked as Blocking_trx_tables_locked,
T.trx_lock_structs as Blocking_trx_lock_structs,
T.trx_lock_memory_bytes as Blocking_trx_lock_memory_bytes,
T.trx_rows_locked as Blocking_trx_rows_locked,
T.trx_rows_modified as Blocking_trx_rows_modified,
T.trx_concurrency_tickets as Blocking_trx_concurrency_tickets,
T.trx_isolation_level as Blocking_trx_isolation_level,
T.trx_unique_checks as Blocking_trx_unique_checks,
T.trx_foreign_key_checks as Blocking_trx_foreign_key_checks,
T.trx_last_foreign_key_error as Blocking_trx_last_foreign_key_error,
T.trx_adaptive_hash_latched as Blocking_trx_adaptive_hash_latched,
T.trx_adaptive_hash_timeout as Blocking_trx_adaptive_hash_timeout,
T.trx_id as Blocking_trx_id,
Ta.trx_state as Blocked_trx_state,
Ta.trx_started as Blocked_trx_started,
Ta.trx_requested_lock_id as Blocked_trx_requested_lock_id,
Ta.trx_wait_started as Blocked_trx_wait_started,
Ta.trx_weight as Blocked_trx_weight,
Ta.trx_mysql_thread_id as Blocked_trx_mysql_thread_id,
Ta.trx_query as Blocked_trx_query,
Ta.trx_operation_state as Blocked_trx_operation_state,
Ta.trx_tables_in_use as Blocked_trx_tables_in_use,
Ta.trx_tables_locked as Blocked_trx_tables_locked,
Ta.trx_lock_structs as Blocked_trx_lock_structs,
Ta.trx_lock_memory_bytes as Blocked_trx_lock_memory_bytes,
Ta.trx_rows_locked as Blocked_trx_rows_locked,
Ta.trx_rows_modified as Blocked_trx_rows_modified,
Ta.trx_concurrency_tickets as Blocked_trx_concurrency_tickets,
Ta.trx_isolation_level as Blocked_trx_isolation_level,
Ta.trx_unique_checks as Blocked_trx_unique_checks,
Ta.trx_foreign_key_checks as Blocked_trx_foreign_key_checks,
Ta.trx_last_foreign_key_error as Blocked_trx_last_foreign_key_error,
Ta.trx_adaptive_hash_latched as Blocked_trx_adaptive_hash_latched,
Ta.trx_adaptive_hash_timeout as Blocked_trx_adaptive_hash_timeout,
Ta.trx_id as Blocked_trx_id               
from  Information_Schema.INNODB_LOCK_WAITS W INNER JOIN   
Information_Schema.INNODB_TRX T ON T.trx_id = W.blocking_trx_id 
inner join Information_Schema.INNODB_TRX Ta on Ta.trx_id = W.requesting_trx_id \G

No comments: