mysql字段为NULL索引是否会失效实例详解

2022-07-14,,,,

项目场景:

很多博客说mysql在字段中创建普通索引,如果该索引中的数据存在null值是不走索引这个结论是错误的,不过尽量还是设置默认值。(版本8.0低于这个版本可能结果不一致)

1、创建表sc_base_color,其中普通索引为 “name,group_num”,这里暂时不测组合索引,下面再测试。

create table `sc_base_color` (
  `id` bigint not null auto_increment,
  `group_num` bigint default null comment '颜色代码',
  `name` varchar(255) character set utf8 collate utf8_general_ci default null comment '颜色名称',
  primary key (`id`) using btree,
  key `idx_name` (`name`),
  key `idx_group_num` (`group_num`)
) engine=innodb auto_increment=574 default charset=utf8mb3 comment='颜色';

2、初始化测试数据

insert into `sc_base_color`(`id`, `group_num`, `name`) values (30, 1, '米黄');
insert into `sc_base_color`(`id`, `group_num`, `name`) values (31, 1, '黑色');
insert into `sc_base_color`(`id`, `group_num`, `name`) values (32, 1, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (33, 1, '白色');
insert into `sc_base_color`(`id`, `group_num`, `name`) values (34, 1, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (35, 1, '绿色');
insert into `sc_base_color`(`id`, `group_num`, `name`) values (36, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (37, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (38, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (39, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (40, null, '紫色');
insert into `sc_base_color`(`id`, `group_num`, `name`) values (41, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (42, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (43, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (44, null, '蓝色');
insert into `sc_base_color`(`id`, `group_num`, `name`) values (45, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (46, null, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (47, 2, '米蓝色');
insert into `sc_base_color`(`id`, `group_num`, `name`) values (48, 2, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (49, 2, null);
insert into `sc_base_color`(`id`, `group_num`, `name`) values (50, 2, '黑红色');

3、测试普通索引为null的情况是否使用了索引

使用 = 查询,测试结果中使用到了索引,其中索引字段的值为“null”

explain select * from sc_base_color where name = '米黄';
explain select * from sc_base_color where group_num = 1;

截图结果,两列数据都存在空,最终走了索引。

使用 大于、小于 查询

explain select * from sc_base_color where name > '米黄';
explain select * from sc_base_color where name < '米黄';

截图结果

使用 不等于、not in 、isnull、!isnull查询

explain select * from sc_base_color where group_num != 1;
explain select * from sc_base_color where group_num not in (1);
explain select * from sc_base_color where  isnull(group_num);
explain select * from sc_base_color where  !isnull(group_num);

截图结果

使用isnull、is not null查询

# 使用is not null可能会导致索引失效,我测试了20条数据,只要null值占全部数据的百分之50就不会失效,否则会失效。又测了40条数据,23条数据不会为空,22条为null的会为空
explain select  * from sc_base_color where  group_num is not null;
# 使用is null也可能会导致索引失效,我测试了20条数据,6数数据不为空不会失效,也就是可能当空的数据占比70%的时候索引会失效。
explain select  * from sc_base_color where  group_num is  null;

由此可以得出结论,字段为空是可以走索引的,但是部分场景可能会失效,尽量还是给默认值。

4、测试组合索引为null是否走了索引

先删除普通索引字段,增加组合索引

alter table sc_base_color drop index idx_group_num;
alter table sc_base_color drop index idx_name;
alter table `sc_base_color` add index idx_group_num_idx_name (group_num, name);

测试 = > < 查询结果

explain select  * from sc_base_color where  group_num > 1;
explain select  * from sc_base_color where  group_num < 1;
explain select  * from sc_base_color where  group_num = 1;
explain select  * from sc_base_color where group_num = 1 and name = '米黄';

截图结果,是可以走索引的,下面的逻辑就不用测试了和普通索引一样,除非不符合最左匹配原则直接查询name字段。

5、总结

在设计数据库的时候尽量还是给字段的默认值。

1、比如int、bigint类型默认值为-1/0

2、比如varchar类型默认值为空串

3、bigdecimal类型为0等等。

null值会有不少坑

1、count(字段null)会过滤统计的数据,sum这些函数也会

2、使用> < 的时候也会过滤掉为null的数据

3、group by 的时候会把所有为null的数据合并,可以随机生成uuid解决

4、还有场景可能也有问题,这里我也忘记了,用的时候才会想起来。

总结

到此这篇关于mysql字段为null索引是否会失效的文章就介绍到这了,更多相关mysql字段null索引失效内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《mysql字段为NULL索引是否会失效实例详解.doc》

下载本文的Word格式文档,以方便收藏与打印。