怎样解决Mysql死锁问题
创始人
2024-12-20 16:21:34
0次

show engine innodb status \G   点击(此处)折叠或打开

  1. *** (1) TRANSACTION:

  2. TRANSACTION 9CC3, ACTIVE 0 sec, OS thread id 2384 fetching rows

  3. mysql tables in use 2, locked 2

  4. LOCK WAIT 6 lock struct(s), heap size 1024, 12 row lock(s), undo log entries 2

  5. MySQL thread id 20, query id 138891 localhost 127.0.0.1 root Sending data

  6. DELETE FROM User WHERE ID IN (SELECT UserID FROM BusinessUser WHERE BusinessID=124001692)

  7. *** (1) WAITING FOR THIS LOCK TO BE GRANTED:

  8. RECORD LOCKS space id 0 page no 92015 n bits 88 index `PRIMARY` of table `df_demo`.`user` trx id 9CC3 lock_mode X waiting

  9. Record lock, heap no 14 PHYSICAL RECORD: n_fields 48; compact format; info bits 0

  10.  0: len 4; hex 80000788; asc ;;

  11.  

  12.  .......................................................................

  13.  

  14. *** (2) TRANSACTION:

  15. TRANSACTION 9CC0, ACTIVE 0 sec, OS thread id 1696 starting index read, thread declared inside InnoDB 370

  16. mysql tables in use 3, locked 3

  17. 9 lock struct(s), heap size 1024, 59 row lock(s), undo log entries 10

  18. MySQL thread id 21, query id 138887 localhost 127.0.0.1 root Sending data

  19. DELETE FROM UserOptions WHERE UserID IN (SELECT u.ID FROM User u JOIN BusinessUser bu ON u.ID=bu.UserID WHERE bu.BusinessID=124001691)

  20. *** (2) HOLDS THE LOCK(S):

  21. RECORD LOCKS space id 0 page no 92015 n bits 88 index `PRIMARY` of table `df_demo`.`user` trx id 9CC0 lock mode S locks rec but not gap

  22. Record lock, heap no 14 PHYSICAL RECORD: n_fields 48; compact format; info bits 0

  23.  0: len 4; hex 80000788; asc ;;

  24. ..........................................................................

  25.  

  26. *** (2) WAITING FOR THIS LOCK TO BE GRANTED:

  27. RECORD LOCKS space id 0 page no 92015 n bits 88 index `PRIMARY` of table `df_demo`.`user` trx id 9CC0 lock mode S locks rec but not gap waiting

  28. Record lock, heap no 11 PHYSICAL RECORD: n_fields 48; compact format; info bits 0

  29.  0: len 4; hex 80000786; asc ;;

  30.  

  31. ...........................................................................

  32.  

  33.  

  34. *** WE ROLL BACK TRANSACTION (1)

1:分析: 看看引起死锁的SQL执行计划:   点击(此处)折叠或打开

  1. mysql> explain select * FROM User WHERE ID IN (SELECT UserID FROM BusinessUser WHERE BusinessID=124001692);

  2. +----+--------------------+--------------+----------------+--------------------------------+-----------------+---------+-

  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

  4. +----+--------------------+--------------+----------------+--------------------------------+-----------------+---------+-

  5. | 1 | PRIMARY | User | ALL | NULL | NULL | NULL | NULL | 6095 | Using where |

  6. | 2 | DEPENDENT SUBQUERY | BusinessUser | index_subquery | idx_UserID_Type,idx_BusinessID | idx_UserID_Type | 5 | func | 1 | Using where |

  7. +----+--------------------+--------------+----------------+--------------------------------+-----------------+---------+-

  8. 2 rows in set (0.00 sec)

User表全表扫描,会引起死锁。   2:解决 重写SQL,把IN改成JOIN,执行计划如下:   点击(此处)折叠或打开

  1. mysql> explain select a.* FROM User a inner join BusinessUser b on a.id=b.userid WHERE b.BusinessID=124001692;

  2. +----+-------------+-------+--------+--------------------------------+----------------+---------+--------------+------+

  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

  4. +----+-------------+-------+--------+--------------------------------+----------------+---------+--------------+------+

  5. | 1 | SIMPLE | b | ref | idx_UserID_Type,idx_BusinessID | idx_BusinessID | 5 | const | 1 | Using where |

  6. | 1 | SIMPLE | a | eq_ref | PRIMARY | PRIMARY | 4 | ng1.b.UserID | 1 | |

  7. +----+-------------+-------+--------+--------------------------------+----------------+---------+--------------+------+

  8. 2 rows in set (0.00 sec)

两表都用到索引了。  

问题得到解决。

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...