博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

查看引起锁的源头

Posted on 2017-08-10 15:07  moss_tan_jun  阅读(252)  评论(0编辑  收藏  举报

SELECT distinct b.trx_id blocking_trx_id,
                b.trx_mysql_thread_id 源头锁thread_id,
                SUBSTRING(p. HOST, 1, INSTR(p. HOST, ':') - 1) blocking_host,
                SUBSTRING(p. HOST, INSTR(p. HOST, ':') + 1) blocking_port,
                IF(p.COMMAND = 'Sleep', p.TIME, 0) idel_in_trx,
                b.trx_query blocking_query,
                r.trx_id waiting_trx_id,
                r.trx_mysql_thread_id waiting_thread,
                TIMESTAMPDIFF(SECOND, r.trx_wait_started, CURRENT_TIMESTAMP) wait_time,
                r.trx_query waiting_query,
                l.lock_table waiting_table_lock
  FROM information_schema.INNODB_LOCKS l
  LEFT JOIN information_schema.INNODB_LOCK_WAITS w
    ON w.requested_lock_id = l.lock_id
  LEFT JOIN information_schema.INNODB_TRX b
    ON b.trx_id = w.blocking_trx_id
  LEFT 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
JOIN (SELECT blocking_trx_id -- 查找最源头的trx_id
      FROM information_schema.INNODB_LOCK_WAITS ilw
     WHERE blocking_trx_id NOT IN
           (SELECT requesting_trx_id
              FROM information_schema.INNODB_LOCK_WAITS)) c
ON c.blocking_trx_id = b.trx_id
ORDER BY wait_time DESC;