简体   繁体   English

MySQL查询在排序时使用Filesort尽管有索引

[英]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_1order by col_2, col_1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MySQL查询中使用OR时,有没有办法使用索引来避免文件排序? - Is there a way to use the index to avoid a filesort when OR is used in MySQL query? MySQL的选择查询是非常缓慢的,并使用文件排序 - mysql select query is very slow and uses filesort MySQL性能,子查询使用临时文件排序,当查询使用order by / group by时 - MySQL performance, subquery using temporary filesort when query uses order by/group by Mysql Filesort和临时时按索引列排序,而不是在pk上排序时 - MySql Filesort and temporary when order by indexed column and not when ordering on pk 使用concat和filesort时缓慢的mysql查询 - slow mysql query when using concat and filesort 如何在简单的MySQL查询中设计索引/查询以避免文件排序? - How to design an index/query to avoid filesort in a simple MySQL query? MySQL 查询在使用索引时变慢 - MySQL query gets slower when uses an index MySQL按查询分组,多个和不使用索引,滞后于使用文件排序 - MySQL group by query with multiple sums not using index, lagging on using filesort MySQL查询使用索引使用临时使用文件排序 - Mysql query using index using filesort using temporary 如何避免在MySQL综合索引上使用order by语句进行前缀查询的文件排序? - How to avoid a filesort on a mysql composite index for a prefix query with an order by statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM