Welcome

首页 / 数据库 / MySQL / MySQL索引与性能(4) 排序

MySQL索引与性能(4) 排序2014-05-15 csdn博客 Zhu_Julian我们知道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,statusG*************************** 1. row *************************** id: 1select_type: SIMPLEtable: users type: indexpossible_keys: NULLkey: login_idkey_len: 387ref: NULL rows: 5894108Extra: Using index1 row in set (0.00 sec)root@test 05:03:17>explain select id from users order by statusG*************************** 1. row *************************** id: 1select_type: SIMPLEtable: users type: indexpossible_keys: NULLkey: login_idkey_len: 387ref: NULL rows: 5894108Extra: 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,statusG*************************** 1. row *************************** id: 1select_type: SIMPLEtable: users type: indexpossible_keys: NULLkey: login_idkey_len: 387ref: NULL rows: 5894108Extra: 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 descG*************************** 1. row *************************** id: 1select_type: SIMPLEtable: users type: indexpossible_keys: NULLkey: login_idkey_len: 387ref: NULL rows: 5894108Extra: Using index1 row in set (0.00 sec)
注:如果在Extra列出现using filesort,表示无法按索引顺序扫描。