介绍mysql索引失效的情况


本文摘自PHP中文网,作者coldplay.xixi,侵删。

mysql视频教程栏目索引失效的情况。

索引对于MySQL而言,是非常重要的篇章。索引知识点也巨多,要想掌握透彻,需要逐个知识点一一击破,今天来先来聊聊哪些情况下会导致索引失效。

图片总结版

索引失效的情况

相关免费学习推荐:mysql视频教程

全值匹配(索引最佳)

1

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

索引失效的情况

1

2

和索引顺序无关,MySQL底层的优化器会进行优化,调整索引的顺序

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

索引失效的情况

1、违反最左前缀法则

1

2

3

如果索引有多列,要遵守最左前缀法则

即查询从索引的最左前列开始并且不跳过索引中的列

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

索引失效的情况

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

1

2

如计算、函数、(自动or手动)类型转换等操作,会导致索引失效从而全表扫描

explain select * from user where left(name,5) = 'zhangsan' and age = 20 and phone = '18730658760';

索引失效的情况

3、索引范围条件右边的列

1

2

索引范围条件右边的索引列会失效

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

索引失效的情况

4、尽量使用覆盖索引

1

2

只访问索引查询(索引列和查询列一致),减少select*

explain select name,age,pos,phone from user where age = 20;

索引失效的情况

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

1

2

3

mysql在使用不等于(!=、<>)的时候无法使用索引会导致全表扫描(除覆盖索引外)

explain select * from user where age != 20;

explain select * from user where age <> 20;

索引失效的情况

索引失效的情况

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

1

2

索引失效

explain select * from user where name like '%zhangsan';

索引失效的情况

1

2

索引生效

explain select * from user where name like 'zhangsan%';

索引失效的情况

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

1

explain select * from user where name = 2000;

索引失效的情况

8、or连接

1

2

少用or

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

索引失效的情况

9、order by

1

2

3

正常(索引参与了排序)

explain select * from user where name = 'zhangsan' and age = 20 order by age,pos;

备注:索引有两个作用:排序和查找

1

2

3

4

导致额外的文件排序(会降低性能)

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

1

2

3

正常(索引参与了排序)

explain select name,age from user where name = 'zhangsan' group by age;

备注:分组之前必排序(排序同order by)

索引失效的情况

1

2

3

4

导致产生临时表(会降低性能)

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;//含非索引字段

索引失效的情况

索引失效的情况

索引失效的情况

使用的示例数据

1

2

3

4

5

6

7

8

9

10

11

12

13

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索引失效的情况的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

mysql事务详细介绍

《高性能mysql》怎么样

怎样安全地关闭mysql实例

mysql怎么加入一个数据库?

mysql存储引擎基础知识

centos 6.5下 mysql-community-server. 5.7.18-1.el6安装

怎样修改mysql列的数据类型?

下载的mysql怎么无法安装

mysql怎样查看帮助

如何在控制台修改mysql字符集

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

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

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

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

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...