![](/img/trans.png)
[英]Can you create an index to determine if a text column is null in mysql?
[英]Can we create an index on a column containing NULL values in MySQL 5.7?
以下是MySQL文檔中的文本:
表的主鍵代表您在最重要的查詢中使用的一列或一組列。 它具有關聯的索引,可提高查詢性能。 查詢性能得益於NOT NULL優化,因為它不能包含任何NULL值 。
從上面的文本中,我不理解該粗體字句的確切含義。 有人請向我解釋。
另外,讓我知道我們是否可以在MySQL 5.7中包含NULL值的列上創建索引? 若否,原因何在?
謝謝。
PRIMARY鍵用於組織磁盤上的數據,並且由於與數據的物理排列方式有關系,因此主鍵的任何部分都不能為NULL。
非主索引可以具有一個或多個允許為NULL的列。 演示
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 | |
+---+-------------+-----------+------+----------------+----------------+---------+-------+------+----------+-------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.