mysql多列排序

来源:互联网 发布:网络安全管理责任书 编辑:程序博客网 时间:2024/06/05 06:09

排序字段应保持一样的顺序(逆序或者降序),则使用索引排序。否则将使用文件排序

mysql> explain select id,time from edu_text  order by count desc, time desc ;
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
| id | select_type | table    | type  | possible_keys | key   | key_len | ref  | rows | Extra       |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
|  1 | SIMPLE      | edu_text | index | NULL          | count | 8       | NULL |    5 | Using index |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------------+
1 row in set (0.00 sec)


mysql> explain select id,time from edu_text  order by count desc, time asc ;
+----+-------------+----------+-------+---------------+-------+---------+------+------+-----------------------------+
| id | select_type | table    | type  | possible_keys | key   | key_len | ref  | rows | Extra                       |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-----------------------------+
|  1 | SIMPLE      | edu_text | index | NULL          | count | 8       | NULL |    5 | Using index; Using filesort |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-----------------------------+
1 row in set (0.00 sec)