MySQL 死锁套路:看一例走不同索引更新的例子

2019-05-2519:10:08数据库教程Comments2,745 views字数 2152阅读模式
CREATE TABLE `t3` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `a` varchar(5) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `b` varchar(5) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),UNIQUE KEY `uk_a` (`a`), KEY `idx_b` (`b`) 
)
复制代码

sql语句如下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

t1
update t3 set b = '' where a = "1";

t2
update t3 set b = '' where b = "2";
复制代码

我们先用之前debug的方式来看一下,这里两条语句分别加了哪些锁文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

第一条语句(通过唯一索引去更新记录)

update t3 set b = '' where a = "1";文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

MySQL 死锁套路:看一例走不同索引更新的例子
MySQL 死锁套路:看一例走不同索引更新的例子
MySQL 死锁套路:看一例走不同索引更新的例子

整理一下,加了3个X锁,顺序分别是文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

序号索引锁类型
1uk_aX
2PRIMARYX
3idx_bX

第二条语句

update t3 set b = '' where b = "2";文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

MySQL 死锁套路:看一例走不同索引更新的例子
MySQL 死锁套路:看一例走不同索引更新的例子
MySQL 死锁套路:看一例走不同索引更新的例子

整理一下,加了3个X锁,顺序分别是文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

序号索引锁类型
1idx_bX
2PRIMARYX
3idx_bX

两条语句从加锁顺序看起来就已经有构成死锁的条件了文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

MySQL 死锁套路:看一例走不同索引更新的例子

手动是比较难模拟的,写个代码去跑一下文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

马上就出现了文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

------------------------
LATEST DETECTED DEADLOCK
------------------------
181102 12:45:05
*** (1) TRANSACTION:
TRANSACTION 50AF, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 376, 2 row lock(s)
MySQL thread id 34, OS thread handle 0x70000d842000, query id 549 localhost 127.0.0.1 root Searching rows for update
update t3 set b = '' where b = "2"
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 67 page no 3 n bits 72 index `PRIMARY` of table `d1`.`t3` trx id 50AF lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 0000000050ae; asc     P ;;
 2: len 7; hex 03000001341003; asc     4  ;;
 3: len 1; hex 31; asc 1;;
 4: len 0; hex ; asc ;;

*** (2) TRANSACTION:
TRANSACTION 50AE, ACTIVE 0 sec updating or deleting
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1248, 3 row lock(s), undo log entries 1
MySQL thread id 35, OS thread handle 0x70000d885000, query id 548 localhost 127.0.0.1 root Updating
update t3 set b = '' where a = "1"
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 67 page no 3 n bits 72 index `PRIMARY` of table `d1`.`t3` trx id 50AE lock_mode X locks rec but not gap
Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
 0: len 4; hex 80000001; asc     ;;
 1: len 6; hex 0000000050ae; asc     P ;;
 2: len 7; hex 03000001341003; asc     4  ;;
 3: len 1; hex 31; asc 1;;
 4: len 0; hex ; asc ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 67 page no 5 n bits 72 index `idx_b` of table `d1`.`t3` trx id 50AE lock_mode X locks rec but not gap waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 1; hex 32; asc 2;;
 1: len 4; hex 80000001; asc     ;;

*** WE ROLL BACK TRANSACTION (1)
复制代码

跟我们线上的死锁日志一模一样文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

作者:挖坑的张师傅
链接:https://juejin.im/post/5ce889a4e51d4510835e0218
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/sjk/12954.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/sjk/12954.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定