当包含多个列作为索引,需要注意的是正确的顺序依赖于该索引的查询,同时需要考虑如何更好的满足排序和分组的需要
案例
建立组合索引a,b,c,不同SQL语句使用索引情况
| 语句 | 索引是否有效 |
|---|---|
| where a=3 | 是,使用了a |
| where a=3 and b=5 | 是,使用了ab |
| where a=3 and b=5 and c=4 | 是,使用了abc |
| where b=3 or where c=4 | 否 |
| where a=3 and c=4 | 是,只使用了a |
| where a=3 and b> 10 and c=7 | 是,使用了ab |
| where a=3 and b like %xx%’ and c=7 | 是,使用了a |
or会不会走索引?
看索引是不是单列索引,如果是,走,如果是联合索引,不走
