首页 > 代码库 > information_schema系列八(锁,事物)

information_schema系列八(锁,事物)

这个系列的文章主要是为了能够让自己了解MySQL5.7的一些系统表,统一做一下备注和使用,也希望分享出来让大家能够有一点点的受益。
第八篇主要看一下一下几系统表:
今天我们主要看一下MySQL information_schema里面的关于innodb的锁和事物的两三个系统表:
看一下锁对应的sql(未结束的事物):
select * from innodb_lock_waits;select * from innodb_locks limit 2\Gselect * from information_schema.innodb_trx\Gselect * from information_schema.innodb_trx where trx_id = 45734628\G
SELECTlw.requesting_trx_id AS request_ID, trx.trx_mysql_thread_id as request_mysql_ID,trx.trx_query AS request_command,lw.blocking_trx_id AS blocking_ID, trx1.trx_mysql_thread_id as blocking_mysql_ID,trx1.trx_query AS blocking_command,lo.lock_index AS lock_indexFROMinformation_schema.innodb_lock_waits lwINNER JOIN information_schema.innodb_locks lo ON lw.requesting_trx_id = lo.lock_trx_idINNER JOIN information_schema.innodb_locks lo1 ON lw.blocking_trx_id = lo1.lock_trx_idINNER JOIN information_schema.innodb_trx trx ON lo.lock_trx_id = trx.trx_idINNER JOIN information_schema.innodb_trx trx1 ON lo1.lock_trx_id = trx1.trx_id;

1: INNODB_LOCKS

2: INNODB_TRX
3: INNODB_LOCK_WAITS
三张表就一起实验好了,都是关于锁和事物的阻塞的。我们现在开两个终端。
第一个终端开启一个事物,进行更新:
root@localhost [(none)]>start transaction;Query OK, 0 rows affected (0.00 sec)root@localhost [(none)]>update qiandai.t1 set col_int_key=333 where pk=10;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0

第二个终端直接也更新同一行数据:

update qiandai.t1 set col_int_key=222 where pk=10;

然后去查看三个表联合查询:

技术分享
可以看得到,第二个更新是被阻塞的,因为第一个更新获取到了排它锁,所以第二个更新一致处于等待状态,直到锁等待时间超时:
SHOW VARIABLES LIKE %LOCK_WAIT%;

上面可以查看到锁等待的超时时间,INNODB默认五十秒。

看一下三个表官方给的解释:
 
INNODB_LOCKS:https://dev.mysql.com/doc/refman/5.7/en/innodb-locks-table.html
INNODB_TRX:https://dev.mysql.com/doc/refman/5.7/en/innodb-trx-table.html
INNODB_LOCK_WAITS:https://dev.mysql.com/doc/refman/5.7/en/innodb-lock-waits-table.html
 

information_schema系列八(锁,事物)