在某些状况下,MySQL可使用索引来知足子句,并避免执行操做时涉及的额外排序。即便与索引不彻底匹配,也可使用索引,只要索引的全部未使用部分和全部额外列都是子句中的常量。若是索引不包含查询访问的全部列,则仅当索引访问比其余访问方法代价低时才使用索引。也就是说若是查询的结果中包含了索引中包含的字段之外的字段时则不使用索引。mysql
DROP TABLE IF EXISTS `t_orderby`; CREATE TABLE `t_orderby` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT NULL, `address` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `idx_name_age`(`name`, `age`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of t_orderby -- ---------------------------- INSERT INTO `t_orderby` VALUES (1, 'zhangsan', 18, '北京'); INSERT INTO `t_orderby` VALUES (2, 'zhangsan2', 20, '北京'); INSERT INTO `t_orderby` VALUES (3, 'zhangsan3', 23, '北京'); INSERT INTO `t_orderby` VALUES (4, 'zhangsan4', 13, '北京'); INSERT INTO `t_orderby` VALUES (5, 'zhangsan5', 19, '北京'); INSERT INTO `t_orderby` VALUES (6, 'zhangsan6', 30, '北京'); INSERT INTO `t_orderby` VALUES (7, 'lisi1', 30, '北京'); INSERT INTO `t_orderby` VALUES (8, 'lisi2', 32, '北京'); INSERT INTO `t_orderby` VALUES (9, 'lisi3', 12, '北京'); INSERT INTO `t_orderby` VALUES (10, 'lisi4', 19, '北京'); INSERT INTO `t_orderby` VALUES (11, 'wangwu', 14, '北京'); INSERT INTO `t_orderby` VALUES (12, 'wangwu2', 18, '北京'); INSERT INTO `t_orderby` VALUES (13, 'wangwu3', 17, '北京'); INSERT INTO `t_orderby` VALUES (14, 'wangwu4', 47, '北京'); INSERT INTO `t_orderby` VALUES (15, 'wangwu5', 37, '北京'); INSERT INTO `t_orderby` VALUES (16, 'wangwu6', 38, '北京'); 复制代码
mysql> explain select * from t_orderby order by name \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_orderby
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 2090301
filtered: 100.00
Extra: Using filesort
1 row in set, 1 warning (0.00 sec)
复制代码
因为select中使用了*
须要查询表中的全部字段,因为address
字段是不包含在idx_name_age
索引中因此没法使用索引,此时须要进行文件排序,即便Extra: Using filesort
。sql
mysql> explain select id,name,age from t_orderby order by name \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_orderby
partitions: NULL
type: index
possible_keys: NULL
key: idx_name_age
key_len: 38
ref: NULL
rows: 2090301
filtered: 100.00
Extra: Using index
1 row in set, 1 warning (0.00 sec)
复制代码
此时可使用索引,由于查询的字段中包含的都包含在idx_name_age
索引中。markdown
mysql> explain select id,name,age from t_orderby order by age \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_orderby
partitions: NULL
type: index
possible_keys: NULL
key: idx_name_age
key_len: 38
ref: NULL
rows: 2090301
filtered: 100.00
Extra: Using index; Using filesort
1 row in set, 1 warning (0.00 sec)
复制代码
此时排序也可使用索引,因为查询的字段都是包含在索引中,虽然排序的字段并不是索引中的第一个字段,可是使用索引对age进行排序的代价仍是比纯使用文件排序要小,因此可使用索引。可是不能彻底依赖索引仍是须要文件排序来辅助。spa
mysql> explain select * from t_orderby where name='zhangsan' order by age \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_orderby
partitions: NULL
type: ref
possible_keys: idx_name_age
key: idx_name_age
key_len: 33
ref: const
rows: 277562
filtered: 100.00
Extra: Using index condition
1 row in set, 1 warning (0.00 sec)
复制代码
此语句虽然在select后使用了*
查询的字段并不是索引中包含的所有字段,可是where条件中使用了name
,order by中使用了age
是符合索引条件下推的,能够直接将须要的记录查询出来而且排序便可。可是这个过程须要进行回表操做。code
索引中断不能使用索引:orm
mysql> explain select * from t_orderby where name>'zhangsan' order by age \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t_orderby
partitions: NULL
type: ALL
possible_keys: idx_name_age
key: NULL
key_len: NULL
ref: NULL
rows: 2090301
filtered: 50.00
Extra: Using where; Using filesort
1 row in set, 1 warning (0.00 sec)
复制代码
在order by
子句中可否使用索引须要考虑两个问题就能够了。第一个就是select后面的字段列表是否能够直接从索引中得到所有字段,另一个就是where条件是否可使用索引来限定一个范围,有了这个查询范围即使是再次回表代价也是能够接受的。排序