2015-11-18 74 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

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