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

二.如果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. Python变量之白首如新,倾盖如故

    python中的变量变量:将运算的中间结果暂存到内存中,方便后续程序调用.变量的命名规则:1.变量名由字母.数字.下划线组成.2.变量名可以用字母.下划线开头,但是不能以数字开头.3.变量名是区分大小 ...

  2. django 创建admin用户名跟密码

    一.django中创建用户名和密码 (venv) D:\project\py37project\Djangopro\Procrm>Python37 manage.py createsuperus ...

  3. c++11の简单线程管理

    1.简单的例子 #include "stdafx.h" #include <iostream> #include <thread> void functio ...

  4. SpringMVC处理请求和返回流程

    流程描述:一个url请求,找打指定的requestMapping再返回指定的jsp界面 通过url拿到指定的java方法 HandlerExecutionChain  mappedHandler = ...

  5. SpringBoot中使用Servlet,Filter,Listener

    项目最近在替换之前陈旧的框架,改用SpringBoot进行重构,初接触,暂时还没有用到Servlet,Filter,Listener的地方,但在之前回顾Servlet的生命周期时,https://ww ...

  6. 日版iphone5 SB 配合REBELiOS卡贴破解电信3G步骤

    1.插入贴膜卡和sim卡:进入“设置—电话—sim卡应用程序”选择CDMA电信解锁: 2.越狱设备,添加cydia.gpplte.com源,安装“6S/6/5S/5C/5电信新补丁”: 3.打卡gpp ...

  7. Vultr CentOS 7 安装 Docker

    前言 最近在梳理公司的架构,想用 VPS 先做一些测试,然后就开始踩坑了!我用 Vultr 新买了个 VPS. 安装的 CentOS 版本: [root@dbn-seattle ~]# cat /et ...

  8. .net core EF的简单使用

    1.在mysql中新建一个表 2.在控制台中装个EF包  Install-Package Pomelo.EntityFrameworkCore.MySql 3.新建一个Person类 4.创建DbCo ...

  9. 浮点数乘积的取整intval,以及高精度函数bcmath的使用

    线上发现个bug,浮点数乘积以后取整,得到的数不符预期.还记得上次踩过的坑是数据库类型转换的一个问题.这个也相当于类型转换了..尴尬 浮点数计算的精度一定要谨慎. 例子如下: <?php $a ...

  10. H5 百度一下,你就知道

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...