MySQL找出鎖等待

2021-09-06 14:25:34 字數 1986 閱讀 9948

1.伺服器級別的鎖等待

可以通過show processlist看到等待鎖的執行緒id,但是無法知道究竟哪個執行緒持有鎖

可以通過mysqladmin debug

相關等待鎖的執行緒以及誰持有鎖可以在錯誤日誌中找到

2.儲存引擎層的鎖等待則比較麻煩,以下是innodb儲存引擎中鎖等待以及哪個執行緒持有鎖的查詢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 block_port, if(p.command="sleep",p.time,0) as idle_in_trx, b.trx_query as blcoking_query from information_schema.innodb_lock_waits as w inner

join information_schema.innodb_trx as b on b.trx_id=w.blocking_trx_id inner

join information_schema.innodb_trx as 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 as p on p.id = b.trx_mysql_thread_id order

by wait_time desc\g

3.如果因為執行緒在乙個事務中空閒而正在遭受大量的鎖操作,下面查詢顯示儲存引擎層有多少查詢被哪些執行緒阻塞。

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 as w inner

join information_schema.innodb_trx as b on b.trx_id = w.blocking_trx_id inner

join information_schema.innodb_trx as r on r.trx_id = w.requesting_trx_id left

join information_schema.processlist as p on p.id = b.trx_mysql_thread_id group

by who_blocks order

by num_waiters desc\g

參考資料

《高效能mysql第三版》

mysql 鎖等待表 MySQL表的鎖等待

今天線上業務遇到乙個問題,因為一張模擬自增序列的表被鎖住,涉及該錶的業務受到影響。線上情況 1 這個表只有乙個id欄位。今天線上業務遇到乙個問題,因為一張模擬自增序列的表被鎖住,涉及該錶的業務受到影響。線上情況 1 這個表只有乙個id欄位。2 id欄位為主鍵索引 3 該錶只有一行資料,記錄全域性最大...

mysql行鎖等待分析

程式通過update xx set where id in a,b,c 一次性鎖大量id,其中某些id被其他session鎖住了。當超過innodb lock wait timeout會try restarting transaction 早上來到公司檢查公司郵件,發現報錯郵件了。怎麼可以忽視,當然...

MySql鎖等待問題的處理辦法

建表 create table testdemo id int 255 notnull c1 varchar 300 character set utf8 collate utf8 general ci null default null c2 int 50 null default null pr...