Nicksxs's Blog

What hurts more, the pain of hard work or the pain of regret?

0%

聊聊 mysql 索引的一些细节

前几天同事问了我个 mysql 索引的问题,虽然大概知道,但是还是想来实践下,就是 is null,is not null 这类查询是否能用索引,可能之前有些网上的文章说都是不能用索引,但是其实不是,我们来看个小试验

1
2
3
4
5
6
7
8
9
10
CREATE TABLE `null_index_t` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`null_key` varchar(255) DEFAULT NULL,
`null_key1` varchar(255) DEFAULT NULL,
`null_key2` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_1` (`null_key`) USING BTREE,
KEY `idx_2` (`null_key1`) USING BTREE,
KEY `idx_3` (`null_key2`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

用个存储过程来插入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

delimiter $ #以delimiter来标记用$表示存储过程结束
create procedure nullIndex1()
begin
declare i int;
declare j int;
set i=1;
set j=1;
while(i<=100) do
while(j<=100) do
IF (i % 3 = 0) THEN
INSERT INTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (null , LEFT(MD5(RAND()), 8), LEFT(MD5(RAND()), 8));
ELSEIF (i % 3 = 1) THEN
INSERT INTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (LEFT(MD5(RAND()), 8), NULL, LEFT(MD5(RAND()), 8));
ELSE
INSERT INTO null_index_t ( `null_key`, `null_key1`, `null_key2` ) VALUES (LEFT(MD5(RAND()), 8), LEFT(MD5(RAND()), 8), NULL);
END IF;
set j=j+1;
end while;
set i=i+1;
set j=1;
end while;
end
$
call nullIndex1();

然后看下我们的 is null 查询

1
EXPLAIN select * from null_index_t WHERE null_key is null;


再来看看另一个

1
EXPLAIN select * from null_index_t WHERE null_key is not null;


从这里能看出来啥呢,可以思考下

请我喝杯咖啡