百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>数据库> 介绍mysql索引失效的状况
分享文章到:

介绍mysql索引失效的状况

发布时间:12/01 来源:未知 浏览: 关键词:

全值匹配(索引最好)

explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';

索引失效的情况

和索引次序无关,MySQL底层的优化器会停止优化,调整索引的次序
explain select * from user where name = 'zhangsan' and age = 20 and pos = 'cxy' and phone = '18730658760';

索引失效的情况

1、违反最左前缀规则

假如索引有多列,要遵照最左前缀规则
即查询从索引的最左前列开端并且不跳过索引中的列
explain select * from user where age = 20 and phone = '18730658760' and pos = 'cxy';

索引失效的情况

2、在索引列上做任何操纵

如运算、函数、(主动or手动)类型转换等操纵,会致使索引失效从而全表扫描
explain select * from user where left(name,5) = 'zhangsan' and age = 20 and phone = '18730658760';

索引失效的情况

3、索引范畴前提右侧的列

索引范畴前提右侧的索引列会失效
explain select * from user where name = 'zhangsan' and age > 20 and pos = 'cxy';

索引失效的情况

4、尽量使用覆盖索引

只拜访索引查询(索引列和查询列一致),减少select*
explain select name,age,pos,phone from user where age = 20;

索引失效的情况

5、使用不等于(!=、<>)

mysql在使用不等于(!=、<>)的时候没法使用索引会致使全表扫描(除覆盖索引外)
explain select * from user where age != 20;
explain select * from user where age <> 20;

索引失效的情况

索引失效的情况

6、like以通配符开头('%abc')

索引失效
explain select * from user where name like '%zhangsan';

索引失效的情况

索引生效
explain select * from user where name like 'zhangsan%';

索引失效的情况

7、字符串不加单引号索引失效

explain select * from user where name = 2000;

索引失效的情况

8、or连接

少用or
explain select * from user where name = '2000' or age = 20 or pos ='cxy';

索引失效的情况

9、order by

正常(索引参与了排序)
explain select * from user where name = 'zhangsan' and age = 20 order by age,pos;
备注:索引有两个作用:排序和查寻
致使额外的文件排序(会落低机能)
explain select name,age from user where name = 'zhangsan' order by pos;//违反最左前缀规则
explain select name,age from user where name = 'zhangsan' order by pos,age;//违反最左前缀规则
explain select * from user where name = 'zhangsan' and age = 20 order by created_time,age;//含非索引字段

索引失效的情况

索引失效的情况

索引失效的情况

10、group by

正常(索引参与了排序)
explain select name,age from user where name = 'zhangsan' group by age;
备注:分组此前必排序(排序同order by)

索引失效的情况

致使发生暂时表(会落低机能)
explain select name,pos from user where name = 'zhangsan' group by pos;//违反最左前缀规则
explain select name,age from user where name = 'zhangsan' group by pos,age;//违反最左前缀规则
explain select name,age from user where name = 'zhangsan' group by age,created_time;//含非索引字段

索引失效的情况

索引失效的情况

索引失效的情况

使用的示例数据

mysql> show create table user \G
******************************************************
       Table: user
Create Table: CREATE TABLE `user` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `age` int(10) DEFAULT '0',
  `pos` varchar(30) DEFAULT NULL,
  `phone` varchar(11) DEFAULT NULL,
  `created_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_name_age_pos_phone` (`name`,`age`,`pos`,`phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

以上就是介绍mysql索引失效的状况的具体内容,更多请关注百分百源码网其它相关文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有150人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板