MySQL数据库之索引会失效的原因分析及解决索引失效的方法
小标 2019-03-29 来源 : 阅读 6524 评论 0

摘要:本文主要向大家介绍了MySQL数据库之索引会失效的原因分析及解决索引失效的方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

本文主要向大家介绍了MySQL数据库之索引会失效的原因分析及解决索引失效的方法 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

MySQL数据库之索引会失效的原因分析及解决索引失效的方法

索引失效原因


一.准备工作


创建student表,id是主键


创建复合索引


create index idx_name_age on student(name,age);

   

查看索引


show index from student;

   

二.索引失效原因


1.全值匹配我最爱


explain select * from student where name = '张三' and age = 1;

   

2.最佳左前缀法则,带头大哥不能死,中间兄弟不能少。


explain select * from student where age = 1;

   

3.不要在索引列上做任何操作


explain select * from student where left(name,1) = '张' and age = 1;

   

4.范围条件后列上索引失效


explain select * from student where age > 1 and name = '王五';

   

查找级别是范围,name上的索引失效。


5.尽量使用覆盖索引减少使用select *


explain select * from student where name = 'zhangsan';


explain select name from student where name = 'zhangsan';

   

6.使用不等于(!= 或者<>)不能使用索引


explain select * from student where name != '张三';

   

7.使用 is null 或者 is not null 也不能使用索引


explain select * from student where name is not null;

   

8.like 已通配符开头(%abc)导致索引失效 (解决方法:使用覆盖索引)


explain select * from student where name like '%张%';

   

想用的话解决方法,使用覆盖索引


explain select name from student where name like '%张%';

   

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


explain select * from student where name = '张三' or age = 2;

   

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 4 不喜欢 | 7
看完这篇文章有何感觉?已经有11人表态,36%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程