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)

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:1024 设计师:我叫白小胖 返回首页

打赏作者

海棠之花

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值