多列索引生效规则

btree索引的常见误区 
在where条件常用的列上都加上索引 
比如:where cat_id=3 and price>100 #查询第3个栏目,100以上的商品 
只能用上cat_id或price索引,因为独立的索引同时只能用上1个。

多列索引生效规则 
多列索引发挥作用,需要满足左前缀要求。

以index(a,b,c)为例:

语句                                    |     索引是否发挥作用
-   -   -   -   -   -   -   -   -   -   -   -   -   -   -   
where a=3                             |     是
where a=3 and b=5                     |     是
where a=3 and b=5 and c=4             |     是
where b=3                             |     否
where c=4                             |     否
where a=3 and c=4                     |     a列能用到索引,c不能
where a=3 and b>10 and c=7            |     a能,b能,c不能
where a=3 and b like 'xxx%' and c=7   |     a能,b能,c不能



猜你喜欢

转载自m635674608.iteye.com/blog/2393713