sql UPDATE ON DUPLICATE KEY造成MYSQL死锁现场

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql UPDATE ON DUPLICATE KEY造成MYSQL死锁现场相关的知识,希望对你有一定的参考价值。

插入不同记录:原生MySQL 5.5.43 InnoDB

------------------------
LATEST DETECTED DEADLOCK
------------------------
150522 16:34:11
*** (1) TRANSACTION:
TRANSACTION 1304, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 6 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 135, OS thread handle 0x7f4ddc296700, query id 3809 172.27.2.235 root update
INSERT INTO mc_msg_session(sender_id, receiver_id, msg_type_id, merge_target_id, unread_count, msg_count, latest_msg_time, created) VALUES (0, 5281127, 1, 0, 0, 0, '2014-07-10 11:28:02', NOW()) ON DUPLICATE KEY UPDATE sender_id = 0,receiver_id = 5281127, msg_type_id = 1, merge_target_id = 0, latest_msg_time = '2014-07-10 11:28:02'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 595 n bits 80 index `uk_receiver_msgTpl_mergeTarget` of table `movie_message_center`.`mc_msg_session` trx id 1304 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 8 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 4; hex 83ca3346; asc   3F;;
 1: len 4; hex 80000001; asc     ;;
 2: len 4; hex 80000000; asc     ;;
 3: len 8; hex 8000000000000066; asc        f;;

*** (2) TRANSACTION:
TRANSACTION 1305, ACTIVE 0 sec inserting
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 140, OS thread handle 0x7f4ddc1d2700, query id 3811 172.27.2.235 root update
INSERT INTO mc_msg_session(sender_id, receiver_id, msg_type_id, merge_target_id, unread_count, msg_count, latest_msg_time, created) VALUES (0, 62266090, 1, 0, 0, 0, '2014-07-10 15:06:17', NOW()) ON DUPLICATE KEY UPDATE sender_id = 0,receiver_id = 62266090, msg_type_id = 1, merge_target_id = 0, latest_msg_time = '2014-07-10 15:06:17'
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 595 n bits 80 index `uk_receiver_msgTpl_mergeTarget` of table `movie_message_center`.`mc_msg_session` trx id 1305 lock_mode X locks gap before rec
Record lock, heap no 8 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 4; hex 83ca3346; asc   3F;;
 1: len 4; hex 80000001; asc     ;;
 2: len 4; hex 80000000; asc     ;;
 3: len 8; hex 8000000000000066; asc        f;;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 595 n bits 80 index `uk_receiver_msgTpl_mergeTarget` of table `movie_message_center`.`mc_msg_session` trx id 1305 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 8 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
 0: len 4; hex 83ca3346; asc   3F;;
 1: len 4; hex 80000001; asc     ;;
 2: len 4; hex 80000000; asc     ;;
 3: len 8; hex 8000000000000066; asc        f;;

*** WE ROLL BACK TRANSACTION (2)








插入同一记录:Percona Server 5.6:
------------------------
LATEST DETECTED DEADLOCK
------------------------
2015-05-25 11:11:36 7fe9b83ef700
*** (1) TRANSACTION:
TRANSACTION 8500, ACTIVE 6 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1184, 3 row lock(s), undo log entries 1
MySQL thread id 88, OS thread handle 0x7fe9bb22c700, query id 1985 172.27.2.235 root update
INSERT INTO mc_msg_session(sender_id, receiver_id, msg_type_id, merge_target_id, unread_count, msg_count, latest_msg_time, created) VALUES (5281127, 2228113, 5, 18, 0, 0, '2014-07-02 13:33:59', NOW()) ON DUPLICATE KEY UPDATE sender_id = 5281127,receiver_id = 2228113, msg_type_id = 5, merge_target_id = 18, latest_msg_time = '2014-07-02 13:33:59'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 595 n bits 80 index `uk_receiver_msgTpl_mergeTarget` of table `movie_message_center`.`mc_msg_session` trx id 8500 lock_mode X locks gap before rec insert intention waiting
*** (2) TRANSACTION:
TRANSACTION 8501, ACTIVE 6 sec inserting
mysql tables in use 1, locked 1
5 lock struct(s), heap size 1184, 3 row lock(s), undo log entries 1
MySQL thread id 90, OS thread handle 0x7fe9b83ef700, query id 1987 172.27.2.235 root update
INSERT INTO mc_msg_session(sender_id, receiver_id, msg_type_id, merge_target_id, unread_count, msg_count, latest_msg_time, created) VALUES (5281127, 2228113, 5, 18, 0, 0, '2014-07-02 13:34:41', NOW()) ON DUPLICATE KEY UPDATE sender_id = 5281127,receiver_id = 2228113, msg_type_id = 5, merge_target_id = 18, latest_msg_time = '2014-07-02 13:34:41'
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 595 n bits 80 index `uk_receiver_msgTpl_mergeTarget` of table `movie_message_center`.`mc_msg_session` trx id 8501 lock_mode X locks gap before rec
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 595 n bits 80 index `uk_receiver_msgTpl_mergeTarget` of table `movie_message_center`.`mc_msg_session` trx id 8501 lock_mode X locks gap before rec insert intention waiting
*** WE ROLL BACK TRANSACTION (2)

以上是关于sql UPDATE ON DUPLICATE KEY造成MYSQL死锁现场的主要内容,如果未能解决你的问题,请参考以下文章

mysql 插入更新在一条sql ON DUPLICATE KEY UPDATE

python 如何使用SQL Alchemy编译INSERT ... ON DUPLICATE KEY UPDATE并支持批量插入。

相当于 Sql Server 中的 MySQL ON DUPLICATE KEY UPDATE

SQL中的ON DUPLICATE KEY UPDATE使用详解

SQL Server 是不是提供类似于 MySQL 的 ON DUPLICATE KEY UPDATE 的功能

MySQL ON DUPLICATE KEY UPDATE 在单个查询中插入多行