[MySQL] 索引与性能(4)- 排序

来源:互联网 发布:linux redis 批量删除 编辑:程序博客网 时间:2024/06/11 13:11

我们知道B树索引是有序的,那么可不可以通过只扫描索引就能完成order by操作呢?答案是肯定的,但条件也比较苛刻:只有当索引的列顺序和order by字句的列顺序完全一致,且order by字句中所有列的排序方式要么全部都是ASC,要么全部都是DESC,MySQL才能使用索引来对结果进行排序;如果查询需要关联多个表,则条件更苛刻,只有当order by字句中的列全部为驱动表(执行计划中)时,才能使用索引做排序。

下面我们来看一些例子:假设users表上有索引(login_id,status)

root@test 05:03:12>explain select id from users order by login_id,status\G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: users         type: indexpossible_keys: NULL          key: login_id      key_len: 387          ref: NULL         rows: 5894108        Extra: Using index1 row in set (0.00 sec)root@test 05:03:17>explain select id from users order by status\G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: users         type: indexpossible_keys: NULL          key: login_id      key_len: 387          ref: NULL         rows: 5894108        Extra: Using index; Using filesort1 row in set (0.00 sec)root@test 05:03:26>explain select id from users order by login_id desc,status\G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: users         type: indexpossible_keys: NULL          key: login_id      key_len: 387          ref: NULL         rows: 5894108        Extra: Using index; Using filesort1 row in set (0.00 sec)root@test 05:04:03>explain select id from users order by login_id desc,status desc\G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: users         type: indexpossible_keys: NULL          key: login_id      key_len: 387          ref: NULL         rows: 5894108        Extra: Using index1 row in set (0.00 sec)
注:如果在Extra列出现using filesort,表示无法按索引顺序扫描。