View Single Post

   
  #1 (permalink)  
Old 02-28-2008, 10:47 AM
nk
 
Posts: n/a
Default Indexes And Sorting

I'm running the following query on a table with an index on dateLast,
and one on status:

SELECT field FROM table
WHERE (DATEDIFF(NOW(), dateLast) > 7) AND (status = 1)
ORDER BY dateLast ASC
LIMIT 10

This query takes about 80 seconds, because MySQL uses the index on
'status', which means it has to sort the rows using filesort.

When I remove 'status' from the query:

SELECT field FROM table
WHERE (DATEDIFF(NOW(), dateLast) > 7)
ORDER BY dateLast ASC
LIMIT 10

The query doesn't even take a second, because MySQL can use the index
on dateLast to both check the where condition and sort the rows.

Any ideas? Is there a way to tell MySQL to order rows using a certain
index?

Reply With Quote