Deleting rows cause lock timeout

天大地大妈咪最大 提交于 2019-12-10 16:37:36

问题


I keep getting these errors when trying to delete rows from a table. The special case here is that I may be running 5 processes at the same time.

The table itself is an Innodb table with ~4.5 million rows. I do not have an index on the column used in my WHERE clause. Other indices are working as supposed to.

It's being done within a transcation, first I delete records, then I insert replacing records, and only if all records are inserted should the transaction be commited.

Error message:

Query error: Lock wait timeout exceeded; try restarting transaction while executing DELETE FROM tablename WHERE column=value

Would it help to create an index on the referenced column here? Should I explicitly lock the rows?

I have found some additional information in question #64653 but I don't think it covers my situation fully.

Is it certain that it is the DELETE statement that is causing the error, or could it be other statements in the query? The DELETE statement is the first one so it seems logical but I'm not sure.


回答1:


An index would definitely help. If you are trying to replace deleted records I would recommend you modify your query to use an update instead of a DELETE followed by an INSERT, if possible:

INSERT INTO tableName SET
column2 = 'value2'
WHERE column = value
ON DUPLICATE KEY UPDATE
column2 = 'value2'



回答2:


An index definitely helps. I once worked on a DB containing user data. There was sometimes a problem with the web front end and user deletion. During the day it worked fine (although it took quite long). But in the late afternoon it sometimes timed out, because the DB server was under more load due to end of day processing. Whacked an index on the affected column and everything ran smoothly from there on.



来源:https://stackoverflow.com/questions/1859947/deleting-rows-cause-lock-timeout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!