MySQL MyISAM 表是否处理索引中的空值?

Do MySQL MyISAM Tables handle null in indexes?

我使用 MySQL 5.5 创建了这个 MySQL table:

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 值时没有使用索引呢?这是经常发生的事情吗?

您问的是:

But why does the index not get used when searching for NULL values?

NULL 是 SQL 中的一种特殊类型的值...它无法通过 =<><= 等所有比较测试。所以普通的BTREE有序索引是没有意义的。

Is that a regular occurrence?

确实如此。