Mysql的索引失效(应避免)(十)

https://blog.csdn.net/qq_29347295/article/details/79112102

版权声明:本文为博主原创文章,未经博主允许不得转载。    https://blog.csdn.net/qq_29347295/article/details/79112102


type:system >const>eq_ref>ref>range>index>all

一般来说:查询要达到index级别,最好能达到ref

案例1:

create index girl_cup_money on girl(age,cup_size,money);


show index from girl

EXPLAIN select * from girl where  age='4';

EXPLAIN select * from girl where age='4' and cup_size='B' ;

EXPLAIN select * from girl where age='4' and cup_size='B'  and money='4';

EXPLAIN select * from girl where  cup_size='B' ;

EXPLAIN select * from girl where age='4'  and money='4';

EXPLAIN select * from girl where  cup_size='B'  and money='4';

小结论:全值匹配我最爱

               最佳左前提法则------如果索引了多列,要遵守最左前法则指的是查询从索引的最左前列开始并且不跳过索引中的列

3.不在索引列上做任何操作(计算,函数,(自动or手动)类型转换),都会导致索引失效而转向全表扫描

如上:索引列没使用函数,可以用到索引,使用函数不能用到索引

4.存储引擎不能使用索引中范围条件右边的列(注意:范围条件列可以使用到)

5.尽量使用覆盖索引(只访问索引的查询(索引列和查询列一致)),减少select *

6.mysql在使用不等于(!=或者<>)的时候无法使用索引会导致全表扫描

7.is null,is not null也无法使用索引

8.like以通配符开头(%abc..)mysql索引实效会变成全表扫描的操作

%abc 和%abc%会变成全表扫描,abc%范围扫描

解决%abc%时索引不被使用的方法:使用覆盖索引,即查询的内容包含在复合索引中

9字符串不加单引号索引失效

上面的name=2000发生了隐式的类型转换,int变成varchar,导致索引失效

10.少用or,用它来连接时会索引失效

猜你喜欢

转载自blog.csdn.net/xxxcyzyy/article/details/85228115