0

I am experiencing a strange deadlock on mysql 5.7.

I use "READ COMMITED" isolation level (i.e. no gap locking), however I get a deadlock (once in a while) when deleting rows by primary key. What is strange is that it reports waiting for a lock that is not even in the query.

The table definition is this:

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;

Here is the partial output of SHOW ENGINE INNODB STATUS:

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)

The strangest part is this:

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        ;;

Why it waits for the locked row ID=26053014 (hex 80000000018d8996) if it's not even involved in the query?

1 Answer 1

1

The culprit was the fact that mysql sometimes decides to not use an index. I.e. this query:

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

sometimes uses no index, when this happens - all rows get locked.

This explains why a row that is not invloved in any of the queries is reported as locked.

The deadlock itself happens because i issue multiple (non-overlapping) DELETE .. IN () statements.

3
  • How did you discover that no index was used? And is id the PRIMARY KEY?
    – Rick James
    Commented Feb 15, 2019 at 6:15
  • Yes ID is PK. I discovered that using explain select. If there are "enough" IDs in the IN() statement (probably some percentage of the total rows) it stops using the PK. Commented Feb 15, 2019 at 10:59
  • Yeah, I've heard of that. But I am still looking for a concrete description of what the cutoff might be. One rumor was that it was a fixed number, perhaps 10. Your list seems so short as to not trigger a table scan.
    – Rick James
    Commented Feb 16, 2019 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.