SupCube

  • 主页
  • 随笔
所有文章 友链 关于我

SupCube

  • 主页
  • 随笔

Mysql-NULL值探索

2020-03-30 阅读量6463

Mysql中NULL和‘’的区别;

在Mysql中字段中可以存储为NULL和‘’;

对于NULL就是在字段中存储NULL值,空值在字段中存在‘’本次主要从存储空间、插入查询方式、count 函数使用区别以及索引字段这四个方面来讲述两者区别;

1.空间占用

在此我想说下mysql中的两个函数:length( )和char_length( )。

length( ) 返回被计算字符串 占用的 字节数;

char_length( )返回被计算字符串占用的字符长度;

我们通过:length来查询 NULL,'',以及‘0’的字节长度;

mysql>  select length(NULL), length(''), length('0');
+--------------+------------+-------------+
| length(NULL) | length('') | length('0') |
+--------------+------------+-------------+
|         NULL |          0 |           1 |
+--------------+------------+-------------+
1 row in set (0.00 sec)

可以发现NULL占用 NULL 字节,''占用0字节,那么NULL到底是多少空间呢?
Mysql 官方文档:NULL columns require additional space in the rowto record whether their values are NULL. For MyISAM tables, each NULL columntakes one bit extra, rounded up to the nearest byte
.通过文档翻译大概意思是:一个null字段占用1bit...;

尽管很小但是还是占用空间的

总价如下:‘’字符串不占用空间空间为0.但是NULL占用1bit;

2.插入查询方式

NULL字段查询不能用= 、<> 、或者>等操作。

必须使用  is null  或者is  not null 来查询;

但是‘’字段可以用 = 、<> 、或者>等操作。

3.count 函数使用

 有如下表:

mysql> select * from user;
+----+---------+
| id | name    |
+----+---------+
|  2 | tom     |
|  3 | supcube |
|  4 | NULL    |
+----+---------+
3 rows in set (0.00 sec)

如果按照name查询个数系统会自动过滤掉null值:

mysql> select count(name) from user;
+-------------+
| count(name) |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

4.索引使用

我在user表中新增索引:index_name (name)

mysql> show keys from user;
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  |          0 | PRIMARY    |            1 | id          | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
| user  |          1 | index_name |            1 | name        | A         |           3 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)

再执行查询查询空值:

mysql> explain
    -> select * from user where name is NULL;
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key        | key_len | ref   | rows | filtered | Extra                    |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+--------------------------+
|  1 | SIMPLE      | user  | NULL       | ref  | index_name    | index_name | 768     | const |    1 |   100.00 | Using where; Using index |
+----+-------------+-------+------------+------+---------------+------------+---------+-------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

由此看见mysql空值不影响索引使用,查询NULL值也不影响索引;

但在sqlserver中:

在SQL SERVER中,定长记录的NULL值占用存储空间,而变长记录的NULL值不占用存储空间。(结论来源:https://blog.csdn.net/h2503652646/article/details/84991240  )
赏

谢谢你请我吃糖果

支付宝
微信

扫一扫,分享到微信

微信分享二维码
ArrayList 与 LinkList 比较
Mysql-常用命令
京公网安备11011102002544 © 京ICP备2024068849号-1
智汇魔方:http://www.supcube.com
  • 所有文章
  • 友链
  • 关于我
  • 切水果
  • json格式化
  • 自动分组
  • 集合差集