views:

169

answers:

2

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.

+3  A: 

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'
cballou
An update is not possible. The replacing records could be fewer/more. What does the index help with? Decreasing the execution time, in turn decreasing probability of lock timeout occuring?
Peter Lindqvist
Ah, I see. You nailed it in regards to decreased execution time. With 4.5 million rows I'm guessing you are experiencing some slow queries. Try running a sample select statement based on one of your column ids to see just how slow it is to retrieve your records without an index. Then go back through and add an index and use that for a comparison. If the column is a fairly large CHAR/VARCHAR you can reduce some overhead by only using a fraction of the field for your index, i.e. **CREATE INDEX partial_idx ON tableName (column(10))** to only use the first 10 characters as your index value.
cballou
It works pretty good with this change. Thank you!
Peter Lindqvist
+1  A: 

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.

Michael
Yes, in my case it was the parallellization that made this error occur. When running a single instance it was never an issue.
Peter Lindqvist