[英]Do MySQL MyISAM Tables handle null in indexes?
我使用MySQL 5.5創建了這個MySQL表:
mysql> CREATE TEMPORARY TABLE IF NOT EXISTS TableOne
-> (high INT NOT NULL, low INT NOT NULL, current INT NULL,
-> INDEX connect (low, current), INDEX theHigh (high), INDEX theCurrent (current));
Query OK, 0 rows affected (0.11 sec)
然后,我測試了兩個查詢:
mysql> describe SELECT * FROM TableOne WHERE current IS NOT NULL;
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | TableOne | ALL | theCurrent | NULL | NULL | NULL | 9238 | Using where |
+----+-------------+----------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
mysql> describe SELECT * FROM TableOne WHERE current = 0;
+----+-------------+----------+------+---------------+------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------------+---------+-------+------+-------------+
| 1 | SIMPLE | TableOne | ref | theCurrent | theCurrent | 5 | const | 1 | Using where |
+----+-------------+----------+------+---------------+------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
??? 我意識到NULL是特殊的。 我意識到在“ UNIQUE”索引中您仍然允許使用多個NULL值,而例如,您將不允許使用多個“ 0”值。 但是,為什么在搜索NULL值時不使用索引? 那是經常發生的嗎?
您詢問:
但是,為什么在搜索NULL值時不使用索引?
NULL是SQL中的一種特殊類型的值...它未通過所有比較測試,例如=
, <>
, <=
等。 因此,普通的BTREE有序索引毫無意義。
那是經常發生的嗎?
這的確是。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.