关于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 索引使用测试的更多相关文章

  1. mysql优化 | 存储引擎,建表,索引,sql的优化建议

    个人对于选择存储引擎,建表,建索引,sql优化的一些总结,给读者提供一些参考意见 推荐访问我的个人网站,排版更好看: https://chenmingyu.top/mysql-optimize/ 存储 ...

  2. 项目中常用的19条MySQL优化

    声明一下:下面的优化方案都是基于 " Mysql-索引-BTree类型 " 的 一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单 ...

  3. 19条MySQL优化准则

    1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我们要重点关注的数据: type列,连接类型.一个好的SQL语句至少要 ...

  4. 巧用这19条MySQL优化,效率至少提高3倍

    阅读本文大概需要 3.8 分钟. 作者丨喜欢拿铁的人 https://zhuanlan.zhihu.com/p/49888088 本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 1 ...

  5. 项目中常用的MySQL 优化

    本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下: 一.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我 ...

  6. SQL学习笔记之项目中常用的19条MySQL优化

    在写文章之前,首先感谢 飞友科技 陆老师提供的文档.. 声明一下:下面的优化方案都是基于 “ Mysql-索引-BTree类型 ” 的 0x00 EXPLAIN 做MySQL优化,我们要善用 EXPL ...

  7. 项目中常用的MySQL优化方法--壹拾玖条

    1.EXPLAIN 做MySQL优化,我们要善用EXPLAIN查看SQL执行计划. 下面来个简单的示例,标注(1.2.3.4.5)我们要重点关注的数据: type列,连接类型.一个好的SQL语句至少要 ...

  8. 19条常用的MySQL优化方法(转)

    本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下:1.EXPLAIN命令做MySQL优化,我们要善用EXPLAIN查看SQL执行计划.下面来个简单的示例,标注(1.2.3.4.5)我们 ...

  9. MySQL 效率提高N倍的19条MySQL优化秘籍

    一.EXPLAIN 做MySQL优化,我们要善用 EXPLAIN 查看SQL执行计划. 下面来个简单的示例,标注(1,2,3,4,5)我们要重点关注的数据 type列,连接类型.一个好的sql语句至少 ...

  10. 项目中常用的19条MySQL优化技巧

    原文:https://segmentfault.com/a/1190000012155267 声明一下:下面的优化方案都是基于 “ Mysql-索引-BTree类型 ” 的 一.EXPLAIN 做My ...

随机推荐

  1. 生成对抗性网络GAN

    同VAE模型类似,GAN模型也包含了一对子模型.GAN的名字中包含一个对抗的概念,为了体现对抗这个概念,除了生成模型,其中还有另外一个模型帮助生成模型更好地学习观测数据的条件分布.这个模型可以称作判别 ...

  2. 小程序之--动态设置页面标题 wx.setNavigationBarTitle

    参考地址 http://www.yilingsj.com/xwzj/2018-11-26/weixin-navigationbartitletext.html 页面最初是[在线教研] 可以在这个页面的 ...

  3. centos7 链路聚合+KVM桥接连网

    一.两个物理网卡做链路聚合(em3,em4) 1)创建team类型的网卡,连接别名为team0,使用的模式为activebackup-主备/loadbalance-负载均衡nmcli con add ...

  4. java 主动信任证书

    java 主动信任证书 SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mid.toCharArray() ...

  5. 浅谈C++ STL string容器

    浅谈C++ STL string容器 本篇随笔简单讲解一下\(C++STL\)中\(string\)容器的使用方法及技巧. string容器的概念 其实\(string\)并不是\(STL\)的一种容 ...

  6. 如何获得大学教材的PDF版本?

    最近急需一本算法书的配套答案,这本配套单独出售,好像在市面上还买不到,在淘宝上搜索也只是上一个版本,并没有最新版本,让我很无奈.加上平时肯定会有这么一种情况,想看一些书,但买回来也看不了几次,加上计算 ...

  7. 动态数组原理【Java实现】(六)

    前言 接下来我们进入集合学习,看过很多文章一上来就是讲解原理感觉会特别枯燥,任何成熟解决方案的出现都是为了解决问题,若通过实际问题引入然后再来讲解原理想必学起来必定事半功倍,从我写博客的那一天起,我就 ...

  8. 痞子衡嵌入式:飞思卡尔i.MX RTyyyy系列MCU特性那些事(1)- 概览

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是飞思卡尔i.MX RTyyyy系列MCU的基本特性. ARM Cortex-M微控制器芯片厂商向来竞争激烈,具体可参看我的另一篇文章&l ...

  9. Hbase数据结构和shell操作

    Hbase的数据结构 基本要素:命名空间.表.行.列.单元格,region,时间戳. 1.命名空间:NameSpaces的作用 Table:表,所有的表都是命名空间的成员,即表必属于某个命名空间,如果 ...

  10. Mac版Sourcetree的安装使用

    本人也在亲测,感觉很有效,和大家分享,参考链接: https://www.jianshu.com/p/b8d0547a8449