以下死锁在MySQL中发生。
*** (1) TRANSACTION:
TRANSACTION 1367965, ACTIVE 1 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 16 lock struct(s), heap size 992, 8 row lock(s), undo log entries 4
MySQL thread id 100, OS thread handle 0x1fbc, query id 825084 localhost ::1 root updating
UPDATE `products` SET `quantity`=1333 WHERE (`id` = 3355)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 2503 page no 212 n bits 96 index `PRIMARY` of table `emerx2`.`products` trx id 1367965 lock_mode X locks rec but not gap waiting
Record lock, heap no 22 PHYSICAL RECORD: n_fields 66; compact format; info bits 0
*** (2) TRANSACTION:
TRANSACTION 1367967, ACTIVE 1 sec fetching rows
mysql tables in use 1, locked 1
2231 lock struct(s), heap size 125816, 43033 row lock(s)
MySQL thread id 87, OS thread handle 0x3a98, query id 825080 localhost 127.0.0.1 root updating
UPDATE products SET updated = NOW(), active = 1, supplier_id = '109', delivery_date = '2019-02-21 16:00:43', quantity = '1' WHERE supplier_id = '109' AND domain_id = 16
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 2503 page no 212 n bits 96 index `PRIMARY` of table `emerx2`.`products` trx id 1367967 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
任何可能的解决方案?我不知道该怎么办,因为后续更新仅在表中没有任何共同点。
答案 0 :(得分:1)
只需拥有INDEX(supplier_id, domain_id)
。不要先获取ID;让MySQL负责。而且它将避免扫描表-容易导致死锁或延迟。
答案 1 :(得分:0)
问题在于,在更新命令中,我使用了2个非索引列。因此,现在我通过这两列选择主键,然后使用主键进行更新,它可以正常工作,而且出乎意料的是它要快得多。