explain select * from t where c1= a1 and c2= b2 and c3= a3and c4= a1
+ -+ -+ -+ + -+ -+ + -+ + +
| 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_c3,idx_t_c4 | idx_t_c4 | 11| const |1 | Using index condition; Using where |
+ -+ -+ -+ + -+ -+ + -+ + +
删除了复合索引后:发现只使用了一个索引c1 , 没有用其它索引 , 这是因为优化器没有发现哪个条件取值记录最少(c2,c3,c4= 等值连接也是匹配多条)就选第最左列索引
explain select * from t where c1= a1 and c2= b2 and c3= a3and c4= a4
+ -+ -+ -+ + -+ -+ + -+ + +
| 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_c3,idx_t_c4 | idx_t_c1 | 11| const |18 | Using index condition; Using where |
+ -+ -+ -+ + -+ -+ + -+ + +
a:
explain select * from t where c4= a1 and c2= b2 and c3= a3and c1= a1
+ -+ -+ -+ + + -+ + -+ + +
| id | select_type | table | type | possible_keys | key| key_len | ref| rows | Extra|
+ -+ -+ -+ + + -+ + -+ + +
|1 | SIMPLE| t| ref| idx_t_c1234| idx_t_c1234 | 44| const,const,const,const |1 | Using where; Using index |
+ -+ -+ -+ + + -+ + -+ + +
where条件后面的顺序无关
b:
explain select * from t where c1= a1 and c2= b2 and c4 a and c3= a3
+ -+ -+ -+ -+ + -+ + + + +
| id | select_type | table | type| possible_keys | key| key_len | ref| rows | Extra|
+ -+ -+ -+ -+ + -+ + + + +
|1 | SIMPLE| t| range | idx_t_c1234| idx_t_c1234 | 44| NULL |1 | Using index condition |
+ -+ -+ -+ -+ + -+ + + + +
key_len: 44 // CHAR(10)*4 + 4 * NULL:说明全用到了4个索引 , 且都是等值查询的索引:c1,c2,c3,c4,全通过
Using index condition:5.6新特性 , Where条件过滤是在innodb引擎层就可做掉了 , 这样innodb发送给server层的会少很多 , 如果不启用该功能 , 则数据通过索引访问后 , 数据要发送到server层进行where过滤
b:
explain select * from t where c1= a1 and c2= b2 and c3= a3and c4 a
+ -+ -+ -+ -+ + -+ + + + +
| id | select_type | table | type| possible_keys | key| key_len | ref| rows | Extra|
+ -+ -+ -+ -+ + -+ + + + +
|1 | SIMPLE| t| range | idx_t_c1234| idx_t_c1234 | 44| NULL |1 | Using index condition |
+ -+ -+ -+ -+ + -+ + + + +
range:代表c4采用索引了 , 且使用到范围查找
c:
explain select * from t where c1= a1 and c2= b2 and c4= b4 order by 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: 22 // CHAR(10)*2 + 2 * NULL:说明全用到了c1,c2索引,且都是等值查询的索引:c1,c2
Using where:说明c4在server层进行where过滤操作
c3:用到了索引排序
ref 需要与索引比较的列 列名或者const(常数 , where id = 1的时候就是const了)
删除了复合索引后:只用到了c1索引 , 也就是只用一个索引 , 其它索引也没用上 , 排序也没用上
explain select * from t where c1= a1 and c2= b2 and c4= b4 order by 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_c4 | idx_t_c1 | 11| const |2 | Using index condition; Using where; Using filesort |
+ -+ -+ -+ + -+ -+ + -+ + -+
d:
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|
推荐阅读
- 同时举办奥运会和冬奥会的国家 世界杯和奥运会同年举办
- 鞋子氧化发黄怎么办,鞋子氧化变绿了怎么恢复
- 包包链条氧化发黑怎么办,小ck流苏链条氧化变黑怎么办
- 创造与魔法建筑方案图纸怎么获得
- 清蒸金昌鱼蒸多久,清蒸金昌鱼做法金昌鱼怎么做好吃
- 出现老年斑还能去除吗 老年斑的去除
- 拉杆箱密码锁如何使用
- 蒸好的紫薯怎么保存,紫薯芋圆半成品怎么保存
- 鱼汤怎么去腥味,鲫鱼汤怎么去腥味
