一.首先看一个我在某公众号看到的一个关于数据库优化的举措

二.如果where子句中查询的列执行了 “is null” 或者 “is not null” 或者 “<=> null” 会不会使用索引呢?

先列出结论:where子句中使用上述对null的判断,如果判断的列设置了索引,那就可以使用到索引

官方依据在:https://dev.mysql.com/doc/refman/5.6/en/is-null-optimization.html

三.测试:

1.建表

 CREATE TABLE `test_null_index` (
`id` int(11) DEFAULT NULL,
`mark` varchar(20) DEFAULT NULL,
`name` varchar(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.插数据

create procedure test_null_index(in num int)
BEGIN
DECLARE i int;
set i=1;
while (i<num)
DO
if mod(i,10)!=0 then
insert into test_null_index values (i,concat('aaa',i),null);
else
insert into test_null_index values (null,concat('aaa',i),'bbb');
end if;
set i=i+1;
END while;
END; call test_null_index(10000);

3.没加索引时,type为All,全表扫描

mysql> explain SELECT * from test_null_index WHERE id is null;
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | test_null_index | ALL | NULL | NULL | NULL | NULL | 10589 | Using where |
+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+
1 row in set (0.00 sec)

4.加上索引,可以看到,索引起作用了

mysql> create index idx_test_null on test_null_index(id);
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> explain SELECT * from test_null_index WHERE id is null;
+----+-------------+-----------------+------+---------------+---------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+---------------+---------------+---------+-------+------+-------------+
| 1 | SIMPLE | test_null_index | ref | idx_test_null | idx_test_null | 5 | const | 998 | Using where |
+----+-------------+-----------------+------+---------------+---------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

四.注意,只能使用针对一个字段的关于null的判断,如果同时在两个字段对null进行判断,还是会走全表扫描

1.测试,可以看到,在name字段加上索引,并查询name为空的语句,同样会走索引

mysql> create index idx_test_null2 on test_null_index(name);
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> explain SELECT * from test_null_index WHERE name is null;
+----+-------------+-----------------+------+----------------+----------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+----------------+----------------+---------+-------+------+-------------+
| 1 | SIMPLE | test_null_index | ref | idx_test_null2 | idx_test_null2 | 36 | const | 8994 | Using where |
+----+-------------+-----------------+------+----------------+----------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

2.同时针对id和name查询为空的语句,走的是全表扫描

mysql> explain SELECT * from test_null_index WHERE id is null or name is null;
+----+-------------+-----------------+------+------------------------------+------+---------+------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------+------+------------------------------+------+---------+------+-------+-------------+
| 1 | SIMPLE | test_null_index | ALL | idx_test_null,idx_test_null2 | NULL | NULL | NULL | 10589 | Using where |
+----+-------------+-----------------+------+------------------------------+------+---------+------+-------+-------------+
1 row in set (0.01 sec)

MySQL_列值为null对索引的影响_实践的更多相关文章

  1. 将表中null值替换成想要的值、查询某一列值为null

    用到ISNULL()函数 例如:SELECT 其他列名,ISNULL(列名,替换值)as 重命名  from 表名 (简单参考:http://www.cnblogs.com/netsa/archive ...

  2. SQL Sever中多列拼接成一列值为NULL

    查询出数据 SELECT a.ID AS KYMain_ID , ',' + a.Leader + ',' AS KYMain_Leader , ), b.TaskLeader) FROM TB_KY ...

  3. SQL判断如果一列值为null则取另一列值代替 isnull()

    [chClientCode] ,[nvcClientName] ,[chRegionCode] ,isnull(chUltimateHeadClientCode,[chClientCode]) as ...

  4. 从DB灌值到DataTable时,字段值为NULL时报错相关信息;

    报错信息: 1.  2.  3.  4.  5.  6.  解决方法: 1. Data Layer SQL 语句取数据时,把其列值有为null的字段用0.00替换,(ISNULL的用法): 2. #r ...

  5. BLOB或TEXT字段使用散列值和前缀索引优化提高查询速度

    1.创建表,存储引擎为myisam,对大文本字段blob使用MD5函数建立一个散列值 create table t2(id varchar(60), content blob, hash_value ...

  6. NULL值比较,两个列的合并,列值按条件替换。

    show create table 表名 -- 显示创建表的sql语句. 为已有的表增加新列.alter table 表名 add 列名 int NULL -- 此行加了一个int 类型 默认可以nu ...

  7. MySQL5.7 虚拟列实现表达式或函数索引

    MySQL5.7 虚拟列实现表达式或函数索引 http://www.linuxidc.com/Linux/2015-11/125162.htm https://dev.mysql.com/doc/re ...

  8. mysql中生成列与JSON类型的索引

    MySQL中支持生成列,生成列的值是根据列定义中包含的表达式计算的. 一个简单的例子来认识生成列! CREATE TABLE triangle( sidea DOUBLE, sideb DOUBLE, ...

  9. mysql 清空或删除表数据后,控制表自增列值的方法

    http://blog.sina.com.cn/s/blog_68431a3b0100y04v.html 方法1: truncate table 你的表名 //这样不但将数据全部删除,而且重新定位自增 ...

随机推荐

  1. centos7查看可登陆用户

    一.命令 cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1 cat /etc/passwd | grep   /bin/bash | cu ...

  2. iOS 常用三方(持续更新)

    iOS 常用三方 1.ZWMSegmentController 分页控制器 https://github.com/weiming4219/ZWMSegmentController

  3. FPGA驱动VGA显示静态图片

    一 .前言 本文设计思想采用明德扬至简设计法.VGA是最常见的视频显示接口,时序也较为简单.本文从利用显示屏通过VGA方式显示测试图案及静态图片着手带大家接触图像显示应用,算是为后续VGA显示摄像头采 ...

  4. 电脑出现问题如何修复Windows 10

    也许Windows 10无法启动.或者它可能会靴子,但会崩溃很多.在任何一种情况下,您都需要在使用PC之前解决问题.以下是修复Windows 10的几种方法. 方法1:使用Windows启动修复 如果 ...

  5. he

    弄好这个网站---to thi tha think 好这个---, 很温馨 那时候我还在看. 前一段时候看yibenhaoshu,走出来的才是理性,所以现在才是理性的看待的. 回头再看看两年前的事情, ...

  6. 文本分类实战(二)—— textCNN 模型

    1 大纲概述 文本分类这个系列将会有十篇左右,包括基于word2vec预训练的文本分类,与及基于最新的预训练模型(ELMo,BERT等)的文本分类.总共有以下系列: word2vec预训练词向量 te ...

  7. 微信小程序开发 (资料汇总,谁还没被坑过?希望助你绕过一些坑)

    最近帮人家做一个微信小程序,刚好想熟悉一下.由于牵扯到多用户使用系统,以及数据共享,所以自然架构选择了,客户端和服务器的方式. 后台服务器是windows server,后台程序是.Net  WebA ...

  8. docker私有镜像仓库搭建

    环境:centos7,dockere版本:18.09.0,镜像仓库:v2 docker-registry:192.168.137.101   docker私有仓库服务器 docker-app: 192 ...

  9. D. The Beatles

    链接 [https://codeforces.com/contest/1143/problem/D] 题意 就是有nkcity,n个面包店 第一个面包店在1city,第x个在(x-1)k+1city ...

  10. (PAT)L2-006 树的遍历 (二叉树构建)

    题目链接:https://www.patest.cn/contests/gplt/L2-006 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格 ...