不懂就問:為什麼MySQL不建議使用NULL作為列預設值?

語言: CN / TW / HK

點選進入“PHP開源社群”    

免費獲取進階面試、文件、影片資源

通常能聽到的答案是 使用了NULL值的列將會使索引失效 ,但是如果實際測試過一下,你就知道 IS NULL 會使用索引.所以上述說法有漏洞.

著急的人拉到最下邊看結論

Preface

Null is a special constraint of columns. The columns in table will be added null constrain if you do not define the column with “not null” key words explicitly when creating the table.Many programmers like to define columns by default because of the conveniences(reducing the judgement code of nullibility) what consequently cause some uncertainty of query and poor performance of database.

NULL 值是一種對列的特殊約束,我們建立一個新列時,如果沒有明確的使用關鍵字 not null 宣告該資料列, Mysql 會預設的為我們新增上 NULL 約束.

有些開發人員在建立資料表時,由於懶惰直接使用Mysql的預設推薦設定.(即允許欄位使用 NULL 值).而這一陋習很容易在使用 NULL 的場景中得出不確定的查詢結果以及引起資料庫效能的下降.

Introduce

Null is null means it is not anything at all,we cannot think of null is equal to ‘’ and they are totally different. MySQL provides three operators to handle null value:“IS NULL”,“IS NOT NULL”,"<=>" and a function ifnull(). IS NULL: It returns true,if the column value is null. IS NOT NULL: It returns true,if the columns value is not null. <=>: It’s a compare operator similar with “=” but not the same.It returns true even for the two null values. (eg. null <=> null is legal) IFNULL(): Specify two input parameters,if the first is null value then returns the second one. It’s similar with Oracle’s NVL() function.

NULL 並不意味著什麼都沒有,我們要注意  NULL 跟  '' (空值)是兩個完全不一樣的值.MySQL中可以操作 NULL 值操作符主要有三個.

  • IS NULL

  • IS NOT NULL

  • <=> 太空船操作符,這個操作符很像 = , select NULL<=>NULL 可以返回 true ,但是 select NULL=NULL 返回 false .

  • IFNULL 一個函式.怎麼使用自己查吧…反正我會了

Example

Null never returns true when comparing with any other values except null with “<=>”.

NULL 通過任一操作符與其它值比較都會得到 NULL ,除了 <=> .

([email protected] mysql3306.sock)[zlm]>create table test_null(
-> id int not null,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.02 sec)

([email protected] mysql3306.sock)[zlm]>insert into test_null values(1,'zlm');
Query OK, 1 row affected (0.00 sec)

([email protected] mysql3306.sock)[zlm]>insert into test_null values(2,null);
Query OK, 1 row affected (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select * from test_null;
+----+------+
| id | name |
+----+------+
| 1 | zlm |
| 2 | NULL |
+----+------+
2 rows in set (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select * from test_null where name=null;
Empty set (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select * from test_null where name is null;
+----+------+
| id | name |
+----+------+
| 2 | NULL |
+----+------+
1 row in set (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select * from test_null where name is not null;
+----+------+
| id | name |
+----+------+
| 1 | zlm |
+----+------+
1 row in set (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select * from test_null where null=null;
Empty set (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select * from test_null where null<>null;
Empty set (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select * from test_null where null<=>null;
+----+------+
| id | name |
+----+------+
| 1 | zlm |
| 2 | NULL |
+----+------+
2 rows in set (0.00 sec)

//null<=>null always return true,it's equal to "where 1=1".
Null means “a missing and unknown value”.Let’s see details below.

NULL代表一個不確定的值,就算是兩個NULL,它倆也不一定相等.(像不像C中未初始化的區域性變數)

([email protected] mysql3306.sock)[zlm]>SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;
+-----------+---------------+------------+----------------+
| 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |
+-----------+---------------+------------+----------------+
| 0 | 1 | 0 | 1 |
+-----------+---------------+------------+----------------+
1 row in set (0.00 sec)

//It's not equal to zero number or vacant string.
//In MySQL,0 means fasle,1 means true.

([email protected] mysql3306.sock)[zlm]>SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
| NULL | NULL | NULL | NULL |
+----------+-----------+----------+----------+
1 row in set (0.00 sec)

//It cannot be compared with number.
//In MySQL,null means false,too.
It truns null as a result if any expression contains null value.

任何有返回值的表示式中有 NULL 參與時,都會得到另外一個 NULL 值.

([email protected] mysql3306.sock)[zlm]>select ifnull(null,'First is null'),ifnull(null+10,'First is null'),ifnull(concat('abc',null),'First is null');
+------------------------------+---------------------------------+--------------------------------------------+
| ifnull(null,'First is null') | ifnull(null+10,'First is null') | ifnull(concat('abc',null),'First is null') |
+------------------------------+---------------------------------+--------------------------------------------+
| First is null | First is null | First is null |
+------------------------------+---------------------------------+--------------------------------------------+
1 row in set (0.00 sec)

//null value needs to be disposed with ifnull() function,what usually causes sql statement more complex.
//As we all know,MySQL does not support funcion index.Therefore,indexes on the column may not be used.That's really worse.
It’s diffrent when using count(*) & count(null column).

使用 count(*) 或者  count(null column) 結果不同, count(null column) <= count(*) .

([email protected] mysql3306.sock)[zlm]>select count(*),count(name) from test_null;
+----------+-------------+
| count(*) | count(name) |
+----------+-------------+
| 2 | 1 |
+----------+-------------+
1 row in set (0.00 sec)

//count(*) returns all rows ignore the null while count(name) returns the non-null rows in column "name".
//This will also leads to uncertainty if someone is unaware of the details above.
When using distinct,group by,order by,all null values are considered as the same value.

雖然 select NULL=NULL 的結果為 false ,但是在我們使用 distinct , group by , order by 時, NULL 又被認為是相同 .

([email protected] mysql3306.sock)[zlm]>insert into test_null values(3,null);
Query OK, 1 row affected (0.00 sec)

([email protected] mysql3306.sock)[zlm]>select distinct name from test_null;
+------+
| name |
+------+
| zlm |
| NULL |
+------+
2 rows in set (0.00 sec)

//Two rows of null value returned one and the result became two.

([email protected] mysql3306.sock)[zlm]>select name from test_null group by name;
+------+
| name |
+------+
| NULL |
| zlm |
+------+
2 rows in set (0.00 sec)

//Two rows of null value were put into the same group.
//By default,group by will also sort the result(null row showed first).

([email protected] mysql3306.sock)[zlm]>select id,name from test_null order by name;
+----+------+
| id | name |
+----+------+
| 2 | NULL |
| 3 | NULL |
| 1 | zlm |
+----+------+
3 rows in set (0.00 sec)

//Three rows were sorted(two null rows showed first).
MySQL supports to use index on column which contains null value(what’s different from oracle).

MySQL中支援在含有 NULL 值的列上使用索引,但是 Oracle 不支援.這就是我們平時所說的如果列上含有 NULL 那麼將會使索引失效.

嚴格來說,這句話對與MySQL來說是不準確的.

([email protected] mysql3306.sock)[sysbench]>show tables;
+--------------------+
| Tables_in_sysbench |
+--------------------+
| sbtest1 |
| sbtest10 |
| sbtest2 |
| sbtest3 |
| sbtest4 |
| sbtest5 |
| sbtest6 |
| sbtest7 |
| sbtest8 |
| sbtest9 |
+--------------------+
10 rows in set (0.00 sec)

([email protected] mysql3306.sock)[sysbench]>show create table sbtest1\G
*************************** 1. row ***************************
Table: sbtest1
Create Table: CREATE TABLE `sbtest1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_1` (`k`)
) ENGINE=InnoDB AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

([email protected] mysql3306.sock)[sysbench]>alter table sbtest1 modify k int null,modify c char(120) null,modify pad char(60) null;
Query OK, 0 rows affected (4.14 sec)
Records: 0 Duplicates: 0 Warnings: 0

([email protected] mysql3306.sock)[sysbench]>insert into sbtest1 values(100001,null,null,null);
Query OK, 1 row affected (0.00 sec)

([email protected] mysql3306.sock)[sysbench]>explain select id,k from sbtest1 where id=100001;
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | sbtest1 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

([email protected] mysql3306.sock)[sysbench]>explain select id,k from sbtest1 where k is null;
+----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
| 1 | SIMPLE | sbtest1 | NULL | ref | k_1 | k_1 | 5 | const | 1 | 100.00 | Using where; Using index |
+----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)

//In the first query,the newly added row is retrieved by primary key.
//In the second query,the newly added row is retrieved by secondary key "k_1"
//It has been proved that indexes can be used on the columns which contain null value.
//column "k" is int datatype which occupies 4 bytes,but the value of "key_len" turn out to be 5.what's happed?Because null value needs 1 byte to store the null flag in the rows.

這個是我自己測試的例子.

mysql> select * from test_1;
+-----------+------+------+
| name | code | id |
+-----------+------+------+
| gaoyi | wo | 1 |
| gaoyi | w | 2 |
| chuzhong | wo | 3 |
| chuzhong | w | 4 |
| xiaoxue | dd | 5 |
| xiaoxue | dfdf | 6 |
| sujianhui | su | 99 |
| sujianhui | NULL | 99 |
+-----------+------+------+
8 rows in set (0.00 sec)

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

mysql> explain select * from test_1 where code is not NULL;
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | test_1 | NULL | range | index_code | index_code | 161 | NULL | 7 | 100.00 | Using index condition |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from test_1 where code='dd';
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
| 1 | SIMPLE | test_1 | NULL | ref | index_code | index_code | 161 | const | 1 | 100.00 | Using index condition |
+----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from test_1 where code like "dd%";
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| 1 | SIMPLE | test_1 | NULL | range | index_code | index_code | 161 | NULL | 1 | 100.00 | Using index condition |
+----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

Summary 總結

null value always leads to many uncertainties when disposing sql statement.It may cause bad performance accidentally.

列中使用 NULL 值容易引發不受控制的事情發生,有時候還會嚴重託慢系統的效能.

例如:

null value will not be estimated in aggregate function() which may cause inaccurate results.

對含有NULL值的列進行統計計算,eg. count() , max() , min() ,結果並不符合我們的期望值.

null value will influence the behavior of the operations such as “distinct”,“group by”,“order by” which causes wrong sort.

干擾排序,分組,去重結果.

null value needs ifnull() function to do judgement which makes the program code more complex.

有的時候為了消除 NULL 帶來的技術債務,我們需要在SQL中使用 IFNULL() 來確保結果可控,但是這使程式變得複雜.

null value needs a extra 1 byte to store the null information in the rows.

NULL 值並是佔用原有的欄位空間儲存,而是額外申請一個位元組去標註,這個欄位添加了 NULL 約束.(就像額外的標誌位一樣)

As these above drawbacks,it’s not recommended to define columns with default null. We recommand to define “not null” on all columns and use zero number & vacant string to substitute relevant data type of null.

根據以上缺點,我們並不推薦在列中設定NULL作為列的預設值,你可以使用 NOT NULL 消除預設設定,使用 0 或者 '' 空字串來代替 NULL .

譯者:guangsu.

原文連結:blog.csdn.net/qq_30549099/article/details/107395521

END

PHP開源社群

掃描關注  進入”PHP資料“

免費獲取進階

面試、文件、影片資源