修改 我让查询更简单,只是为了测试:
select *
from table1 where date >= '2012-02-02' order by date, col_2 desc
我在日期和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 |
+----+-------------+------------------+-------+--------------------------+-----------------+---------+------+------+-----------------------------+
如果我在列col_2和date上有索引,为什么mySQL会使用filesort?如何防止它?
答案 0 :(得分:1)
答案是按照您创建索引的顺序订购结果。
如果index是(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
。