mysql优化总结-btree索引

 

在mysql中,索引可以有效的提高查询效率,但在实际项目中有时候即使在where条件都加上索引,也不一定能够使用到索引,更多情况下是联合索引用的比较多

举个栗子:where id=3 and price>100;//查询id为3,100元以上的商品(id,price 分别为添加索引)

       误区:只能用上id或price其中一个,因为是独立的索引,同时只能用上一个。

         

联合索引:在多列同时创建索引后,需要满足左前缀原则,才用到索引

      以index(a,b,c)为例。(注意和顺序有关)

 

语句 否使用索引
where a=3 是,只使用了a列
where a=3 and =5  是,使用了ab列
 where a=3  and b=4 and c=5  是,使用了abc
 where b=3 or c=4  否因为跳过a
 where a=3 and c =4  a用到索引,c不能
 where a=3 and b like 'hello%'  a用到了,b部分用到
   

     

              

原文地址:https://www.cnblogs.com/fukai-blog/p/6689064.html