+ -+ -+ -+ + + -+ + -+ + +
|1 | SIMPLE| t| ref| idx_t_c1234| idx_t_c1234 | 11| const |1 | Using index condition; Using where; Using temporary; Using filesort |
key_len: 11 // CHAR(10)*1 + 1 * NULL:说明全用到了c1索引,且都是等值查询的索引:c1
Using temporary:DISTINCT , 或者使用了不同的ORDER BY 和GROUP BY 列,且没用到索引 , 才会用临时表来排序 , 该临时表是内存临时表 , 还不是最糟糕的 , 最怕的是Using disk temporary
Using filesort:当我们试图对一个没有索引的字段进行排序时 , 就是filesoft
c3,c2由于与(c1,c2,c3,c4)索引不连续 , 无法用到索引排序
删除了复合索引后:只用到了c1索引 , 也就是只用一个索引 , 其它索引也没用上 , group by 也没用上
explain select * from t where c1= a1 and c4= c4 group by c3,c2;
+ -+ -+ -+ + -+ -+ + -+ + +
| id | select_type | table | type | possible_keys| key| key_len | ref| rows | Extra|
+ -+ -+ -+ + -+ -+ + -+ + +
|1 | SIMPLE| t| ref| idx_t_c1,idx_t_c4 | idx_t_c4 | 11| const |1 | Using index condition; Using where; Using temporary; Using filesort |
+ -+ -+ -+ + -+ -+ + -+ + +
d:
explain select * from t where c1= a1 and c4= c4 group by c2,c3;
+ -+ -+ -+ + + -+ + -+ + +
| id | select_type | table | type | possible_keys | key| key_len | ref| rows | Extra|
+ -+ -+ -+ + + -+ + -+ + +
|1 | SIMPLE| t| ref| idx_t_c1234| idx_t_c1234 | 11| const |1 | Using index condition; Using where |
+ -+ -+ -+ + + -+ + -+ + +
c2,c3用到了(c1,c2,c3,c4)索引排序 , 与c1相连
e:
explain select * from t where c1= a3 and c5= a5 order by c2,c3;
+ -+ -+ -+ + + -+ + -+ + +
| id | select_type | table | type | possible_keys | key| key_len | ref| rows | Extra|
+ -+ -+ -+ + + -+ + -+ + +
|1 | SIMPLE| t| ref| idx_t_c1234| idx_t_c1234 | 11| const |1 | Using index condition; Using where |
+ -+ -+ -+ + + -+ + -+ + +
key_len: 11 // CHAR(10)*1 + 1 * NULL:说明全用到了c1索引,且都是等值查询的索引:c1
f:
explain select * from t where c1= a1 and c2= a2 and c5= a5 order by c2,c3;
+ -+ -+ -+ + + -+ + -+ + +
| id | select_type | table | type | possible_keys | key| key_len | ref| rows | Extra|
+ -+ -+ -+ + + -+ + -+ + +
|1 | SIMPLE| t| ref| idx_t_c1234| idx_t_c1234 | 22| const,const |1 | Using index condition; Using where |
+ -+ -+ -+ + + -+ + -+ + +
key_len: 11 // CHAR(10)*2 + 2 * NULL:说明全用到了c1索引,且都是等值查询的索引:c1,c2
group by 中能通过索引避免排序的原理:
explain select * from t where c1= a1 and c4= c4 group by c3,c2;
explain select * from t where c1= a1 and c2= b2 and c4= b4 order by c3;
where条件只是过虑数据 , 在过滤的过程中 , 如果c3,c2有索引 , 就可直接使用
在查找的过程中 , 己可得到c3在一起的数据 , 此时可以sum,avg等 , 不用排序了
删除了复合索引后:只用到了c1索引 , 也就是只用一个索引 , 其它索引也没用上 , order by也没用上
explain select * from t where c1= a1 and c2= a2 and c5= a5 order by c2,c3;
+ -+ -+ -+ + -+ -+ + -+ + -+
| id | select_type | table | type | possible_keys| key| key_len | ref| rows | Extra|
+ -+ -+ -+ + -+ -+ + -+ + -+
|1 | SIMPLE| t| ref| idx_t_c1,idx_t_c2 | idx_t_c1 | 11| const |2 | Using index condition; Using where; Using filesort |
+ -+ -+ -+ + -+ -+ + -+ + -+
g:
explain select * from t where c3= a%
+ -+ -+ -+ + + + + + + -+
| id | select_type | table | type | possible_keys | key| key_len | ref| rows | Extra|
+ -+ -+ -+ + + + + + + -+
|1 | SIMPLE| t| ALL| NULL| NULL | NULL| NULL |36 | Using where |
+ -+ -+ -+ + + + + + + -+
全表扫 , 没用到了复合索引idx_t_c1234 , 除非Where条件后面有c1,c2
推荐阅读
- 同时举办奥运会和冬奥会的国家 世界杯和奥运会同年举办
- 鞋子氧化发黄怎么办,鞋子氧化变绿了怎么恢复
- 包包链条氧化发黑怎么办,小ck流苏链条氧化变黑怎么办
- 创造与魔法建筑方案图纸怎么获得
- 清蒸金昌鱼蒸多久,清蒸金昌鱼做法金昌鱼怎么做好吃
- 出现老年斑还能去除吗 老年斑的去除
- 拉杆箱密码锁如何使用
- 蒸好的紫薯怎么保存,紫薯芋圆半成品怎么保存
- 鱼汤怎么去腥味,鲫鱼汤怎么去腥味
