I have a query with an ORDER BY clause which uses a column which is the last column on an index which is being used in the WHERE clause, essentially of the form:
SELECT cols FROM tables WHERE col_1 = x AND col_2 = y AND col_3 = z ORDER BY col_4
and the index is created on columns (col_1, col_2, col_3, col_4) in that order.
When I profile the query over 99% of the time is spent in the "Sorting result" state. col_4 is a timestamp column if that makes any difference. I understand that ORDER BY can only use an index under certain circumstances, but I'm still a bit mystified as to precisely when the optimiser would do so.
