Question

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.

Was it helpful?

Solution

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'

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top