首页 > 代码库 > MySQL锁等待分析【2】

MySQL锁等待分析【2】

MySQL锁等待分析【1】中对锁等待的分析是一步一步来的、虽然最后是分析出来了,可是用时是比较长的;理清各个表之间的关系后,得到如下SQL语句,方便以后使用

select 
    block_trx.trx_mysql_thread_id as blocking_session_id, -- 已经持有锁的session ID
    request_trx.trx_mysql_thread_id as request_session_id, -- 正在申请锁的session ID
    block_trx.trx_query as blocking_sql_text, -- 已经持有锁的SQL语句
    request_trx.trx_query as requesting_sql_text, -- 正在申请锁的SQL语句
    waits.blocking_trx_id as blocking_trx_id, -- 已经持有锁的事务ID
    waits.requesting_trx_id as requesting_trx_id, -- 正在申请锁的事务ID
    waits.requested_lock_id as requested_lock_id, -- 锁对象的ID
    locks.lock_table as lock_table, -- 锁对象所锁定的表
    locks.lock_type as lock_type, -- 锁类型
    locks.lock_mode as lock_mode -- 锁模式
from information_schema.innodb_lock_waits as waits
inner join information_schema.innodb_trx as block_trx
    on waits.blocking_trx_id=block_trx.trx_id
inner join information_schema.innodb_trx as request_trx
    on waits.requesting_trx_id=request_trx.trx_id
inner join information_schema.innodb_locks as locks
    on waits.requested_lock_id=locks.lock_id;
+---------------------+--------------------+-------------------+-----------------------------------+-----------------+-------------------+-------------------+--------------+-----------+-----------+
| blocking_session_id | request_session_id | blocking_sql_text | requesting_sql_text               | blocking_trx_id | requesting_trx_id | requested_lock_id | lock_table   | lock_type | lock_mode |
+---------------------+--------------------+-------------------+-----------------------------------+-----------------+-------------------+-------------------+--------------+-----------+-----------+
|                1435 |               1255 | NULL              | select * from tempdb.t for update | 26293           | 26299             | 26299:38:3:2      | `tempdb`.`t` | RECORD    | X         |
|                1435 |               1165 | NULL              | insert into t(x) values(3)        | 26293           | 26294             | 26294:38:3:1      | `tempdb`.`t` | RECORD    | X         |
+---------------------+--------------------+-------------------+-----------------------------------+-----------------+-------------------+-------------------+--------------+-----------+-----------+

 

MySQL锁等待分析【2】