MySQL/MariaDB的锁超详细讲解(3)

mysql> show engine innodb status;
------------
TRANSACTIONS
------------
Trx id counter 14915
Purge done for trx's n:o < 14912 undo n:o < 0 state: running but idle
History list length 896
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 14909, not started
MySQL thread id 36, OS thread handle 0x7f5d57e4b700, query id 961 localhost root init
show engine innodb status
---TRANSACTION 14914, ACTIVE 465 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1184, 1 row lock(s)
MySQL thread id 34, OS thread handle 0x7f5d57e8c700, query id 959 localhost root updating
update tt set b= 'x' where a=1
------- TRX HAS BEEN WAITING 13 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 184 page no 3 n bits 80 index `GEN_CLUST_INDEX` of table `test`.`tt` trx id 14914 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 6; hex 000000000601; asc      ;;
 1: len 6; hex 000000003a41; asc    :A;;
 2: len 7; hex 2f000001580feb; asc /  X  ;;
 3: len 4; hex 80000001; asc    ;;
 4: len 5; hex 6820202020; asc h    ;;

------------------
---TRANSACTION 14913, ACTIVE 490 sec
2 lock struct(s), heap size 360, 6 row lock(s), undo log entries 1
MySQL thread id 1, OS thread handle 0x7f5d57f4f700, query id 900 localhost root

从上面的结果可以看到锁等待的信息。

"TRX HAS BEEN WAITING 13 SEC FOR THIS LOCK TO BE GRANTED"表示该事务申请锁已经等待了13秒。

"RECORD LOCKS space id 184 page no 3 n bits 80 index `GEN_CLUST_INDEX` of table `test`.`tt` trx id 14914 lock_mode X waiting"表示test.tt表上的记录要申请的行锁(recode lock)是独占锁并且正在waiting,并且标明了该行记录所在表数据文件中的物理位置:表空间id为184,页码为3。

关于这些信息的详细解释,后文会逐渐说明。

2.使用show processlist查看。

MySQL/MariaDB的锁超详细讲解

show full processlist;

从上面的结果可以看出,update语句一直处于updating状态。所以,该方法查出来的并不一定是锁等待,有可能是更新的记录太多或者其他问题,总之这里看出来的是该语句还没有执行完成。

3.查看information_schema中的数据字典

在information_schema架构下,有3个表记录了事务和锁相关的信息。分别是INNODB_TRX,INNODB_LOCKS,INNODB_LOCK_WAITS。

这三个表可能相对复杂,以下分别说明这3张表的各列。

MySQL/MariaDB的锁超详细讲解

根据上面实验过程中的锁查看该表的部分结果如下:

mysql> select * from information_schema.INNODB_TRX\G
*************************** 1. row ***************************
                    trx_id: 14914
                trx_state: LOCK WAIT
              trx_started: 2017-03-30 06:07:51
    trx_requested_lock_id: 14914:184:3:2
          trx_wait_started: 2017-03-30 06:39:25
                trx_weight: 2
      trx_mysql_thread_id: 34
                trx_query: update tt set b= 'x' where a=1
*************************** 2. row ***************************
                    trx_id: 14913
                trx_state: RUNNING
              trx_started: 2017-03-30 06:07:26
    trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 3
      trx_mysql_thread_id: 1
                trx_query: NULL

从结果中可以看出id为14914的事务正处于锁等待状态,该事务中要申请锁的语句是update语句,也就是说是因为该语句而导致的锁等待。

MySQL/MariaDB的锁超详细讲解

从innodb_trx表中只能查看到事务的信息,而不能看到锁相关的信息。要看锁的信息,需要查看表innodb_locks。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3810f55420a83dd159a316202f796b19.html