order by city_id (primary key index)
order by country_id (no indexing!!!)
認(rèn)證0級(jí)講師
Your city_id is the primary key, and country_id is the composite primary key.
changed to
SELECT * FROM city FORCE INDEX(idx_fk_country_id) ORDER BY country_id;
That’s it, you’re forcing the use of an index in SELECT
中查詢了索引建以外的列,那么ORDER BY
就不會(huì)使用索引了。你可以用FORCE INDEX
.
Another point is the so-called covering index. The definition of a covering index is: MySQL can return the select
field based on the index without querying the file again based on the index to get the result.
What happens when you use select *
時(shí),你沒有強(qiáng)制指定索引,那么mysql
為了得到你的查詢的字段而查詢文件,然后再進(jìn)行排序操作,這就沒有用到覆蓋索引。而你使用了force index
就會(huì)強(qiáng)制使用覆蓋索引,這樣就不會(huì)出現(xiàn)filesort
.