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不能
本文解析了BTree索引常见的使用误区,例如在复合查询中仅能利用其中一个索引的情况,并详细介绍了多列索引的左前缀原则,帮助读者理解如何更高效地使用数据库索引。
651

被折叠的 条评论
为什么被折叠?



