索引的失效

1.6、索引有失效的时候,什么时候失效呢?(重要)

  (失效1)select * from emp where ename like ‘%T’;

  

 

 

   type = ALL

  ename上即使添加了索引,也不会走所引,为什么?

  原因是模糊匹配中以“%”开始

  尽量避免模糊查询时以"%"开始

  这是一种优化的手段/策略

  (失效2)
    使用or的时候会失效,如果使用or那么要求or两边的条件字段都要有索引,才会走索引,如果其中一边有一个字段没索引,

    另一个字段上的索引也会失效。这就是为什么不建议使用or的原因。

    案例:explain select * from emp where ename = ‘KING’ or job = 'MANAGER';  //这种情况可以采用union的方式

      

 

 

     type = ALL

  (失效3)

    使用 复合索引的时候,没有使用左侧的列查找,索引失效

    什么是复合索引:两个字段或更多的字段联合起来添加一个索引

    create index emp_job_sal_index on emp(job,sal);

    explain select *from emp where job = 'MANAGER';

    

 

 

 

    explain select *from emp where sal = 800;

    

 

 

   (失效4)

    在where中索引列参加了数学运算,索引失效

    create index emp_sal_index on emp(sal);

    explain select * from emp where sal+1 = 800;//字段参加了运算

  

    

 

 

   (失效5)

      在where中索引列使用了函数

    explain select * from emp where lower(ename) = 'smith';

      

 

    (失效情况很多)。。。。

posted @ 2022-01-23 19:53  doremi429  阅读(121)  评论(0编辑  收藏  举报