以mysql版本8.0.11为例
1.
<> 、not in 、not exist、exists、!= 会使索引失效
然而如果是主键索引 <>、not in、 != 索引同样能生效
in, is null, is not null,>,>=,<,<=,= 索引都能生效
2.
where name like '%a' 索引失效
where name like 'a%' 或者‘a%a%’ 索引生效
3.
如果索引字段是字符串需加引号
where name = '11' 索引生效
where name = 11 查询结果正常 索引失效 where name = aa 报错
4.
对索引列做计算会使索引失效
select * from user where substring(name,1.2)='aa'
5.
查询有or的时候,查询条件字段都为索引 索引才能生效
如 select * from user where id='1' or name = 'a' 只有当 id,name都为索引才生效
6.
如果使用全表扫描速度比使用索引快或者查询出的结果数量和总表都差不多了 索引不生效
本文详细介绍了MySQL中索引的工作原理及使用技巧,包括不同查询条件对索引的影响、字符串类型字段索引的正确用法、计算列索引的问题、复合索引的使用场景以及全表扫描与索引查询效率对比等内容。
213

被折叠的 条评论
为什么被折叠?



