SELECT
"HOLD:",
ph.id h_processid,
trh.trx_id h_trx_id,
trh.trx_started h_started,
trh.trx_state h_state,
lsh.lock_table h_table,
lsh.lock_index h_index,
lsh.lock_mode AS h_lock_mode,
lsh.lock_type h_lock_type,
ph.USER h_user,
ph.HOST h_host,
ph.command p_command,
ph.info p_info,
"WAIT:",
pw.id w_processid,
trw.trx_id w_trx_id,
trw.trx_started w_started,
trw.trx_state w_state,
lsw.lock_table w_table,
lsw.lock_index w_index,
lsw.lock_mode w_lock_mode,
lsw.lock_type w_lock_type,
pw.USER w_user,
pw.HOST w_host,
pw.command w_command,
pw.info w_info
FROM
information_schema.INNODB_LOCK_WAITS lw
INNER JOIN information_schema.INNODB_TRX trh ON trh.trx_id = lw.blocking_trx_id
INNER JOIN information_schema.PROCESSLIST ph ON trh.trx_mysql_thread_id = ph.id
LEFT JOIN information_schema.INNODB_LOCKS lsh ON lsh.lock_trx_id = trh.trx_id
INNER JOIN information_schema.INNODB_TRX trw ON trw.trx_id = lw.requesting_trx_id
INNER JOIN information_schema.PROCESSLIST pw ON trw.trx_mysql_thread_id = pw.id
LEFT JOIN information_schema.INNODB_LOCKS lsw ON lsw.lock_trx_id = trw.trx_id;
select
a.trx_mysql_thread_id as `spid`,
d.trx_mysql_thread_id as `blocked`,
a.trx_state,
a.TRX_ROWS_LOCKED,
a.TRX_ROWS_MODIFIED,
c.lock_id,
c.lock_table,
c.lock_data,
c.lock_mode,
c.lock_type,
c.lock_index,
a.trx_operation_state,
ifnull(a.trx_query, g.SQL_TEXT) as trx_query,
ifnull(d.trx_query, n.SQL_TEXT) as trx_query_block,
e.`user`,
e.host,
e.DB,
e.COMMAND,
e.`time`,
e.state,
a.trx_wait_started,
a.trx_started
from information_schema.INNODB_TRX a
left join information_schema.INNODB_LOCK_WAITS b
on a.trx_id = b.requesting_trx_id
left join information_schema.INNODB_LOCKS c
on a.trx_id = c.lock_trx_id
left join information_schema.INNODB_TRX d
on b.blocking_trx_id = d.trx_id
left join information_schema.processlist e
on a.trx_mysql_thread_id = e.id
left join performance_schema.threads f
on f.PROCESSLIST_ID = a.trx_mysql_thread_id
left join performance_schema.events_statements_current g
on f.THREAD_ID = g.THREAD_ID
left join performance_schema.threads m
on m.PROCESSLIST_ID = d.trx_mysql_thread_id
left join performance_schema.events_statements_current n
on m.THREAD_ID = n.THREAD_ID
;
SELECT
p2.`HOST` 被阻塞方host,
p2.`USER` 被阻塞方用户,
r.trx_id 被阻塞方事务id,
r.trx_mysql_thread_id 被阻塞方线程号,
TIMESTAMPDIFF(
SECOND,
r.trx_wait_started,
CURRENT_TIMESTAMP
) 等待时间,
r.trx_query 被阻塞的查询,
l.lock_table 阻塞方锁住的表,
m.`lock_mode` 被阻塞方的锁模式,
m.`lock_type` "被阻塞方的锁类型(表锁还是行锁)",
m.`lock_index` 被阻塞方锁住的索引,
m.`lock_space` 被阻塞方锁对象的space_id,
m.lock_page 被阻塞方事务锁定页的数量,
m.lock_rec 被阻塞方事务锁定行的数量,
m.lock_data 被阻塞方事务锁定记录的主键值,
p.`HOST` 阻塞方主机,
p.`USER` 阻塞方用户,
b.trx_id 阻塞方事务id,
b.trx_mysql_thread_id 阻塞方线程号,
b.trx_query 阻塞方查询,
l.`lock_mode` 阻塞方的锁模式,
l.`lock_type` "阻塞方的锁类型(表锁还是行锁)",
l.`lock_index` 阻塞方锁住的索引,
l.`lock_space` 阻塞方锁对象的space_id,
l.lock_page 阻塞方事务锁定页的数量,
l.lock_rec 阻塞方事务锁定行的数量,
l.lock_data 阻塞方事务锁定记录的主键值,
IF (p.COMMAND = 'Sleep', CONCAT(p.TIME,' 秒'), 0) 阻塞方事务空闲的时间
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 l ON w.blocking_lock_id = l.lock_id AND l.`lock_trx_id`=b.`trx_id`
INNER JOIN information_schema.INNODB_LOCKS m ON m.`lock_id`=w.`requested_lock_id` AND m.`lock_trx_id`=r.`trx_id`
INNER JOIN information_schema. PROCESSLIST p ON p.ID = b.trx_mysql_thread_id
INNER JOIN information_schema. PROCESSLIST p2 ON p2.ID = r.trx_mysql_thread_id
ORDER BY
等待时间 DESC;