索引的最左前缀法则:查询从复合索引最左边的列开始,并且不跳过符合索引中的列,如果跳过了某列,则从最左边开始只对按索引顺序匹配到的进行索引查询。
假设某表table有a,b,c,d,e列。
假设现在有联合索引:b_c_d
1、只使用索引b
select * from table where b=null
2、只使用索引b、c
select * from table where b=null and c=null
3、使用索引b、c、d
select * from table where b=null and c=null and d=null
4、只使用索引b。
select * from table where b=null and d=null
**分析:**因为跳过了索引c,所以d不会走索引。
5、不使用索引。
select * from table where c=null and d=null
**分析:**因为是从索引b开始检索,而查询条件中没有b,所以不会走索引。

本文解析了如何利用复合索引b_c_d进行SQL查询优化,重点讲解了不同情况下索引的使用和避免,如不使用索引、仅依赖部分索引等,并揭示了索引最左前缀法则的应用实例。
472

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



