Starting point: as I understand, MySQL will implicitly add the primary key at the end of every secondary index.
I have a table with:
- a primary index on
id
- a secondary index on
project_id
So, I thought MySQL would would use the secondary index project_id
for the following query, without needing a filesort:
SELECT * FROM db.table_name WHERE project_id = 5 ORDER BY `id` LIMIT 1;
However, explain shows me that PRIMARY is being used as the key:
If I use force_index on the project_id
column, the index is used, but the (estimated) number of estimated rows and 'using filesort' shows me that the primary key is not being used for the order by.
SELECT * FROM db.table_name FORCE INDEX (`project_id`) WHERE project_id = 5 ORDER BY `id` LIMIT 1;
It's a MyISAM table, I don't know if that matters here...
So: why isn't the filesort avoided? What am I missing?
Note: Database and table names are edited.