i couldn’t find a way to optimize the following Query:
SELECT *
FROM tbl
WHERE type='51' AND `start`<='2012-01-19'
ORDER BY end DESC
LIMIT 5
I’ve tried by indexing each column in a separate index (type,start,end), and all of them in the same index, but MySQL keeps telling me that needs to do a filesort
Is this query just impossible to optimize?
Yes, as long you have range comparison in
WHEREand sort by another field – mysql cannot use index for sorting.It could if you had
WHERE type='51' ANDstart='2012-01-19' ORDER BY end DESCorWHERE type='51' ANDstart<= '2012-01-19' ORDER BY start DESChttp://dev.mysql.com/doc/refman/5.5/en/order-by-optimization.html — and here is a chapter relevant to your problem