mysql索引命中规则

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

  1. 如果where后面有or,这样不会命中索引如:
select * from student where userName='小明' or age=19;

如果想要or后面的也走索引的话,应该是创建两个单列索引

  1. like是以%开头的不会命中索引如:
select * from student where userName like %明
  1. 如果列类型是字符串,那一定要在条件中将数据使用引号引用起来,否则不使用索引如:
select * from student where userName='小明' and age=19 and phone=1887821;
  1. 没有查询条件,或者查询条件没有建立索引

  2. 查询条件中,在索引列上使用函数(+/-*/)

select * from student where userName='小明' and age-1=19 错误
select * from student where userName='小明' and age=20   正确
  1. 采用 not in, not exist

  2. B-tree 索引 is null 不会走, is not null 会走

  3. 负向条件查询不能使用索引。

select * from order where status != 0;
  1. 数据区分度不大的字段不宜使用索引 例如:性别,状态等。

转载于:https://my.oschina.net/u/3054838/blog/3039751

你可能感兴趣的:(mysql索引命中规则)