MySQL has had an interesting optimization for years now1, which has popped up from time to time: in certain circumstances, it may choose to use an index that is index-wise less efficient, but provides the resulting rows in order, to avoid a filesort of the result.
What does this typically look like in production? A query that
seems simple and easy takes much longer than it should,
sometimes. (Perhaps in production, the query gets killed by
pt-kill
or exceeds the
max_execution_time
provided.) The query could be
very simple indeed:
SELECT ... WHERE `other_id` = 555 ORDER BY `id` ASC LIMIT 1
There’s an index on other_id
, and running the query
with an appropriate USE INDEX
, the query is fast.
Even weirder, changing the query to use LIMIT 10
causes it to …