MySQL索引与性能(4) 排序 - MySQL教程
发布时间:2014-05-15 17:39:03 所属栏目:MySql教程 来源:站长网
导读:我们知道B树索引是有序的,那么可不可以通过只扫描索引就能完成order by操作呢?答案是肯定的,但条件也比较 苛刻:只有当索引的列顺序和order by字句的列顺序
我们知道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: 1 select_type: SIMPLE table: users type: index possible_keys: NULL key: login_id key_len: 387 ref: NULL rows: 5894108 Extra: Using index 1 row in set (0.00 sec) root@test 05:03:17>explain select id from users order by statusG *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: index possible_keys: NULL key: login_id key_len: 387 ref: NULL rows: 5894108 Extra: Using index; Using filesort 1 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: 1 select_type: SIMPLE table: users type: index possible_keys: NULL key: login_id key_len: 387 ref: NULL rows: 5894108 Extra: Using index; Using filesort 1 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: 1 select_type: SIMPLE table: users type: index possible_keys: NULL key: login_id key_len: 387 ref: NULL rows: 5894108 Extra: Using index 1 row in set (0.00 sec) 注:如果在Extra列出现using filesort,表示无法按索引顺序扫描。 查看本栏目更多精彩内容:http://www.bianceng.cn/database/MySQL/ (编辑:佛山站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |