删除然后插入innodb上的事务死锁

时间:2015-09-21 03:12:23

标签: mysql sql innodb

我的表格方案如下所示

CREATE TABLE `mapping` (
   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
   `id1` bigint(20) unsigned NOT NULL,
   `id2` bigint(20) unsigned NOT NULL,
   PRIMARY KEY (`id`),
   UNIQUE KEY `id_UNIQUE` (`id1`, `id2`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

然后我开始一个交易,然后得到一个错误:

begin transaction
delete from mapping where id1=xxx and id2 IN (...)
insert into mapping (id1,id2) values(?,?),(?,?)...
end
Lock wait timeout exceeded;try restarting transaction.

似乎delete& insert中的操作具有相同的记录,innodb将具有行锁死锁,在这种情况下优雅的方法是什么

0 个答案:

没有答案