mysql 鎖超時定位與分析

mysql 鎖超時調查方法

此SQL可以顯示鎖等待的詳細信息,包括阻塞SQL,被阻塞SQL及阻塞時間等。

select r.trx_id as waiting_trx_id, r.trx_mysql_thread_id as waiting_thread, TIMESTAMPDIFF(SECOND, r.trx_wait_started, CURRENT_TIMESTAMP) as wait_time, r.trx_query as waiting_query, l.lock_table as waiting_table_lock, b.trx_id as blocking_trx_id, b.trx_mysql_thread_id as blocking_thread, SUBSTRING(p.host, 1, INSTR(p.host, ‘:’) -1) as blocking_host, SUBSTRING(p.host, INSTR(p.host, ‘:’)+1) as blocking_port, IF(p.command =”Sleep”, p.time,0) as idle_in_trx, b.trx_query as blocking_query 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 as l on w.requested_lock_id=l.lock_id left join information_schema.PROCESSLIST p on p.id=b.trx_mysql_thread_id order by wait_time desc limit 1\G
*************************** 1. row ***************************
waiting_trx_id: 2AA9FED000
waiting_thread: 187
wait_time: 50
waiting_query: INSERT IGNORE INTO workingSeedTrackTask ( task_priority, task_status, retry_count, created_at , source_count , trackingProtocol_id, seed_id ) ( SELECT ‘low’, ‘created’, 0, NOW(), source_count, trackingProtocol_id, id FROM seed WHERE trackingProtocol_id = 1 AND id = 5249101 )
waiting_table_lock: `hubble`.`workingSeedTrackTask`
blocking_trx_id: 2AA9FE7A59
blocking_thread: 179087834
blocking_host: 206.99.94.72
blocking_port: 37525
idle_in_trx: 0

blocking_query: insert into workingSeedTrackTask(seed_id, source_count, trackingProtocol_id, task_priority, task_status, created_at) select d.id, d.source_count, d.trackingProtocol_id, ‘urgent’, ‘created’, now() from verifySeed a, seed_trackingMetaFinished b, mddb.meta c, seed d where a.seed_id = b.seed_id and a.seed_id > 7183277 and b.seed_id > 7183277 and b.verify_status = ‘match’ and c.company_id in(34,1369) and a.is_tracking = ‘true’ and b.trackingMeta_id = c.id and a.seed_id = d.id ON DUPLICATE KEY UPDATE task_priority = VALUES(task_priority)

此SQL可以顯示有多少查詢被哪些線程阻塞,被阻塞最長時間及被阻塞線程數。

select concat(‘thread ‘, b.trx_mysql_thread_id, ‘ from ‘, p.host) as who_blocks, if(p.command=”Sleep”, p.time,0) as idle_in_trx, max(TIMESTAMPDIFF(SECOND, r.trx_wait_started, now())) as max_wait_time, count(*) as num_waiters 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 left join information_schema.PROCESSLIST p on p.id=b.trx_mysql_thread_id group by who_blocks order by num_waiters desc;
+——————————————————————+————-+—————+————-+
| who_blocks | idle_in_trx | max_wait_time | num_waiters |
+——————————————————————+————-+—————+————-+
| thread 179084010 from 206.99.94.72:37276 | 0 | 39 | 13 |
| thread 172 from ip-10-171-54-59.us-west-1.compute.internal:58220 | 0 | 35 | 2 |
| thread 136 from ip-10-171-54-59.us-west-1.compute.internal:58135 | 0 | 5 | 1 |
| thread 93 from ip-10-171-54-59.us-west-1.compute.internal:58083 | 0 | 30 | 1 |
| thread 179084008 from 206.99.94.72:37274 | 0 | 39 | 1 |
+——————————————————————+————-+—————+————-+

發佈了25 篇原創文章 · 獲贊 4 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章