mysql 优化之 is null ,is not null 索引使用测试
关于mysql优化部分,有很多网友说尽量避免使用is null, is not null,select * 等,会导致索引失效,性能降低?那是否一定收到影响呢?真的就不会使用索引了吗?
本文的测试数据库版本为5.7.18,不同版本得出的结果可能会有所不同:

本文测试的两张表数据如下:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `t_user` (`id`, `name`, `age`, `sex`) VALUES ('1', 'jemis', '20', '男');
INSERT INTO `t_user` (`id`, `name`, `age`, `sex`) VALUES ('2', 'fox', '20', '男');
INSERT INTO `t_user` (`id`, `name`, `age`, `sex`) VALUES ('3', 'tony', '20', '男'); CREATE UNIQUE INDEX indexName ON t_user(name(20));
一、索引字段不能为空的情况下:
测试select * 结合is null 和 is not null :


可以得出:
EXPLAIN select * from t_user where `name` is not null; 不使用索引;
EXPLAIN select * from t_user where `name` is null; 不使用索引;
查询索引字段的情况下:


有以上可以得出查询索引字段时:
EXPLAIN select name from t_user where `name` is not null; 使用索引
EXPLAIN select name from t_user where `name` is null; 不使用索引
select 索引字段加 非索引字段时:



可以得出:当select索引字段+其他字段时:
EXPLAIN select name,age from t_user where `name` is not null; 不使用索引
EXPLAIN select name,age from t_user where `name` is null; 不使用索引
由此可见,当索引字段不可以为null 时,只有使用is not null 并且返回的结果集中只包含索引字段时,才使用索引
二、当索引字段可以为null 时测试数据:
CREATE TABLE `t_user1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `springtest`.`t_user1` (`id`, `name`, `age`, `sex`) VALUES ('1', 'jemis', '20', '男');
INSERT INTO `springtest`.`t_user1` (`id`, `name`, `age`, `sex`) VALUES ('2', 'fox', '20', '男');
INSERT INTO `springtest`.`t_user1` (`id`, `name`, `age`, `sex`) VALUES ('3', 'tony', '20', '男'); CREATE UNIQUE INDEX indexName ON t_user1(name(20));
当索引字段可以为null,使用is null ,is not null:
当select * 时:


查询语句select *:EXPLAIN select * from t_user1 where `name` is not null;; 不使用索引
EXPLAIN select * from t_user1 where `name` is null; 使用索引
select 索引字段:


select 索引字段的结论为:
EXPLAIN select name from t_user1 where `name` is not null; 使用索引
EXPLAIN select name from t_user1 where `name` is null; 使用索引
select 索引字段 + 非索引字段 为:


当select索引字段+其他字段时: EXPLAIN select name,age from t_user1 where `name` is not null;不使用索引,会导致索引失效
EXPLAIN select name,age from t_user1 where `name` is null; 使用索引
总结以上情形可知:1、当索引字段不可以为null 时,只有使用is not null 返回的结果集中只包含索引字段时,才使用索引
2、当索引字段可以为空时,使用 is null 不影响覆盖索引,但是使用 is not null 只有完全返回索引字段时才会使用索引
最后附上本文的测试完整版sql以及结论:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `springtest`.`t_user` (`id`, `name`, `age`, `sex`) VALUES ('', 'jemis', '', '男');
INSERT INTO `springtest`.`t_user` (`id`, `name`, `age`, `sex`) VALUES ('', 'fox', '', '男');
INSERT INTO `springtest`.`t_user` (`id`, `name`, `age`, `sex`) VALUES ('', 'tony', '', '男');
CREATE UNIQUE INDEX indexName ON t_user(name(20)); CREATE TABLE `t_user1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; INSERT INTO `springtest`.`t_user1` (`id`, `name`, `age`, `sex`) VALUES ('', 'jemis', '', '男');
INSERT INTO `springtest`.`t_user1` (`id`, `name`, `age`, `sex`) VALUES ('', 'fox', '', '男');
INSERT INTO `springtest`.`t_user1` (`id`, `name`, `age`, `sex`) VALUES ('', 'tony', '', '男'); CREATE UNIQUE INDEX indexName ON t_user1(name(20)); 当字段值可以为null,使用is null ,is not null,
查询语句:EXPLAIN select * from t_user1 where `name` is not null;; 不使用索引
EXPLAIN select * from t_user1 where `name` is null; 使用索引 当不使用select * 时 : EXPLAIN select name from t_user1 where `name` is not null; 使用索引
EXPLAIN select name from t_user1 where `name` is null; 使用索引 当select索引字段+其他字段时: EXPLAIN select name,age from t_user1 where `name` is not null;不使用索引,会导致索引失效
EXPLAIN select name,age from t_user1 where `name` is null; 使用索引 从以上可以看出,当索引字段可以为空时,使用 is null 不影响覆盖索引,但是使用 is not null 只有完全返回索引字段时才会使用索引 当字段值不能为null 时,EXPLAIN select * from t_user where `name` is not null; 不使用索引;
EXPLAIN select * from t_user where `name` is null; 不使用索引; 当select索引字段 时 : EXPLAIN select name from t_user where `name` is not null; 使用索引
EXPLAIN select name from t_user where `name` is null; 不使用索引 当select索引字段+其他字段时: EXPLAIN select name,age from t_user where `name` is not null; 不使用索引
EXPLAIN select name,age from t_user where `name` is null; 不使用索引 由此可见,当索引字段不可以为null 时,只有使用is not null 返回的结果集中只包含索引字段时,才使用索引
mysql 优化之 is null ,is not null 索引使用测试的更多相关文章
- mysql优化 | 存储引擎,建表,索引,sql的优化建议
个人对于选择存储引擎,建表,建索引,sql优化的一些总结,给读者提供一些参考意见 推荐访问我的个人网站,排版更好看: https://chenmingyu.top/mysql-optimize/ 存储 ...
- 项目中常用的19条MySQL优化
声明一下:下面的优化方案都是基于 " Mysql-索引-BTree类型 " 的 一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单 ...
- 19条MySQL优化准则
1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我们要重点关注的数据: type列,连接类型.一个好的SQL语句至少要 ...
- 巧用这19条MySQL优化,效率至少提高3倍
阅读本文大概需要 3.8 分钟. 作者丨喜欢拿铁的人 https://zhuanlan.zhihu.com/p/49888088 本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 1 ...
- 项目中常用的MySQL 优化
本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 一.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我 ...
- SQL学习笔记之项目中常用的19条MySQL优化
在写文章之前,首先感谢 飞友科技 陆老师提供的文档.. 声明一下:下面的优化方案都是基于 “ Mysql-索引-BTree类型 ” 的 0x00 EXPLAIN 做MySQL优化,我们要善用 EXPL ...
- 项目中常用的MySQL优化方法--壹拾玖条
1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我们要重点关注的数据: type列,连接类型.一个好的SQL语句至少要 ...
- 19条常用的MySQL优化方法(转)
本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下:1.EXPLAIN命令做MySQL优化,我们要善用EXPLAIN查看SQL执行计划.下面来个简单的示例,标注(1.2.3.4.5)我们 ...
- MySQL 效率提高N倍的19条MySQL优化秘籍
一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据 type列,连接类型.一个好的sql语句至少 ...
- 项目中常用的19条MySQL优化技巧
原文:https://segmentfault.com/a/1190000012155267 声明一下:下面的优化方案都是基于 “ Mysql-索引-BTree类型 ” 的 一.EXPLAIN 做My ...
随机推荐
- 十二、ITK例程-医疗图像配准的HelloWorld程序
一.说明 医疗图像配准是ITK的一个重要内容,而我们今天想要说的一个程序则相当于是其中的HelloWorld程序. 程序源码位置: InsightToolkit-\Examples\Registrat ...
- 【Golang基础】defer执行顺序
defer 执行顺序类似栈的先入后出原则(FILO) 一个defer引发的小坑:打开文件,读取内容,删除文件 // 原始问题代码 func testFun(){ // 打开文件 file, ...
- Java之System类
System类概述 java.lang.System 类中提供了大量的静态方法,可以获取与系统相关的信息或系统级操作,在System类的API文档中,常用的方法有: public static lon ...
- Windows10 搭建Kafka集群
下载Kafka 1.下载Kafka:http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.12-2.3.0.tgz 2.解压后复制Kafka文件夹,分 ...
- SpringBoot2.0 基础案例(15):配置MongoDB数据库,实现增删改查逻辑
本文源码:GitHub·点这里 || GitEE·点这里 一.NoSQL简介 1.NoSQL 概念 NoSQL( Not Only SQL ),意即"不仅仅是SQL".对不同于传统 ...
- Ruby中星号打包解包操作
Ruby中可以使用一个星号*和两个星号**完成一些打包.解包操作,它们称为splat操作符: 一个星号:以数组为依据进行打包解包(参考文章) 两个星号:以hash为依据进行打包解包(参考文章) 两个星 ...
- .NET Core C# 中级篇2-7 文件操作
.NET Core CSharp 中级篇2-7 本节内容为文件操作 简介 文件操作在我们C#里还是比较常见的,例如我们读取Excel.Txt文件的内容,在程序中,这些文件都是以流的方式读取进入我们内存 ...
- linux-创建/使用快照/克隆(类似windows中备份还原)
一. 创建/使用快照 1.什么是快照 说的直白一点,就是创建一个备份. 当执行了不可逆的错误操作后,可以通过快照用来恢复系统 2.创建快照的3种模式 挂载状态下创建快照 开机状态下创建 ...
- javaWeb综合案例
商城案例 分模块:用户模块→ 分类模块 → 商品模块→购物车模块(最难)→ 订单模块 后台模块(往前台的数据的增删改查) 今日任务: 1用户注册 a注册完成 b给注册人发邮件 2用户激活 a 去邮箱激 ...
- vue关于mock的简单使用
一.mock 1.简介 mock是一个模拟数据生成器,旨在帮助前端独立于后端进行开发,帮助编写单元测试.其可模拟 Ajax 并返回模拟数据,使前端不用去调用后端的接口,方便测试. 2.vue直接使用m ...