[英]MySQL query uses filesort despite index when ordering
Edit: I made query it simpler just to test: 编辑:我使查询更简单,只是为了测试:
select *
from table1 where date >= '2012-02-02' order by date, col_2 desc
I have composite index on date and col_2 however when I do explain on my query it shows: 我在日期和col_2上有综合索引,但是当我在查询中做解释时,它显示:
+----+-------------+------------------+-------+--------------------------+-----------------+---------+------+------+-----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+-------+--------------------------+-----------------+---------+------+------+-----------------------------+
| 1 | SIMPLE | table1 | range | col_2_date, date | col_2_date | 4 | NULL | 4643 | Using where; Using filesort |
+----+-------------+------------------+-------+--------------------------+-----------------+---------+------+------+-----------------------------+
Why does mySQL use filesort if I have index on the columns col_2 and date and how could I prevent it? 如果我在col_2和date列上有索引,为什么mySQL为什么使用filesort?如何防止它发生?
答案是按照您创建索引的顺序对结果进行排序。例如,如果索引为(col_1,col_2),则使用... order by col_1 desc, col_2 desc
或... order by col_1 asc, col_2 asc
和例如,不... order by col_1 asc, col_2 desc
order by col_2, col_1
或order by col_2, col_1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.