3

Looking for help understanding why this deadlock is happening. I've read the MySQL documentation and the many SO questions on deadlock related issues but I'm obviously missing something.

This is the table in question:

CREATE TABLE `table_queue` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB ;

It has a whole bunch of other columns that have been removed to keep it simple (none of them are forein keys).

two or more threads are executing the following query:

DELETE q  FROM table_queue q WHERE q.id IN(165765,165770,165782,165787,165791 .. );

The IN statement could have upwars of 1,000 values. As you can see, it is deleting on the primary key. However, the innodb status output shows that one thread is waiting for a lock on a record that isn't even in its delete list (in the IN clause).

SHOW ENGINE INNODB Output:

------------------------
LATEST DETECTED DEADLOCK
------------------------
140526 15:54:03
*** (1) TRANSACTION:
TRANSACTION A841, ACTIVE 1 sec fetching rows
mysql tables in use 1, locked 1
LOCK WAIT 36 lock struct(s), heap size 2496, 882 row lock(s), undo log entries 70
MySQL thread id 59, OS thread handle 0xa69d9b40, query id 40446 localhost 127.0.0.1 localhost Sending data
DELETE q  FROM table_queue q WHERE q.id IN(165765,165770,165782,165787, ...
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 4653 n bits 216 index `PRIMARY` of table `maildb`.`table_queue` trx id A841 lock_mode X waiting
Record lock, heap no 67 PHYSICAL RECORD: n_fields 20; compact format; info bits 0
 0: len 4; hex 800288fb; asc     ;;
 1: len 6; hex 00000000a82d; asc      -;;
 2: len 7; hex a2000001e83a9e; asc      : ;;
 3: len 4; hex 80000001; asc     ;;
 4: len 4; hex 80000002; asc     ;;
 5: len 11; hex 6361726c6f732b36313535; asc jsmith+6155;;
 6: len 5; hex 536d697468; asc Smith;;
 7: len 22; hex 6361726c6f732b3631353540636f6d6d65722e636f6d; asc [email protected];;
 8: len 4; hex 8000180b; asc     ;;
 9: len 8; hex 43617465676f7279; asc Category;;
 10: len 4; hex 80000000; asc     ;;
 11: len 4; hex 80000000; asc     ;;
 12: len 4; hex 80000000; asc     ;;
 13: len 4; hex 80000000; asc     ;;
 14: SQL NULL;
 15: SQL NULL;
 16: len 1; hex 91; asc  ;;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;

*** (2) TRANSACTION:
TRANSACTION A844, ACTIVE 1 sec starting index read
mysql tables in use 1, locked 1
30 lock struct(s), heap size 2496, 646 row lock(s)
MySQL thread id 60, OS thread handle 0xa69a8b40, query id 40453 localhost 127.0.0.1 localhost Sending data
DELETE q  FROM table_queue q WHERE q.id IN(166139,166146,166162,166166,166171,166175,166177,....
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 4653 n bits 216 index `PRIMARY` of table `maildb`.`table_queue` trx id A844 lock mode S locks rec but not gap
Record lock, heap no 67 PHYSICAL RECORD: n_fields 20; compact format; info bits 0
 0: len 4; hex 800288fb; asc     ;;
 1: len 6; hex 00000000a82d; asc      -;;
 2: len 7; hex a2000001e83a9e; asc      : ;;
 3: len 4; hex 80000001; asc     ;;
 4: len 4; hex 80000002; asc     ;;
 5: len 11; hex 6361726c6f732b36313535; asc jsmith+6155;;
 6: len 5; hex 536d697468; asc Smith;;
 7: len 22; hex 6361726c6f732b3631353540636f6d6d65722e636f6d; asc [email protected];;
 8: len 4; hex 8000180b; asc     ;;
 9: len 8; hex 43617465676f7279; asc Category;;
 10: len 4; hex 80000000; asc     ;;
 11: len 4; hex 80000000; asc     ;;
 12: len 4; hex 80000000; asc     ;;
 13: len 4; hex 80000000; asc     ;;
 14: SQL NULL;
 15: SQL NULL;
 16: len 1; hex 91; asc  ;;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;

Record lock, heap no 74 PHYSICAL RECORD: n_fields 20; compact format; info bits 0
 0: len 4; hex 80028902; asc     ;;
 1: len 6; hex 00000000a82d; asc      -;;
 2: len 7; hex a2000001e83af9; asc      : ;;
 3: len 4; hex 80000001; asc     ;;
 4: len 4; hex 80000001; asc     ;;
 5: len 11; hex 6361726c6f732b36313632; asc jsmith+6162;;
 6: len 5; hex 536d697468; asc Smith;;
 7: len 22; hex 6361726c6f732b3631363240636f6d6d65722e636f6d; asc [email protected];;
 8: len 4; hex 80001812; asc     ;;
 9: len 8; hex 43617465676f7279; asc Category;;
 10: len 4; hex 80000000; asc     ;;
 11: len 4; hex 80000000; asc     ;;
 12: len 4; hex 80000000; asc     ;;
 13: len 4; hex 80000000; asc     ;;
 14: SQL NULL;
 15: SQL NULL;
 16: len 1; hex 94; asc  ;;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;
...
...

As far as I understand, transaction A841 is wanting to get an exclusive lock on a certain record. In this case I can tell the record has id = 166139. Looking at application logs, I can tell that that transaction/thread does NOT have id 166139 in its DELETE IN clause so why does it need a lock for that record?

Am I misunderstanding that if you explicitly specify the rows to delete using the primary key, MySQL should only be locking those matching PK rows? Is it possible it's locking other neighboring rows for some reason?

1
  • If the list of id's is very big, MySQL could decide it's more performant to lock the whole table instead of locking each record. Commented May 27, 2014 at 1:33

1 Answer 1

2

Innodb locks all the rows that are need to scan in case of delete operation.. You better understand by reading the following Blog:

BLOG LINK

1
  • I has the same problem. I just added the suggested indexes on my table.
    – digao_mb
    Commented Jan 14, 2015 at 16:19

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.