2

编辑: 我让查询变得更简单只是为了测试:

select *
from table1 where date >= '2012-02-02' order by date, col_2 desc

我在 date 和 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 使用文件排序,我该如何防止它?

4

1 回答 1

1

答案是按照您创建索引的相同顺序对结果进行排序..例如,如果索引是 (col_1, col_2) 然后使用... order by col_1 desc, col_2 descor... order by col_1 asc, col_2 asc而不是... order by col_1 asc, col_2 descororder by col_2, col_1例如。

于 2012-11-22T16:53:55.547 回答