Mysql死锁,为什么此事务持有x锁并等待s锁?

时间:2019-01-22 06:10:38

标签: mysql database-deadlocks

我正在尝试更新表t_inventory_warehouse中的库存,收集每个仓库中的所有库存并将其放入表t_inventory中。

我不知道为什么这会导致死锁。我认为,当事务持有X锁时,无需询问S锁。

这是我来自SHOW ENGINE INNODB STATUS的日志,隔离是REPEATABLE-READ,而mysql版本是5.7.24。

------------------------
LATEST DETECTED DEADLOCK
------------------------
2019-01-22 03:47:58 0x7f5edd3d1700
*** (1) TRANSACTION:
TRANSACTION 1821172, ACTIVE 0 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s)
MySQL thread id 209322, OS thread handle 140045406635776, query id 13681842 192.168.1.26 root updating
update t_inventory_warehouse
     SET pure_amount =
        case

          when (id = 522846726673376 and pure_amount = 388.00) then 378.00

        end,
        amount = pure_amount + batch_amount,
        available_amount = amount - pre_order_count
    where
     (
      (id = 522846726673376 and pure_amount = 388.00 and (select @update_id := concat_ws(',', id, @update_id)))
     )
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 732 page no 3 n bits 80 index PRIMARY of table `inventory`.`t_inventory_warehouse` trx id 1821172 lock_mode X locks rec but not gap waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 21; compact format; info bits 0
 0: len 8; hex 0001db86bd6a53e0; asc      jS ;;
 1: len 6; hex 0000001bc9f3; asc       ;;
 2: len 7; hex 2e000001f02168; asc .    !h;;
 3: len 8; hex 0001db7f02e3ff60; asc        `;;
 4: len 8; hex 0001db7f09077080; asc       p ;;
 5: len 8; hex 0001dae0082aa080; asc      *  ;;
 6: len 11; hex 800000000000017a000000; asc        z   ;;
 7: len 11; hex 80000000000000ee000000; asc            ;;
 8: len 11; hex 800000000000017a000000; asc        z   ;;
 9: len 11; hex 8000000000000000000000; asc            ;;
 10: len 11; hex 800000000000008c000000; asc            ;;
 11: len 11; hex 8000000000000000000000; asc            ;;
 12: len 11; hex 8000000000000000000000; asc            ;;
 13: SQL NULL;
 14: SQL NULL;
 15: SQL NULL;
 16: len 8; hex 000000000000e71b; asc         ;;
 17: len 8; hex 0000000000002afa; asc       * ;;
 18: len 8; hex 0000000000000000; asc         ;;
 19: len 4; hex 5c36273f; asc \6'?;;
 20: len 4; hex 5c46926e; asc \F n;;

*** (2) TRANSACTION:
TRANSACTION 1821171, ACTIVE 0 sec fetching rows
mysql tables in use 2, locked 2
4 lock struct(s), heap size 1136, 4 row lock(s), undo log entries 1
MySQL thread id 209318, OS thread handle 140045415421696, query id 13681844 192.168.1.26 root Sending data
update t_inventory a join
    (select inventory_id, ifnull(sum(amount),0) amount, ifnull(sum(pre_order_count),0) pre_order_count,
    ifnull(sum(available_amount),0) available_amount
    from t_inventory_warehouse where inventory_id in
     (
      522813532536672
     )
    and tenant_id = 59163 and org_id = 11002 and chain_id = 0
    group by warehouse_id, inventory_id) b
    on a.id = b.inventory_id
    set a.amount = b.amount,
        a.pre_order_count = b.pre_order_count,
        a.available_amount = b.available_amount,

        a.cost_price_status = 1
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 732 page no 3 n bits 80 index PRIMARY of table `inventory`.`t_inventory_warehouse` trx id 1821171 lock_mode X locks rec but not gap
Record lock, heap no 4 PHYSICAL RECORD: n_fields 21; compact format; info bits 0
 0: len 8; hex 0001db86bd6a53e0; asc      jS ;;
 1: len 6; hex 0000001bc9f3; asc       ;;
 2: len 7; hex 2e000001f02168; asc .    !h;;
 3: len 8; hex 0001db7f02e3ff60; asc        `;;
 4: len 8; hex 0001db7f09077080; asc       p ;;
 5: len 8; hex 0001dae0082aa080; asc      *  ;;
 6: len 11; hex 800000000000017a000000; asc        z   ;;
 7: len 11; hex 80000000000000ee000000; asc            ;;
 8: len 11; hex 800000000000017a000000; asc        z   ;;
 9: len 11; hex 8000000000000000000000; asc            ;;
 10: len 11; hex 800000000000008c000000; asc            ;;
 11: len 11; hex 8000000000000000000000; asc            ;;
 12: len 11; hex 8000000000000000000000; asc            ;;
 13: SQL NULL;
 14: SQL NULL;
 15: SQL NULL;
 16: len 8; hex 000000000000e71b; asc         ;;
 17: len 8; hex 0000000000002afa; asc       * ;;
 18: len 8; hex 0000000000000000; asc         ;;
 19: len 4; hex 5c36273f; asc \6'?;;
 20: len 4; hex 5c46926e; asc \F n;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 732 page no 3 n bits 80 index PRIMARY of table `inventory`.`t_inventory_warehouse` trx id 1821171 lock mode S waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 21; compact format; info bits 0
 0: len 8; hex 0001db86bd6a53e0; asc      jS ;;
 1: len 6; hex 0000001bc9f3; asc       ;;
 2: len 7; hex 2e000001f02168; asc .    !h;;
 3: len 8; hex 0001db7f02e3ff60; asc        `;;
 4: len 8; hex 0001db7f09077080; asc       p ;;
 5: len 8; hex 0001dae0082aa080; asc      *  ;;
 6: len 11; hex 800000000000017a000000; asc        z   ;;
 7: len 11; hex 80000000000000ee000000; asc            ;;
 8: len 11; hex 800000000000017a000000; asc        z   ;;
 9: len 11; hex 8000000000000000000000; asc            ;;
 10: len 11; hex 800000000000008c000000; asc            ;;
 11: len 11; hex 8000000000000000000000; asc            ;;
 12: len 11; hex 8000000000000000000000; asc            ;;
 13: SQL NULL;
 14: SQL NULL;
 15: SQL NULL;
 16: len 8; hex 000000000000e71b; asc         ;;
 17: len 8; hex 0000000000002afa; asc       * ;;
 18: len 8; hex 0000000000000000; asc         ;;
 19: len 4; hex 5c36273f; asc \6'?;;
 20: len 4; hex 5c46926e; asc \F n;;

*** WE ROLL BACK TRANSACTION (1)

0 个答案:

没有答案