2015-11-18 76 views
1

我使用批量插入时,桌子上面临僵局问题,查询低于如何避免死锁情况插入选择更新重复?

INSERT INTO registrationupdates (refreshtime,firstseentime,userdevice,isexist,userpart,expires,ipaddress,contacturi,deviceid) 
SELECT refreshtime,firstseentime,userdevice,isexist,userpart,expires,ipaddress,contacturi,(SELECT IFNULL(id,0) FROM devicedetails WHERE deviceip LIKE CONCAT('%',registrarip , '%')) 
FROM register_temp_table WHERE isexist=1 ON DUPLICATE KEY UPDATE `refreshtime`=VALUES(refreshtime),`firstseentime`=VALUES(firstseentime),`userdevice`=VALUES(userdevice),`isexist`=VALUES(isexist);  

我得到下面的错误消息

Deadlock found when trying to get lock; try restarting transaction 

“contacturi”是唯一的钥匙,而且索引上用户部分列在注册更新表上。

任何人都可以帮助我吗?

+0

参见[如何与应对死锁(http://dev.mysql.com/doc/refman/5.7/en/innodb-deadlocks.html解决您的问题) – Drew

回答

0

您可以将其分成两个查询,以避免在单个查询中插入和选择相同的表。

我认为它可能避免死锁条件

+0

这是怎么分开的? –

+0

请参阅此链接 http://stackoverflow.com/questions/2470185/how-to-avoid-deadlock-in-mysql。 –

+0

你有没有得到你所需要的? –