mysql 不走索引的情况

Posted 二十六画生的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 不走索引的情况相关的知识,希望对你有一定的参考价值。

1 in, 2个及以上参数 ,单列索引a, where a in (xxxx) ,如果参数是1个会用到索引,如果参数是2个及以上不会用到索引(包括int和varchar类型的字段)

2 like '%abc' 或者 like‘%abc%’

3 where num/2=100 或者 substring(a,1,3)='ab'或者age+10=30

4 where id !=2 或者 where id <> 2

5 where name is null

6 not in ,单列索引a ,where a not in (xxxxx) ,不管里面是一个还是多个参数都用不到a的索引(包括int和varchar类型的字段)

7 字符类型的字段与数字比较 

 

8 单列索引a , where条件中 :

a用到(比如a> xxx 或者 a>=yyy 或者 a >xxx and a <yyy 或者 a  between xxx and yyy  均会用到),

a > xxx or a <yyy 用不到。(包括int和varchar类型的字段)

 

9 单列索引a,单列索引b,单列索引c,where条件中 :

a or b 均用不到,

a or b or c 均用不到,

a and b 用到 a的索引,

a and b and c 用到a的索引,

c and b 用到b的索引(谁的索引定义在前面用谁的),

(a and b ) or c 均用不到,

(a or b ) and c 用到c的索引

 

10 复合索引a-b-c,where条件中 :

a用到,

a and b用到,

b and a用到,

a and c用到,

c and a用到,

a and b and c用到,

c and b and a用到,

b用不到,

c用不到,

b and c用不到,

c and b用不到,

a or b用不到,

b or c用不到,

a or b or c用不到,

(a or b) and c用不到,

(a and b) or c用不到,

(a or d) and b and c 用不到

当前版本:

mysql> select version() ;
+-----------+
| version() |
+-----------+
| 5.7.28    |
+-----------+
1 row in set (0.00 sec)

 

更多企业内的技术应用和使用技巧,请移步至我的公众号【程序员实用技能】

以上是关于mysql 不走索引的情况的主要内容,如果未能解决你的问题,请参考以下文章

问答 | MySQL 什么时候不走索引?

数据查询必备技能SQL调优:Mysql什么情况下不走索引

mysql不走索引5000条数据要多久

Mysql时间范围查询不走索引问题

Mysql时间范围查询不走索引问题

Mysql查询条件为大于时,不走索引失效问题排查