我们可以在 MySQL 5.7 中为包含 NULL 值的列创建索引吗?
Can we create an index on a column containing NULL values in MySQL 5.7?
以下是 MySQL 文档中的文本:
The primary key for a table represents the column or set of columns
that you use in your most vital queries. It has an associated index,
for fast query performance. Query performance benefits from the NOT
NULL optimization, because it cannot include any NULL values.
我不明白上面加粗字体的句子的确切含义。有没有人给我解释一下。
此外,请问我们是否可以在 MySQL 5.7 中的包含 NULL 值的列上创建索引?如果不是,是什么原因?
谢谢。
A PRIMARY 键用于组织磁盘上的数据,因为与数据的物理排列方式有关,所以主键的任何部分都不能为 NULL .
非主索引可以有一个或多个允许 NULL 的列。 demo
CREATE TABLE `my_docs` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`rev` int(3) ,
`content` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `my_docs` (`rev`, `content`) VALUES
(1, 'The earth is flat'),
(1, 'One hundred angels can dance on the head of a pin'),
(NULL, 'The earth is flat and rests on a bull\'s horn'),
(3, 'The earth is like a ball.');
alter table `my_docs` add key my_added_index (`rev`);
SELECT id, rev, content
FROM `my_docs`
where rev = 3
| id | rev | content |
|----|-----|---------------------------|
| 4 | 3 | The earth is like a ball. |
+---+-------------+-----------+------+----------------+----------------+---------+-------+------+----------+-------+
| d | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+---+-------------+-----------+------+----------------+----------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | my_docs | ref | my_added_index | my_added_index | 5 | const | 1 | 100.00 | |
+---+-------------+-----------+------+----------------+----------------+---------+-------+------+----------+-------+
以下是 MySQL 文档中的文本:
The primary key for a table represents the column or set of columns that you use in your most vital queries. It has an associated index, for fast query performance. Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values.
我不明白上面加粗字体的句子的确切含义。有没有人给我解释一下。
此外,请问我们是否可以在 MySQL 5.7 中的包含 NULL 值的列上创建索引?如果不是,是什么原因?
谢谢。
A PRIMARY 键用于组织磁盘上的数据,因为与数据的物理排列方式有关,所以主键的任何部分都不能为 NULL .
非主索引可以有一个或多个允许 NULL 的列。 demo
CREATE TABLE `my_docs` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`rev` int(3) ,
`content` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `my_docs` (`rev`, `content`) VALUES
(1, 'The earth is flat'),
(1, 'One hundred angels can dance on the head of a pin'),
(NULL, 'The earth is flat and rests on a bull\'s horn'),
(3, 'The earth is like a ball.');
alter table `my_docs` add key my_added_index (`rev`);
SELECT id, rev, content
FROM `my_docs`
where rev = 3
| id | rev | content |
|----|-----|---------------------------|
| 4 | 3 | The earth is like a ball. |
+---+-------------+-----------+------+----------------+----------------+---------+-------+------+----------+-------+
| d | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+---+-------------+-----------+------+----------------+----------------+---------+-------+------+----------+-------+
| 1 | SIMPLE | my_docs | ref | my_added_index | my_added_index | 5 | const | 1 | 100.00 | |
+---+-------------+-----------+------+----------------+----------------+---------+-------+------+----------+-------+