mysql null值 索引_MySQL-mysql 含NULL值的column索引
这种说法是错的:”MySql 中是不能用 null 作索引的,任何包含null值的列都将不会被包含在索引中“
我们做个实验如下,可以看到b字段建有索引,b 字段中有 null,但是
select count(*) from a group by b
用了b上的覆盖索引。 如果索引里不包含null的话,只用覆盖索引是 不能满足要求的(无法计算出b中null的个数)。
mysql> show create table a;
+-------+-------------------------------------------------------------------------------------------
----------------------------------------+
| Table | Create Table
|
+-------+-------------------------------------------------------------------------------------------
----------------------------------------+
| a | CREATE TABLE `a` (
`a` int(11) DEFAULT NULL,
`b` int(11) DEFAULT NULL,
KEY `k` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------
----------------------------------------+
1 row in set (0.03 sec)
mysql> select * from a;
+------+------+
| a | b |
+------+------+
| 1 | NULL |
| 2 | NULL |
| 2 | 23 |
| 2 | 45 |
| 2 | 4 |
| 2 | 43 |
+------+------+
6 rows in set (0.00 sec)
mysql> explain select count(*) from a group by b;
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | a | index | NULL | k | 5 | NULL | 6 | Using index |
+----+-------------+-------+-------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)
再来:"任何在where子句中使用is null或is not null的语句优化器是不允许使用索引的。"
mysql> show index from t3;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+-
-------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+-
-------+------+------------+---------+---------------+
| t3 | 1 | isd | 1 | uid | A | 200 | NULL |
NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+-
-------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> select count(*) from t3 where uid is null;
+----------+
| count(*) |
+----------+
| 3009 |
+----------+
1 row in set (0.03 sec)
mysql> explain select count(*) from t3 where uid is null;
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------
---------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
|
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------
---------+
| 1 | SIMPLE | t3 | ref | isd | isd | 5 | const | 3008 | Using where; Usi
ng index |
+----+-------------+-------+------+---------------+------+---------+-------+------+-----------------
---------+
1 row in set (0.00 sec)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)