序号 | 索引 | 锁类型 |
---|---|---|
1 | uk_a | X |
2 | PRIMARY | X |
3 | idx_b | X |
update t3 set b = '' where b = "2";
整理一下,加了 3 个 X 锁,顺序分别是
序号 | 索引 | 锁类型 |
---|---|---|
1 | idx_b | X |
2 | PRIMARY | X |
3 | idx_b | X |
两条语句从加锁顺序看起来就已经有构成死锁的条件了
手动是比较难模拟的,写个代码并发的去同时执行那两条 SQL 语句,马上就出现死锁了
------------------------ 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)
分析一下死锁日志
*** (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
事务2:想获取主键索引的 X 锁
*** (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
事务1:持有主键索引的 X 锁
*** (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
事务1:想获取普通索引 idx_b 的 X 锁
与我们分析的完全一致,也与线上的死锁日志一模一样
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
标签:昌都 黔西 荆门 陕西 鄂尔多斯 梅河口 北京 驻马店
巨人网络通讯声明:本文标题《由不同的索引更新解决MySQL死锁套路》,本文关键词 由,不同,的,索引,更新,解决,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。