MYSQL:删除时出现奇怪的死锁,为什么会发生这种情况?

问题描述 投票:0回答:1

我在mysql 5.7上遇到了一个奇怪的死锁。

我使用“READ COMMITED”隔离级别(即没有间隙锁定),但是当按主键删除行时,我遇到了死锁(偶尔)。奇怪的是,它报告等待查询中甚至不存在的锁。

表定义是这样的:

CREATE TABLE `table123` (
  `id` bigint(16) NOT NULL AUTO_INCREMENT,
  `col1` int(11) DEFAULT NULL,
  `col2` decimal(13,6) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `col1` (`col1`)
) ENGINE=InnoDB AUTO_INCREMENT=26098012 DEFAULT CHARSET=utf8;

以下是SHOW ENGINE INNODB状态的部分输出:

LATEST DETECTED DEADLOCK
------------------------
2019-02-14 14:22:09 0x7fa4f0077700
*** (1) TRANSACTION:
TRANSACTION 96040670, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 12 lock struct(s), heap size 1136, 84 row lock(s), undo log entries 27
MySQL thread id 27032525, OS thread handle 140346247255808, query id 703284759 127.0.0.1 user123 updating
DELETE FROM `table123` WHERE (/*5/280*/ id IN (26053052,26053057,26053062,26053067,26053072,26053077))
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1428 page no 3 n bits 176 index PRIMARY of table `schema123`.`table123` trx id 96040670 lock_mode X locks rec but not gap waiting
Record lock, heap no 86 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d8996; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402ce3; asc B   @, ;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 80000000000018; asc        ;;

*** (2) TRANSACTION:
TRANSACTION 96040669, ACTIVE 0 sec fetching rows, thread declared inside InnoDB 4970
mysql tables in use 1, locked 1
13 lock struct(s), heap size 1136, 128 row lock(s), undo log entries 57
MySQL thread id 27032524, OS thread handle 140346378385152, query id 703284777 127.0.0.1 user123 updating
DELETE FROM `table123` WHERE (/*4/343*/ id IN (26053016,26053021,26053026,26053031,26053036,26053041))
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 1428 page no 3 n bits 176 index PRIMARY of table `schema123`.`table123` trx id 96040669 lock_mode X locks rec but not gap
Record lock, heap no 4 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89a7; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001403516; asc B   @5 ;;
 3: len 4; hex 80000157; asc    W;;
 4: len 7; hex 800000000000de; asc        ;;

Record lock, heap no 8 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d899b; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402d14; asc B   @- ;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 80000000000035; asc       5;;

Record lock, heap no 13 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89a2; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 420000014034e5; asc B   @4 ;;
 3: len 4; hex 80000157; asc    W;;
 4: len 7; hex 80000000000050; asc       P;;

Record lock, heap no 17 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d8997; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 4200000140316d; asc B   @1m;;
 3: len 4; hex 8000016c; asc    l;;
 4: len 7; hex 80000000000348; asc       H;;

Record lock, heap no 18 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89a0; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402d45; asc B   @-E;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 80000000000018; asc        ;;

Record lock, heap no 19 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89a6; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001403200; asc B   @2 ;;
 3: len 4; hex 8000016c; asc    l;;
 4: len 7; hex 8000000000091d; asc        ;;

Record lock, heap no 21 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89ab; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001403231; asc B   @21;;
 3: len 4; hex 8000016c; asc    l;;
 4: len 7; hex 80000000000762; asc       b;;

Record lock, heap no 28 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89ac; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001403547; asc B   @5G;;
 3: len 4; hex 80000157; asc    W;;
 4: len 7; hex 800000000000b4; asc        ;;

Record lock, heap no 40 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89aa; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402da7; asc B   @- ;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 80000000000036; asc       6;;

Record lock, heap no 60 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d899c; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 4200000140319e; asc B   @1 ;;
 3: len 4; hex 8000016c; asc    l;;
 4: len 7; hex 8000000000074d; asc       M;;

Record lock, heap no 62 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d8998; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001403483; asc B   @4 ;;
 3: len 4; hex 80000157; asc    W;;
 4: len 7; hex 80000000000050; asc       P;;

Record lock, heap no 70 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d899d; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 420000014034b4; asc B   @4 ;;
 3: len 4; hex 80000157; asc    W;;
 4: len 7; hex 800000000000b2; asc        ;;

Record lock, heap no 79 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89a1; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 420000014031cf; asc B   @1 ;;
 3: len 4; hex 8000016c; asc    l;;
 4: len 7; hex 80000000000348; asc       H;;

Record lock, heap no 86 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d8996; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402ce3; asc B   @, ;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 80000000000018; asc        ;;

Record lock, heap no 87 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89b1; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001403578; asc B   @5x;;
 3: len 4; hex 80000157; asc    W;;
 4: len 7; hex 800000000000fa; asc        ;;

Record lock, heap no 88 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89af; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402dd8; asc B   @- ;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 8000000000004b; asc       K;;

Record lock, heap no 89 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89b0; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001403262; asc B   @2b;;
 3: len 4; hex 8000016c; asc    l;;
 4: len 7; hex 80000000000a41; asc       A;;

Record lock, heap no 102 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89a5; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402d76; asc B   @-v;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 80000000000043; asc       C;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1428 page no 3 n bits 176 index PRIMARY of table `schema123`.`table123` trx id 96040669 lock_mode X locks rec but not gap waiting
Record lock, heap no 54 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d89ba; asc         ;;
 1: len 6; hex 000005b976de; asc     v ;;
 2: len 7; hex 43000001a213a3; asc C      ;;
 3: len 4; hex 8000010f; asc     ;;
 4: len 7; hex 80000000000028; asc       (;;

*** WE ROLL BACK TRANSACTION (1)

最奇怪的部分是:

DELETE FROM `table123` WHERE (/*5/280*/ id IN (26053052,26053057,26053062,26053067,26053072,26053077))
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 1428 page no 3 n bits 176 index PRIMARY of table `schema123`.`table123` trx id 96040670 lock_mode X locks rec but not gap waiting
Record lock, heap no 86 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 8; hex 80000000018d8996; asc         ;;
 1: len 6; hex 000005b976dd; asc     v ;;
 2: len 7; hex 42000001402ce3; asc B   @, ;;
 3: len 4; hex 80000166; asc    f;;
 4: len 7; hex 80000000000018; asc        ;;

为什么它等待锁定的行ID = 26053014(十六进制80000000018d8996),如果它甚至不参与查询?

mysql innodb deadlock
1个回答
1
投票

罪魁祸首是mysql有时决定不使用索引。即这个查询:

DELETE FROM `table123` WHERE (/*4/343*/ id IN (26053016,26053021,26053026,26053031,26053036,26053041))

有时不使用索引,当发生这种情况时 - 所有行都被锁定。

这解释了为什么没有涉及任何查询的行被报告为已锁定。

死锁本身发生是因为我发出多个(非重叠)DELETE .. IN()语句。

© www.soinside.com 2019 - 2024. All rights reserved.