*** (1) TRANSACTION: TRANSACTION 39474, ACTIVE 58 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 1200, 4 row lock(s), undo log entries 3 MySQL thread id 9, OS thread handle 123145525800960, query id 77 localhost root updating DELETE FROM t1 WHERE id = 4 *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 114 page no 4 n bits 80 index PRIMARY of table `dhy`.`t1` trx id 39474 lock_mode X locks rec but not gap waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000004; asc ;; 1: len 6; hex 000000009a33; asc 3;; 2: len 7; hex 02000001471399; asc G ;; 3: len 2; hex 6464; asc dd;;
*** (2) TRANSACTION: TRANSACTION 39475, ACTIVE 46 sec starting index read mysql tables in use 1, locked 1 3 lock struct(s), heap size 1200, 4 row lock(s), undo log entries 3 MySQL thread id 10, OS thread handle 123145526104064, query id 78 localhost root updating DELETE FROM t1 WHERE id = 3 *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 114 page no 4 n bits 80 index PRIMARY of table `dhy`.`t1` trx id 39475 lock_mode X locks rec but not gap Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000004; asc ;; 1: len 6; hex 000000009a33; asc 3;; 2: len 7; hex 02000001471399; asc G ;; 3: len 2; hex 6464; asc dd;;
Record lock, heap no 6 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000005; asc ;; 1: len 6; hex 000000009a33; asc 3;; 2: len 7; hex 02000001471375; asc G u;; 3: len 2; hex 6565; asc ee;;
Record lock, heap no 7 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000006; asc ;; 1: len 6; hex 000000009a33; asc 3;; 2: len 7; hex 02000001471351; asc G Q;; 3: len 2; hex 6666; asc ff;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 114 page no 4 n bits 80 index PRIMARY of table `dhy`.`t1` trx id 39475 lock_mode X locks rec but not gap waiting Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000003; asc ;; 1: len 6; hex 000000009a32; asc 2;; 2: len 7; hex 01000001462e1f; asc F. ;; 3: len 2; hex 6363; asc cc;;
DELETE FROM t1 WHERE id = 4 *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 114 page no 4 n bits 80 index PRIMARY of table `dhy`.`t1` trx id 39474 lock_mode X locks rec but not gap waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000004; asc ;; //聚集索引的值 1: len 6; hex 000000009a33; asc 3;; //事务ID 2: len 7; hex 02000001471399; asc G ;; //undo 记录 3: len 2; hex 6464; asc dd;; //非主键字段的值
通过以上信息可以得出事务39474执行delete语句时,锁等待发生在申请ID=4这条记录上的X锁“lock_mode X locks rec but not gap waiting”
事务39475持有锁的信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 114 page no 4 n bits 80 index PRIMARY of table `dhy`.`t1` trx id 39475 lock_mode X locks rec but not gap Record lock, heap no 5 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000004; asc ;; 1: len 6; hex 000000009a33; asc 3;; 2: len 7; hex 02000001471399; asc G ;; 3: len 2; hex 6464; asc dd;;
Record lock, heap no 6 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000005; asc ;; 1: len 6; hex 000000009a33; asc 3;; 2: len 7; hex 02000001471375; asc G u;; 3: len 2; hex 6565; asc ee;;
Record lock, heap no 7 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000006; asc ;; 1: len 6; hex 000000009a33; asc 3;; 2: len 7; hex 02000001471351; asc G Q;; 3: len 2; hex 6666; asc ff;;
事务39475持有在ID=4,5,6上X锁
事务39475同样在执行delete语句时发生了所等待
1 2 3 4 5 6 7 8 9 10 11 12 13 14
*** (2) TRANSACTION: TRANSACTION 39475, ACTIVE 46 sec starting index read mysql tables in use 1, locked 1 3 lock struct(s), heap size 1200, 4 row lock(s), undo log entries 3 MySQL thread id 10, OS thread handle 123145526104064, query id 78 localhost root updating DELETE FROM t1 WHERE id = 3
*** (2) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 114 page no 4 n bits 80 index PRIMARY of table `dhy`.`t1` trx id 39475 lock_mode X locks rec but not gap waiting Record lock, heap no 4 PHYSICAL RECORD: n_fields 4; compact format; info bits 32 0: len 4; hex 00000003; asc ;; 1: len 6; hex 000000009a32; asc 2;; 2: len 7; hex 01000001462e1f; asc F. ;; 3: len 2; hex 6363; asc cc;;
申请ID=3上的X锁时发生了所等待,执行的语句是:DELETE FROM t1 WHERE id = 3,那么可以得出39474在id=3上持有了X锁,但是在死锁日志中并没有显示出事务39474持有锁的信息 那么这两个事务加锁的顺序应是: 1. 事务39474持有了id=3上的X锁 2. 事务39475持有了id=4上的X锁 3. 事务39474申请id=4上X锁时发生了锁等待执行的语句是:DELETE FROM t1 WHERE id = 4 4. 事务39475申请id=3上X锁时触发了死锁,因为此时双方都在申请对方持有的锁不能进行下去了。