多列索引的运用

 

索引

  * 多个单列索引 ,只能使用其中的一个索引

  1、btree索引的常见误区

    在where条件常用的列都加上索引

    例:where cat_id=3 and price>100; //查询第三个栏目,100元以上的商品

    误:cat_id上和price尚都加上索引

    错:只能用上cat_id或price索引,因为独立的索引,同时只能用上1个

  2、在多列上建立索引后,查询哪个列,索引都将发挥作用

  误:多列索引上,索引发挥作用,需要满足左前缀要求

语句 索引是否发挥作用
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 'xxxx%' and c=7 A能用,B能用,C不能用

    例:

      A:where c1=x and c2=x and c4>x and c3=x   // 全部用上了

      B:where c1=x and c2=x and c4=x order by c3  // c1,c2,c3用上了

      C:where c1=x and c4=x group by c3,c2  // c1用上了

      D:where c1=? and c5=? order by c2,c3 // c1,c2,c3用上了

      E:where c1=? and c2=? and c5=? order by c2,c 3 // c1,c2,c3用上了

猜你喜欢

转载自www.cnblogs.com/longqin/p/11655686.html