什么是联合索引
一个索引包含的字段为多个
什么是最左匹配?
在index(c1 c2 c3) 下,当where 条件为 (c1)、(c1、c2)(c1、c2、c3) 三种条件时才会生效。
select * from user where c1= 12 and c2= 14 and c3 = 3 // 索引全匹配
select * from user where c1= 12 and c2= 14 // 索引部分匹配
select * from user where c1= 12 and c3 = 3 // 索引部分匹配
select * from user where c2= 12 and c3 = 3 // 索引无法匹配
值得注意的是,当遇到范围查询(>、<、between、like)就会停止匹配。
对(a,b,c,d)建立索引,where后条件为
a = 1 and b = 2 and c > 3 and d = 4
那么,a,b,c三个字段能用到索引,而d就匹配不到。因为遇到了范围查询,当遇到范围查询(>、<、between、like)就会停止匹配。!
为什么?
创建的c1、c2、c3索引,相当于创建了(c1)、(c1、c2)(c1、c2、c3)三个索引,看完下面你就知道为什么相当于创建了三个索引。
联合索引如何实现的
1,1
1,2
1,3
2,1
2,2
2,3
以一定顺序排列,然后构建B+树