温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL:Innodb 一个死锁案例

发布时间:2020-08-08 21:13:42 来源:ITPUB博客 阅读:136 作者:gaopengtttt 栏目:MySQL数据库

一、准备数据和问题

RR隔离级别

CREATE TABLE `ty` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `a` int(11) DEFAULT NULL,
  `b` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idxa` (`a`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4
insert into ty(a,b) values(2,3),(5,4),(6,7);

问:


MySQL:Innodb 一个死锁案例

image.png

这种情况会产生死锁,如果将
insert into ty(a,b) values(2,10);
改为:
insert into ty(a,b) values(5,10);

则不会产生死锁为什么?

二、初始化数据画图

本死锁的堵塞主要集中在二级索引中,我们将二级索KEY  idxa  ( a )和主键的数据按照Innodb引擎存储的方式大概排列一下则如图:

MySQL:Innodb 一个死锁案例

image.png


三、T2 步骤1

T2 步骤1:delete from ty where a=5;

-----TRX NO:334719 LOCK STRUCT(1)(Add by gaopeng)
RECORD LOCKS space id 653 page no 4 n bits 72 index idxa of table `test`.`ty` trx id 334719 lock_mode X(LOCK_X) locks gap and rec(LOCK_ORDINARY[next_key_lock])
Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
 0: len 4; hex 80000005; asc     ;; 1: len 4; hex 80000009; asc     ;;
-----TRX NO:334719 LOCK STRUCT(1)(Add by gaopeng)
RECORD LOCKS space id 653 page no 3 n bits 72 index PRIMARY of table `test`.`ty` trx id 334719 lock_mode X(LOCK_X) locks rec but not gap(LOCK_REC_NOT_GAP)
Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 4; hex 80000009; asc     ;; 1: len 6; hex 000000051b7f; asc       ;; 2: len 7; hex 760000082b13fd; asc v   +  ;; 3: len 4; hex 80000005; asc     ;; 4: len 4; hex 80000004; asc     ;;
-----TRX NO:334719 LOCK STRUCT(1)(Add by gaopeng)
RECORD LOCKS space id 653 page no 4 n bits 72 index idxa of table `test`.`ty` trx id 334719 lock_mode X(LOCK_X) locks gap before rec(LOCK_GAP)
Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 4; hex 80000006; asc     ;; 1: len 4; hex 8000000a; asc     ;;

根据这个记录我们可以画图如下,红色部分为锁定的部分箭头为gap lock:

MySQL:Innodb 一个死锁案例

image.png

四、T1 步骤2

T2 步骤1:delete from ty where a=5; 堵塞

-----TRX NO:334724 LOCK STRUCT(1)(Add by gaopeng)
RECORD LOCKS space id 653 page no 4 n bits 72 index idxa of table `test`.`ty` trx id 334724 lock_mode X(LOCK_X) locks gap and rec(LOCK_ORDINARY[next_key_lock]) waiting(LOCK_WAIT)
Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
 0: len 4; hex 80000005; asc     ;; 1: len 4; hex 80000009; asc     ;;

根据这个记录我们可以画图如下,黄色部分为事务T1准备上锁但是被堵塞的部分,包含黄色部分和红色部分的记录说明它既被T2锁定了并且T1拿不到这条记录的锁,它实际上就是一个next key lock的堵塞:

MySQL:Innodb 一个死锁案例

image.png

五、T2步骤3

这一步如果是:

insert into ty(a,b) values(2,10);
则发生死锁,实际上这一条记录记录在二级索引的值为(2,11),11是主键的值,则画图如下:


MySQL:Innodb 一个死锁案例

image.png

这种情况下则T2也被堵塞,因为这个区域T1也处于堵塞下,则发生死锁。死锁记录如下:

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 653 page no 4 n bits 72 index idxa of table `test`.`ty` trx id 334712 lock_mode X(LOCK_X) locks gap before rec(LOCK_GAP) insert intention(LOCK_INSERT_INTENTION) waiting(LOCK_WAIT)
Record lock, heap no 3 PHYSICAL RECORD: n_fields 2; compact format; info bits 32
 0: len 4; hex 80000005; asc     ;; 1: len 4; hex 80000009; asc     ;;

及插入印象锁堵塞

这一步如果是:

insert into ty(a,b) values(5,10);
不会发生死锁,,实际上这一条记录记录在二级索引的值为(5,11),11是主键的值,则画图如下:

MySQL:Innodb 一个死锁案例

image.png

如果是这种情况,不会发生死锁,我们可以看到对于二级索引而言这个区域没有其他事物堵塞,只是T2最开始获取过,本事务再次获取不会有问题。

六、总结

本案例实际上就是看最后触发死锁的插入操作插入的记录到底落在二级索引的哪个区域。

作者微信: MySQL:Innodb 一个死锁案例


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI