4

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.

2 Answers 2

3

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 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? Commented Dec 7, 2009 at 13:31
  • 1
    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. Commented Dec 7, 2009 at 13:40
1

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.

1
  • Yes, in my case it was the parallellization that made this error occur. When running a single instance it was never an issue. Commented Dec 7, 2009 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.