views:

188

answers:

2

I have to delete about 10K rows from a table that has more than 100 million rows based on some criteria. When I execute the query, it takes about 5 minutes. I ran an explain plan (the delete query converted to select * since MySQL does not support explain delete) and found that MySQL uses the wrong index.

My question is: is there any way to tell MySQL which index to use during delete? If not, what ca I do? Select to temp table then delete from temp table?

Thank you!

+1  A: 

There is index hint syntax. //ETA: sadly, not for deletes

ETA: Have you tried running ANALYZE TABLE $mytable?

If that doesn't pay off, I'm thinking you have 2 choices: Drop the offending index before the delete and recreate it after. Or JOIN your delete table to another table on the desired index which should ensure that the desired index is used.

dnagirl
Yes, but to my understanding index hint does not work for delete statements.
Manuel Darveau
@Manuel Darveau: Drat! just tested it. You are correct.
dnagirl
+1  A: 

I've never really come across a situation where MySQL chose the wrong index, but rather my understanding of how indexes worked was usually at fault.

You might want to check out this book: http://oreilly.com/catalog/9780596003067

It has a great section on how indexes work and other tuning options.

UltimateBrent
+1, the book opens your eyes on many things you would never think about.
newtover
Editing, dnagirl is right about index hint syntax, I didn't know that went live.
UltimateBrent