MySQL_列值为null对索引的影响_实践
一.首先看一个我在某公众号看到的一个关于数据库优化的举措

二.如果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对索引的影响_实践的更多相关文章
- 将表中null值替换成想要的值、查询某一列值为null
用到ISNULL()函数 例如:SELECT 其他列名,ISNULL(列名,替换值)as 重命名 from 表名 (简单参考:http://www.cnblogs.com/netsa/archive ...
- SQL Sever中多列拼接成一列值为NULL
查询出数据 SELECT a.ID AS KYMain_ID , ',' + a.Leader + ',' AS KYMain_Leader , ), b.TaskLeader) FROM TB_KY ...
- SQL判断如果一列值为null则取另一列值代替 isnull()
[chClientCode] ,[nvcClientName] ,[chRegionCode] ,isnull(chUltimateHeadClientCode,[chClientCode]) as ...
- 从DB灌值到DataTable时,字段值为NULL时报错相关信息;
报错信息: 1. 2. 3. 4. 5. 6. 解决方法: 1. Data Layer SQL 语句取数据时,把其列值有为null的字段用0.00替换,(ISNULL的用法): 2. #r ...
- BLOB或TEXT字段使用散列值和前缀索引优化提高查询速度
1.创建表,存储引擎为myisam,对大文本字段blob使用MD5函数建立一个散列值 create table t2(id varchar(60), content blob, hash_value ...
- NULL值比较,两个列的合并,列值按条件替换。
show create table 表名 -- 显示创建表的sql语句. 为已有的表增加新列.alter table 表名 add 列名 int NULL -- 此行加了一个int 类型 默认可以nu ...
- MySQL5.7 虚拟列实现表达式或函数索引
MySQL5.7 虚拟列实现表达式或函数索引 http://www.linuxidc.com/Linux/2015-11/125162.htm https://dev.mysql.com/doc/re ...
- mysql中生成列与JSON类型的索引
MySQL中支持生成列,生成列的值是根据列定义中包含的表达式计算的. 一个简单的例子来认识生成列! CREATE TABLE triangle( sidea DOUBLE, sideb DOUBLE, ...
- mysql 清空或删除表数据后,控制表自增列值的方法
http://blog.sina.com.cn/s/blog_68431a3b0100y04v.html 方法1: truncate table 你的表名 //这样不但将数据全部删除,而且重新定位自增 ...
随机推荐
- c/c++ 多线程 detach的困惑
多线程 detach的困惑 求大神解答: 1,当在一个函数里启动一个线程后,并detach了 2,detach的线程里使用了这个函数里new出来的一个对象 3,detach后,delete了这个对象 ...
- git 命令添加整个文件夹以及文件夹下的内容
对于一个文件夹提交到服务器上,喜欢用 git add .(后面为".") 这种情况对于一个文件夹的还是很有用的,但出现了多个不同文件夹后,要分别提交就不能这么用了, 可以使用如下指 ...
- mybatis使用oracle的nulls first/nulls last
nulls first/nulls last 顾名思义,就是在检索结果集里,有null值的时候,把null值认为是最大值,还是最小值. nulls first 放置在结果集最前面 nulls last ...
- LNMP构建动态网站WordPress
LNMP构建动态网站wordpress 一.部署LNMP架构 1.安装nginx #配置nginx源 cat>/etc/yum.repos.d/nginx.repo<<-EOF [N ...
- Java学习笔记记录(二)
1.复合语句 if条件语句 使用场景:boolean类型判断.一个范围的判断.几个常量值的判断 有左大括号就没有分号,有分号就没有左大括号. 如下: public class demo1 { stat ...
- Operation category READ is not supported in state standby
Namenode 开启HA之后,由于zookeeper异常,出现脑裂现象 执行 $./hdfs haadmin -getServiceState nn1 ...
- gulp配置(编译压缩转码自动刷新注释全)
参考自:http://www.sheyilin.com/2016/02/gulp_introduce/ 在原先基础上增加了less编译 es6转码资源地图等,修改了一部分的热刷新. gulpfile. ...
- JavaScript面向对象—基本数据类型和引用数据类型的区别和变量及作用域(函数和变量)
基本类型和引用类型的值 ECMAScript 变量可能包含两种不同的数据类型的值:基本类型值和引用类型值. 基本类型值指的是那些保存在栈内存中的简单数据段,即这种值完全保存在内存中的一个位置. 而引用 ...
- 学号 20175329 2018-2019-3《Java程序设计》第八周学习总结
学号 20175329 2018-2019-3<Java程序设计>第八周学习总结 教材学习内容总结 第十五章 泛型 可以使用"class 名称"声明一个类,为了和普通的 ...
- django的中英文支持及切换
版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 1.http://mlocati.github.io/articles/gettext-iconv-w ...