spring 从InnoDB引擎状态了解MySQL死锁日志

anauzrmj  于 2023-06-28  发布在  Spring
关注(0)|答案(1)|浏览(110)

昨天在我的生产中,我的应用程序遇到了与死锁相关的问题,
我使用InnoDB引擎状态来检查哪个查询生成了死锁,但我无法理解为什么会生成死锁。
有2个线程同时运行。

  • 第一个线程尝试从temp table执行查询replace into aggregate
  • 第2个线程尝试delete record from aggregate

有人能帮助我如何重现相同的问题,或者是什么导致了这个问题。
下面是发动机状态

------------------------
LATEST DETECTED DEADLOCK
------------------------
2018-06-27 03:23:08 0x20ec
*** (1) TRANSACTION:
TRANSACTION 82199210, ACTIVE 59 sec fetching rows
mysql tables in use 2, locked 2
LOCK WAIT 16 lock struct(s), heap size 1136, 618 row lock(s)
MySQL thread id 94048, OS thread handle 13292, query id 1096831619 localhost 127.0.0.1 cpro Sending data
DELETE aggr FROM aggregate aggr INNER JOIN product p ON aggr.product_fk=p.product_pk WHERE p.id=2 AND date BETWEEN '2018-04-27 00:00:00' AND '2018-06-26 23:59:59.999'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 45178 page no 57766 n bits 576 index UNIQUE_aggregate of table `data`.`aggregate` trx id 82199210 lock_mode X locks rec but not gap waiting
Record lock, heap no 447 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 3; hex 8fc49b; asc    ;;
 1: len 4; hex 8000000d; asc     ;;
 2: len 8; hex 6172726976616c73; asc arrivals;;
 3: len 4; hex 0000803f; asc    ?;;
 4: len 6; hex 00001a2b5ff2; asc    +_ ;;

*** (2) TRANSACTION:
TRANSACTION 82197992, ACTIVE 499 sec inserting
mysql tables in use 2, locked 1
2251 lock struct(s), heap size 237776, 30436 row lock(s), undo log entries 24811
MySQL thread id 94049, OS thread handle 8428, query id 1096943836 localhost 127.0.0.1 cpro Creating sort index
replace into aggregate SELECT  date, PRODUCT_FK, DATA_POINT, LOS, SUM(`HOUR`) FROM aggregate_temp GROUP BY date, PRODUCT_FK, DATA_POINT, LOS
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 45178 page no 57766 n bits 576 index UNIQUE_aggregate of table `data`.`aggregate` trx id 82197992 lock_mode X locks rec but not gap
Record lock, heap no 447 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 3; hex 8fc49b; asc    ;;
 1: len 4; hex 8000000d; asc     ;;
 2: len 8; hex 6172726976616c73; asc arrivals;;
 3: len 4; hex 0000803f; asc    ?;;
 4: len 6; hex 00001a2b5ff2; asc    +_ ;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 45178 page no 57766 n bits 576 index UNIQUE_aggregate of table `data`.`aggregate` trx id 82197992 lock_mode X waiting
Record lock, heap no 447 PHYSICAL RECORD: n_fields 5; compact format; info bits 32
 0: len 3; hex 8fc49b; asc    ;;
 1: len 4; hex 8000000d; asc     ;;
 2: len 8; hex 6172726976616c73; asc arrivals;;
 3: len 4; hex 0000803f; asc    ?;;
 4: len 6; hex 00001a2b5ff2; asc    +_ ;;

*** WE ROLL BACK TRANSACTION (1)
------------
TRANSACTIONS
------------
Trx id counter 82207138
Purge done for trx's n:o < 82207138 undo n:o < 0 state: running but idle
History list length 2996
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 281885670630400, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
wz3gfoph

wz3gfoph1#

执行SQL时锁定。
看这个例子。
delete sql将删除记录1和3,replace sql将更新3和1。
如果执行序列像这样:

  1. tr1删除记录% 1。
  2. tr2替换记录3。
  3. tr1尝试删除记录3并等待tr2释放锁定。
  4. tr2尝试替换记录1并检测死锁。
    您的删除和替换涉及的记录太多,可能会出现上述情况。

相关问题