mysql5.6查询死锁_MySQL 5.6死锁两次锁定同一行?

本文讨论了在MySQL 5.6中遇到的死锁问题,涉及事务尝试两次锁定同一行导致的死锁情况。内容包括了死锁的日志分析,解释了事务1等待和持有锁的状态,以及事务2持有锁并等待的情况。解决方案建议将查询与更新分开,并考虑启用innodb_locks_unsafe_for_binlog选项以提高并发性,但需要注意其对复制和时间点恢复的影响。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

I am seeing a deadlock with MySQL 5.6 because of what seems like trying to lock the same row/s twice.

From the snippet below, rows where id = (11, 12, 13, 14, 15) already have a lock. And when another transaction tried to acquire a lock on these, MySQL failed the transaction detecting a deadlock.

Is my reading of this correct? If so, is there anything in MySQL 5.6 to get over this? FWIW, the same code in 5.5 worked just fine (for several hundreds of iterations).

------------------------

LATEST DETECTED DEADLOCK

------------------------

2013-07-25 11:46:05 13a515000

*** (1) TRANSACTION:

TRANSACTION 2333130, ACTIVE 0 sec fetching rows

mysql tables in use 1, locked 1

LOCK WAIT 31 lock struct(s), heap size 6960, 6 row lock(s)

MySQL thread id 2944, OS thread handle 0x13ae88000, query id 184533 localhost 127.0.0.1 root Sending data

SELECT id FROM table_meta WHERE id IN (11, 12, 13, 14, 15) FOR UPDATE

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

RECORD LOCKS space id 128954 page no 5 n bits 176 index `PRIMARY` of table `db_test1`.`table_meta` trx id 2333130 lock_mode X locks rec but not gap waiting

*** (2) TRANSACTION:

TRANSACTION 2333255, ACTIVE 0 sec starting index read

mysql tables in use 1, locked 1

3 lock struct(s), heap size 1248, 11 row lock(s)

MySQL thread id 2927, OS thread handle 0x13a515000, query id 186769 localhost 127.0.0.1 root Sending data

SELECT id FROM table_meta WHERE id IN (1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15) FOR UPDATE

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

RECORD LOCKS space id 128954 page no 5 n bits 176 index `PRIMARY` of table `db_test1`.`table_meta` trx id 2333255 lock_mode X locks rec but not gap

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

RECORD LOCKS space id 128954 page no 5 n bits 176 index `PRIMARY` of table `db_test1`.`table_meta` trx id 2333255 lock_mode X locks rec but not gap waiting

*** WE ROLL BACK TRANSACTION (2)

解决方案

Sure,

Just sorted this for one of my clients in 5.6. Actually these are innodb deadlocks, select is followed by an update which is causing the deadlocks. Please update the query and do a separate update.

Do you have slave server ?

One more thing to keep into account – INSERT … SELECT also performs read in locking mode and so partially bypasses versioning and retrieves latest committed row. So even if you’re operation in REPEATABLE-READ mode, this operation will be performed in READ-COMMITTED

mode, potentially giving different result compared to what pure SELECT would give. This by the way applies to SELECT .. LOCK IN SHARE MODE and SELECT … FOR UPDATE as well.

One my ask what is if I’m not using replication and have my binary log disabled ? If replication is not used you can enable innodb_locks_unsafe_for_binlog option, which will relax locks which Innodb sets on statement execution, which generally gives better concurrency. However as the name says it makes locks unsafe fore replication and point in time recovery, so use innodb_locks_unsafe_for_binlog option with caution.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值