SQL
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.

MySQL难以优化引用可空列查询,它会使索引、索引统计和值更加复杂。可空列需要更多的存储空间,还需要MySQL内部进行特殊处理。可空列被索引后,每条记录都需要一个额外的字节,还能导致MYisam 中固定大小的索引变成可变大小的索引。
—— 出自《高性能MySQL第二版》

不用 Null 的理由

  1. 所有使用NULL值的情况,都可以通过一个有意义的值的表示,这样有利于代码的可读性和可维护性,并能从约束上增强业务数据的规范性。
  2. NULL值到非NULL的更新无法做到原地更新,更容易发生索引分裂,从而影响性能。 :::tips 注意:但把NULL列改为NOT NULL带来的性能提示很小,除非确定它带来了问题,否则不要把它当成优先的优化措施,最重要的是使用的列的类型的适当性。 :::

  3. NULL值在timestamp类型下容易出问题,特别是没有启用参数explicit_defaults_for_timestamp

  4. NOT IN!= 等负向条件查询在有 NULL 值的情况下返回永远为空结果,查询容易出错

举例:

  1. create table table_2 (
  2. `id` INT (11) NOT NULL,
  3. user_name varchar(20) NOT NULL
  4. )
  5. create table table_3 (
  6. `id` INT (11) NOT NULL,
  7. user_name varchar(20)
  8. )
  9. 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")
  10. insert into table_3 values (1,"zhaoliu_2_1"),(2, null)
  11. -- 1NOT IN子查询在有NULL值的情况下返回永远为空结果,查询容易出错
  12. select user_name from table_2 where user_name not in (select user_name from table_3 where id!=1)
  13. mysql root@10.48.186.32:t_test_zz5431> select user_name from table_2 where user_name not
  14. -> in (select user_name from table_3 where id!=1);
  15. +-------------+
  16. | user_name |
  17. |-------------|
  18. +-------------+
  19. 0 rows in set
  20. Time: 0.008s
  21. mysql root@10.48.186.32:t_test_zz5431>
  22. -- 2、单列索引不存null值,复合索引不存全为null的值,如果列允许为null,可能会得到“不符合预期”的结果集
  23. -- 如果name允许为null,索引不存储null值,结果集中不会包含这些记录。所以,请使用not null约束以及默认值。
  24. select * from table_3 where name != 'zhaoliu_2_1'
  25. -- 3、如果在两个字段进行拼接:比如题号+分数,首先要各字段进行非null判断,否则只要任意一个字段为空都会造成拼接的结果为null
  26. select CONCAT("1",null) from dual; -- 执行结果为null
  27. -- 4、如果有 Null column 存在的情况下,count(Null column)需要格外注意,null 值不会参与统计。
  28. mysql root@10.48.186.32:t_test_zz5431> select * from table_3;
  29. +------+-------------+
  30. | id | user_name |
  31. |------+-------------|
  32. | 1 | zhaoliu_2_1 |
  33. | 2 | <null> |
  34. | 21 | zhaoliu_2_1 |
  35. | 22 | <null> |
  36. +------+-------------+
  37. 4 rows in set
  38. Time: 0.007s
  39. mysql root@10.48.186.32:t_test_zz5431> select count(user_name) from table_3;
  40. +--------------------+
  41. | count(user_name) |
  42. |--------------------|
  43. | 2 |
  44. +--------------------+
  45. 1 row in set
  46. Time: 0.007s
  47. -- 5、注意 Null 字段的判断方式, = null 将会得到错误的结果。
  48. mysql root@localhost:cygwin> create index IDX_test on table_3 (user_name);
  49. Query OK, 0 rows affected
  50. Time: 0.040s
  51. mysql root@localhost:cygwin> select * from table_3 where user_name is null\G
  52. ***************************[ 1. row ]***************************
  53. id | 2
  54. user_name | None
  55. 1 row in set
  56. Time: 0.002s
  57. mysql root@localhost:cygwin> select * from table_3 where user_name = null\G
  58. 0 rows in set
  59. Time: 0.002s
  60. mysql root@localhost:cygwin> desc select * from table_3 where user_name = 'zhaoliu_2_1'\G
  61. ***************************[ 1. row ]***************************
  62. id | 1
  63. select_type | SIMPLE
  64. table | table_3
  65. type | ref
  66. possible_keys | IDX_test
  67. key | IDX_test
  68. key_len | 23
  69. ref | const
  70. rows | 1
  71. Extra | Using where
  72. 1 row in set
  73. Time: 0.006s
  74. mysql root@localhost:cygwin> desc select * from table_3 where user_name = null\G
  75. ***************************[ 1. row ]***************************
  76. id | 1
  77. select_type | SIMPLE
  78. table | None
  79. type | None
  80. possible_keys | None
  81. key | None
  82. key_len | None
  83. ref | None
  84. rows | None
  85. Extra | Impossible WHERE noticed after reading const tables
  86. 1 row in set
  87. Time: 0.002s
  88. mysql root@localhost:cygwin> desc select * from table_3 where user_name is null\G
  89. ***************************[ 1. row ]***************************
  90. id | 1
  91. select_type | SIMPLE
  92. table | table_3
  93. type | ref
  94. possible_keys | IDX_test
  95. key | IDX_test
  96. key_len | 23
  97. ref | const
  98. rows | 1
  99. Extra | Using where
  100. 1 row in set
  101. Time: 0.002s
  102. mysql root@localhost:cygwin>

Null 列需要更多的存储空间:需要一个额外字节作为判断是否为 NULL 的标志位

举例:

  1. alter table table_3 add index idx_user_name (user_name);
  2. alter table table_2 add index idx_user_name (user_name);
  3. explain select * from table_2 where user_name='zhaoliu_2_1';
  4. explain select * from table_3 where user_name='zhaoliu_2_1';

SQL中不用 Null 的理由 - 图1
可以看到同样的 varchar(20) 长度,table_2 要比 table_3 索引长度大,这是因为:
两张表的字符集不一样,且字段一个为 NULL 一个非 NULL。
SQL中不用 Null 的理由 - 图2
key_len 的计算规则和三个因素有关:数据类型、字符编码、是否为 NULL

  1. key_len 62 == 20*3(utf8 3字节) + 2 (存储 varchar 变长字符长度 2字节,定长字段无需额外的字节)
  2. key_len 83 == 20*4(utf8mb4 4字节) + 1 (是否为 Null 的标识) + 2 (存储 varchar 变长字符长度 2字节,定长字段无需额外的字节)

所以说索引字段最好不要为NULL,因为NULL会使索引、索引统计和值更加复杂,并且需要额外一个字节的存储空间。