【Mysql】两条insert 语句产生的死锁
发表于:2024-10-25 作者:千家信息网编辑
千家信息网最后更新 2024年10月25日,背景:查看status日志发现两条insert 出现了死锁RECORD LOCKS space id 388 page no 27032 n bits 616 index `idx_svcorders
千家信息网最后更新 2024年10月25日【Mysql】两条insert 语句产生的死锁背景:查看status日志发现两条insert 出现了死锁
3. 根据
LOCK WAIT 14 lock struct(s), heap size 2936, 7 row lock(s), undo log entries 7
有7个undo entires,而单纯的INSERT一条记录只有一个undo entry,因此可以推断除了INSERT,必然还有别的操作
基于以上,事务除了INSERT,可能还存在DELETE/UPDATE,并且这些操作是走的二级索引来查找更新记录。
一个简单但不完全相同的重现步骤: DROP TABLE t1; CREATE TABLE `t1` ( `a` int(11) NOT NULL AUTO_INCREMENT, `b` int(11) DEFAULT NULL, `c` int(11) DEFAULT NULL, PRIMARY KEY (`a`), KEY `b` (`b`) ) ENGINE=InnoDB ; insert into t1(a, b,c) values(1,2,3),(5,4,6),(8, 7,9),(12,12,19),(15,15,11);
再举一个例子
session1: mysql> select now();start TRANSACTION;
+---------------------+
| now() |
+---------------------+
| 2018-01-25 16:08:46 |
+---------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test01 where app='05' for update; ---第1步 锁住【6.04】-【7.05】以及【7.05】-【8.06】 两段区间
+----+-----+
| id | app |
+----+-----+
| 7 | 05 |
+----+-----+
1 row in set (0.00 sec)
mysql> insert into test01(app) values ('07'); --第三步 等待第二步释放
Query OK, 1 row affected (23.24 sec)
session2:
mysql> select * from test01 where app='08' for update; --第二步 锁住【12,07】-【13,08】以及【13,08】-【14,09】两段区间
+----+-----+
| id | app |
+----+-----+
| 13 | 08 |
+----+-----+
1 row in set (0.00 sec)
mysql> insert into test01(app) values ('04'); ----第四步 等待第一步释放,,于是死锁
ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
查看死锁日志:
死锁日志是不是和上面的一样?
参考:
http://blog.itpub.net/22664653/viewspace-2145068/ ----杨奇龙
http://www.sohu.com/a/169663059_610509 ---insert ..select 语句产生死锁
http://blog.itpub.net/7728585/viewspace-2146183/ ---insert ..select 语句产生死锁--八怪
- RECORD LOCKS space id 388 page no 27032 n bits 616 index `idx_svcorderserviceitem_workorderid_quantity` of table `ecejservice`.`svc_order_service_item` trx id 596252578 lock_mode X insert intention waiting
3. 根据
LOCK WAIT 14 lock struct(s), heap size 2936, 7 row lock(s), undo log entries 7
有7个undo entires,而单纯的INSERT一条记录只有一个undo entry,因此可以推断除了INSERT,必然还有别的操作
基于以上,事务除了INSERT,可能还存在DELETE/UPDATE,并且这些操作是走的二级索引来查找更新记录。
一个简单但不完全相同的重现步骤: DROP TABLE t1; CREATE TABLE `t1` ( `a` int(11) NOT NULL AUTO_INCREMENT, `b` int(11) DEFAULT NULL, `c` int(11) DEFAULT NULL, PRIMARY KEY (`a`), KEY `b` (`b`) ) ENGINE=InnoDB ; insert into t1(a, b,c) values(1,2,3),(5,4,6),(8, 7,9),(12,12,19),(15,15,11);
session1:
begin; delete from t1 where b = 12; //二级索引上lock_mode X、lock_mode X locks gap before rec以及主键上的lock_mode X locks rec but not gap 二级索引:heap_no=5, type_mode=3 (12上的LOCK_ORDINARY类型锁,包括记录和记录前的GAP) 聚集索引:heap_no=5,type_mode=1027 二级索引:heap_no=6,type_mode=547(15上的GAP锁) session2: begin; delete from t1 where b = 7; //二级索引上lock_mode X、lock_mode X locks gap before rec以及主键上的lock_mode X locks rec but not gap 二级索引:heap_no=4,type_mode=3 (7上的LOCK_ORDINARY类型锁,包括记录和记录前的GAP) 聚集索引:heap_no=4,type_mode=1027 二级索引:heap_no=5,type_mode=547 (记录12上的GAP锁) session1: insert into t1 values (NULL, 6,10); //新插入记录聚集索引无冲突插入成功,二级索引等待插入意向锁(lock_mode X locks gap before rec insert intention waiting) 二级索引,heap_no=4, type_mode=2819 (请求记录7上面的插入意向锁LOCK_X | LOCK_GAP | LOCK_INSERT_INTENTION, 需要等待session2 session2: insert into t1 values (NULL, 7,10); 二级索引:heap_no=5, type_mode=2819 (请求记录12上的插入意向锁LOCK_X | LOCK_GAP | LOCK_INSERT_INTENTION,需要等待session1) 互相等待,导致发生死锁 从打印的死锁信息来看,基本和线上发生的死锁现象是一致的。再举一个例子
- mysql> select * from test01;
- +----+-----+
- | id | app |
- +----+-----+
- | 1 | 01 |
- | 2 | 02 |
- | 5 | 03 |
- | 10 | 03 |
- | 6 | 04 |
- | 7 | 05 |
- | 8 | 06 |
- | 9 | 06 |
- | 11 | 06 |
- | 12 | 07 |
- | 13 | 08 |
- | 14 | 09 |
- | 15 | 09 |
- +----+-----+
- 13 rows in set (0.00 sec)
查看死锁日志:
- LATEST DETECTED DEADLOCK
- ------------------------
- 2018-01-25 16:09:54 0x7f07d23ff700
- *** (1) TRANSACTION:
- TRANSACTION 5375, ACTIVE 51 sec inserting
- mysql tables in use 1, locked 1
- LOCK WAIT 5 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1
- MySQL thread id 2294, OS thread handle 139671567841024, query id 42463 localhost root update
- insert into test01(app) values ('07')
- *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
- RECORD LOCKS space id 64 page no 4 n bits 80 index idx_app of table `devops`.`test01` trx id 5375 lock_mode X locks gap before rec insert intention waiting
- Record lock, heap no 12 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
- 0: len 2; hex 3038; asc 08;;
- 1: len 4; hex 0000000d; asc ;;
- *** (2) TRANSACTION:
- TRANSACTION 5376, ACTIVE 38 sec inserting
- mysql tables in use 1, locked 1
- 5 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1
- MySQL thread id 2293, OS thread handle 139671568905984, query id 42464 localhost root update
- insert into test01(app) values ('04')
- *** (2) HOLDS THE LOCK(S):
- RECORD LOCKS space id 64 page no 4 n bits 80 index idx_app of table `devops`.`test01` trx id 5376 lock_mode X
- Record lock, heap no 12 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
- 0: len 2; hex 3038; asc 08;;
- 1: len 4; hex 0000000d; asc ;;
- *** (2) WAITING FOR THIS LOCK TO BE GRANTED:
- RECORD LOCKS space id 64 page no 4 n bits 80 index idx_app of table `devops`.`test01` trx id 5376 lock_mode X locks gap before rec insert intention waiting
- Record lock, heap no 6 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
- 0: len 2; hex 3035; asc 05;;
- 1: len 4; hex 00000007; asc ;;
- *** WE ROLL BACK TRANSACTION (2)
- ------------
死锁日志是不是和上面的一样?
参考:
http://blog.itpub.net/22664653/viewspace-2145068/ ----杨奇龙
http://www.sohu.com/a/169663059_610509 ---insert ..select 语句产生死锁
http://blog.itpub.net/7728585/viewspace-2146183/ ---insert ..select 语句产生死锁--八怪
索引
死锁
意向
日志
类型
语句
区间
面的
更新
相同
一致
成功
事务
例子
信息
只有
必然
所知
步骤
现象
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广东捷付网络技术有限公司
国际性互联网科技展设备
网络安全基础 仰红礼
服务器上mip是什么意思
BBS网络安全
网络安全法全文打印
出名的分布式数据库技术
大学生网络安全与诈骗观后感
机场网络安全重要性
四川通用服务器直销厂家
数据库在键码属性上建立索引
湖南美租网络技术有限公司
人人爱家网络技术有限公司
维护数据库有哪些
我的世界服务器怎样快速找到他人
区块链服务网络技术创新基地
怎么限制服务器流量
云服务器图片如何引用
只适用于数据库表的索引
所有网络安全元素里最重要的是
咸阳软件开发价格
卷积神经网络安全帽佩戴级别
缓存服务器部署
银行加强信息网络安全会议
新员工网络安全 软通知乎
plsql无法识别数据库
涛思数据库售价
ccess数据库难吗
手机软件开发商变更
软件开发质保一年成本