mysql查询事务和锁

记录原因:今天在一个mysql更新语句的执行过程中,总是执行超时,后来查到原因是因为有一个事务没有关闭,导致那条记录的查询和更新都会执行超时

以下的SQL语句可以查询当前数据库, 有哪些事务,都锁定哪些资源

SELECT
    trx_id AS  `事务ID`,
    trx_state AS `事务状态`,
    trx_requested_lock_id  AS  `事务需要等待的资源`,
    trx_wait_started    AS  `事务开始等待时间`,
    trx_tables_in_use AS `事务使用表`,
    trx_tables_locked AS `事务拥有锁`,
    trx_rows_locked  AS `事务锁定行`,
    trx_rows_modified  AS `事务更改行`
FROM
    information_schema.innodb_trx ;
SELECT
    lock_id         AS  `锁ID`,
    lock_trx_id    AS  `拥有锁的事务ID`,
    lock_mode      AS  `锁模式    `,
    lock_type      AS  `锁类型`,
    lock_table         AS  `被锁的表`,
    lock_index         AS  `被锁的索引`,
    lock_space         AS  `被锁的表空间号`,
    lock_page      AS  `被锁的页号`,
    lock_rec       AS  `被锁的记录号`,
    lock_data      AS  `被锁的数据`
FROM
    information_schema.innodb_locks;
SELECT
  requesting_trx_id   AS  `请求锁的事务ID`,
  requested_lock_id   AS  `请求锁的锁ID`,
  blocking_trx_id     AS  `当前拥有锁的事务ID`,
  blocking_lock_id    AS  `当前拥有锁的锁ID`
FROM
  information_schema.innodb_lock_waits;

代码转自:奋斗熊的博客

你可能感兴趣的:(mysql)