I’m trying to sort out some of my slowest queries, and here is one that takes some time, partially because it’s a bit slow, but also because I run it quite often.
Anyway, my table site_articles has seperate indexes on approved (int), type (int) and created (datetime), and the only one used is created. There is however a full tablescan occurring…
SELECT id, title, created, headline FROM site_articles WHERE approved=1 AND type=0 AND id <> 14277 AND DATE(created) = DATE(‘20100225’) ORDER BY created DESC LIMIT 15
Is there any way I can improve this one? Note that I can pass the date ‘20100225’ in any format, so I can change that if necessary.
Ah yes found a solution!
Query execution down from 0.27s avg to 0.0016s !
SELECT id, title, created, headline
FROM site_articles
WHERE approved =1
AND TYPE =0
AND id <>14277
AND created
BETWEEN TIMESTAMP( '20100225000000' )
AND TIMESTAMP( '20100225235959' )
ORDER BY created DESC
LIMIT 15
Removed the requirement to perform DATE(…) on every row. Thanks for the hint on functions in WHERE, made me think