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 ('1', 'jemis', '20', '男'); INSERT INTO `springtest`.`t_user` (`id`, `name`, `age`, `sex`) VALUES ('2', 'fox', '20', '男'); INSERT INTO `springtest`.`t_user` (`id`, `name`, `age`, `sex`) VALUES ('3', 'tony', '20', '男'); 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 ('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, 查询语句: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 返回的结果集中只包含索引字段时,才使用索引
View Code
转载于:https://www.cnblogs.com/cheng21553516/p/11450765.html
mysql 优化之 is null ,is not null 索引使用测试相关推荐
- 项目中常用的19条MySQL优化
声明一下:下面的优化方案都是基于 " Mysql-索引-BTree类型 " 的 一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单 ...
- 项目中常用的 19 条 MySQL 优化总结
作者:zhangqh 地址:segmentfault.com/a/1190000012155267 本文总结了19条关于Mysql的优化方案,本文的优化方案都是基于 " Mysql-索引-B ...
- mysql 常用优化方案_项目中常用的 19 条 MySQL 优化方案
声明一下:下面的优化方案都是基于 " Mysql-索引-BTree类型 " 的 一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单 ...
- 巧用这19条MySQL优化,效率至少提高3倍
本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我 ...
- 开发中常用的19 条 MySQL 优化技巧
作者:zhangqh 来源:segmentfault.com/a/1190000012155267 本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 一.EXPLAIN 做MySQL ...
- mysql优化 坑_mysql之我们终将踩过的坑(优化)
一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据 type列,连接类型.一个好的sql语句至少 ...
- 关于mysql的项目_项目中常用的MySQL 优化
本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 一.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我 ...
- 效率提高N倍的19条MySQL优化秘籍
一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据 type列,连接类型.一个好的sql语句至少 ...
- mysql 分段执行_了解一下项目中常用的19条MySQL优化
一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据 · type列,连接类型.一个好的sql语句 ...
- mysql 分段执行_19个MySQL优化技巧,索引优化这样做最有效!
- 点击上方"中国统计网"订阅我吧!- 声明:下面的优化方案都是基于" Mysql-索引-BTree类型 "01善用EXPLAIN 做MySQL优化,我们要善用 ...
最新文章
- 端口号被占用怎么解决
- css火狐 hover 图片会重新请求,在“unhovering”之后使CSS Hover状态保持不变
- dm9000AE调试记录
- 剑灵系统推荐加点_剑灵重制修炼系统 无定式加点打造自我风格
- 尴尬!苹果公司首开官方微博 迎接它的却是无穷无尽的吐槽...
- windows安装ffmpeg_免费实用的录屏工具!支持全屏、特定窗口、选定区域录制,支持添加水印、嵌入摄像头(附ffmpeg安装)...
- 比较经典的java程序_一些经典的java小程序代码,最好能复制后直接使用的 爱问知识人...
- 久其报表大厅_久其报表大厅[Web版]操作手册.pdf
- C报错ld returned 1 exit status可能的原因
- 使用Mac下的sequel Pro数据库错误MySQL said: Authentication plugin 'caching_sha2_password'
- python图片保存jpg、show变成bmp_python bmp转换为jpg 并删除原图的方法
- 荣耀畅玩7c能用鸿蒙吗,荣耀畅玩7C评测:人脸识别双摄 超高性价比
- 视觉slam十四讲课后习题ch3-7
- 下面关于在dos停止mysql_下面关于在DOS停止MySql的命令中,正确的是 (5.0分)_学小易找答案...
- 前端 - excel导入 / 导出功能
- StarUML使用文档
- wifi信息修改插件ios_iOS免越狱修改运营商信息/修改“应用图标标记”颜色
- 讷于言而敏于行_百度百科
- 学习笔记---高等数学前置知识---数列、排列组合、解不等式
- 英语初级语法--动词