作者: Kris-Q
来源:https://www.cnblogs.com/myJuly/p/13396756.html

1、NULL 为什么这么多人用?


2、是不是以讹传讹?

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.

3、给我一个不用 Null 的理由?

注意:但把NULL列改为NOT NULL带来的性能提示很小,除非确定它带来了问题,否则不要把它当成优先的优化措施,最重要的是使用的列的类型的适当性。
举例:
create table table_2 (`id` INT (11) NOT NULL,user_name varchar(20) NOT NULL)create table table_3 (`id` INT (11) NOT NULL,user_name varchar(20))insert into table_2 values (4,"zhaoliu_2_1"),(2,"lisi_2_1"),(3,"wangmazi_2_1"),(1,"zhangsan_2"),(2,"lisi_2_2"),(4,"zhaoliu_2_2"),(3,"wangmazi_2_2")insert into table_3 values (1,"zhaoliu_2_1"),(2, null)-- 1、NOT IN子查询在有NULL值的情况下返回永远为空结果,查询容易出错select user_name from table_2 where user_name not in (select user_name from table_3 where id!=1)mysql root@10.48.186.32:t_test_zz5431> select user_name from table_2 where user_name not-> in (select user_name from table_3 where id!=1);+-------------+| user_name ||-------------|+-------------+0 rows in setTime: 0.008smysql root@10.48.186.32:t_test_zz5431>-- 2、单列索引不存null值,复合索引不存全为null的值,如果列允许为null,可能会得到“不符合预期”的结果集-- 如果name允许为null,索引不存储null值,结果集中不会包含这些记录。所以,请使用not null约束以及默认值。select * from table_3 where name != 'zhaoliu_2_1'-- 3、如果在两个字段进行拼接:比如题号+分数,首先要各字段进行非null判断,否则只要任意一个字段为空都会造成拼接的结果为null。select CONCAT("1",null) from dual; -- 执行结果为null。-- 4、如果有 Null column 存在的情况下,count(Null column)需要格外注意,null 值不会参与统计。mysql root@10.48.186.32:t_test_zz5431> select * from table_3;+------+-------------+| id | user_name ||------+-------------|| 1 | zhaoliu_2_1 || 2 | <null> || 21 | zhaoliu_2_1 || 22 | <null> |+------+-------------+4 rows in setTime: 0.007smysql root@10.48.186.32:t_test_zz5431> select count(user_name) from table_3;+--------------------+| count(user_name) ||--------------------|| 2 |+--------------------+1 row in setTime: 0.007s-- 5、注意 Null 字段的判断方式, = null 将会得到错误的结果。mysql root@localhost:cygwin> create index IDX_test on table_3 (user_name);Query OK, 0 rows affectedTime: 0.040smysql root@localhost:cygwin> select * from table_3 where user_name is null\G***************************[ 1. row ]***************************id | 2user_name | None1 row in setTime: 0.002smysql root@localhost:cygwin> select * from table_3 where user_name = null\G0 rows in setTime: 0.002smysql root@localhost:cygwin> desc select * from table_3 where user_name = 'zhaoliu_2_1'\G***************************[ 1. row ]***************************id | 1select_type | SIMPLEtable | table_3type | refpossible_keys | IDX_testkey | IDX_testkey_len | 23ref | constrows | 1Extra | Using where1 row in setTime: 0.006smysql root@localhost:cygwin> desc select * from table_3 where user_name = null\G***************************[ 1. row ]***************************id | 1select_type | SIMPLEtable | Nonetype | Nonepossible_keys | Nonekey | Nonekey_len | Noneref | Nonerows | NoneExtra | Impossible WHERE noticed after reading const tables1 row in setTime: 0.002smysql root@localhost:cygwin> desc select * from table_3 where user_name is null\G***************************[ 1. row ]***************************id | 1select_type | SIMPLEtable | table_3type | refpossible_keys | IDX_testkey | IDX_testkey_len | 23ref | constrows | 1Extra | Using where1 row in setTime: 0.002smysql root@localhost:cygwin>
举例:
alter table table_3 add index idx_user_name (user_name);
alter table table_2 add index idx_user_name (user_name);
explain select * from table_2 where user_name='zhaoliu_2_1';
explain select * from table_3 where user_name='zhaoliu_2_1';

可以看到同样的 varchar(20) 长度,table_2 要比 table_3 索引长度大,这是因为:
两张表的字符集不一样,且字段一个为 NULL 一个非 NULL。

作者: Kris-Q
来源:https://www.cnblogs.com/myJuly/p/13396756.html
版权申明:内容来源网络,仅供学习研究,版权归原创者所有。如有侵权烦请告知,我们会立即删除并表示歉意。谢谢!
编辑推荐:
- MySQL 为何不建议使用 NULL ?03-01
- 一个 MySQL 数据库死锁的案例和解决方案03-01
- MySQL的Json类型字段IN查询分组和优化方法03-01
- 第三代分布式数据库(1)——踢球时代03-01
- MySQL 死锁后事务无法回滚是真的吗?03-01
- 从数据库发展历程到数据结构设计探析03-01
- ansible安装GreatSQL构建MGR集群03-01
- MySQL 复制错误 1837 的相关缺陷一例03-01
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
