當前位置: 妍妍網 > 碼農

為什麽 MySQL 不推薦預設值為 null ?

2024-05-20碼農

來源: cnblogs.com/aaron8219/p/9259379.html

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

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

前言

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 的場景中得出不確定的查詢結果以及引起資料庫效能的下降。

介紹

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 一個函式.怎麽使用自己查吧…反正我會了

  • 範例

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

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

     1 (root@localhost mysql3306.sock)[zlm]>createtable test_null(
    2 -> idintnotnull,
    3 -> namevarchar(10)
    4 -> );
     5 Query OK, 0 rows affected (0.02 sec)
     6 
     7 (root@localhost mysql3306.sock)[zlm]>insertinto test_null values(1,'zlm');
     8 Query OK, 1 row affected (0.00 sec)
     9 
    10 (root@localhost mysql3306.sock)[zlm]>insertinto test_null values(2,null);
    11 Query OK, 1 row affected (0.00 sec)
    12 
    13 (root@localhost mysql3306.sock)[zlm]>select * from test_null;
    14 +----+------+
    15 | id | name |
    16 +----+------+
    17 | 1 | zlm |
    18 | 2 | NULL |
    19 +----+------+
    20 2 rows in set (0.00 sec)
    21 // -------------------------------------->這個很有代表性<----------------------
    22 (root@localhost mysql3306.sock)[zlm]>select * from test_null wherename=null;
    23 Empty set (0.00 sec)
    24
    25 (root@localhost mysql3306.sock)[zlm]>select * from test_null wherenameisnull;
    26 +----+------+
    27 | id | name |
    28 +----+------+
    29 | 2 | NULL |
    30 +----+------+
    31 1 row in set (0.00 sec)
    32
    33 (root@localhost mysql3306.sock)[zlm]>select * from test_null wherenameisnotnull;
    34 +----+------+
    35 | id | name |
    36 +----+------+
    37 | 1 | zlm |
    38 +----+------+
    39 1 row in set (0.00 sec)
    40
    41 (root@localhost mysql3306.sock)[zlm]>select * from test_null wherenull=null;
    42 Empty set (0.00 sec)
    43
    44 (root@localhost mysql3306.sock)[zlm]>select * from test_null wherenull<>null;
    45 Empty set (0.00 sec)
    46
    47 (root@localhost mysql3306.sock)[zlm]>select * from test_null wherenull<=>null;
    48 +----+------+
    49 | id | name |
    50 +----+------+
    51 | 1 | zlm |
    52 | 2 | NULL |
    53 +----+------+
    54 2 rows in set (0.00 sec)
    55 //null<=>nullalwaysreturntrue,it's equal to "where 1=1".

    Null means 「a missing and unknown value」.Let’s see details below.

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

     1 (root@localhost mysql3306.sock)[zlm]>SELECT0ISNULL0ISNOTNULL''ISNULL''ISNOTNULL;
     2 +-----------+---------------+------------+----------------+
     3 | 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |
     4 +-----------+---------------+------------+----------------+
     5 | 0 | 1 | 0 | 1 |
     6 +-----------+---------------+------------+----------------+
     7 1 row in set (0.00 sec)
    8
    9 //It's not equal to zero number or vacant string.
    10 //In MySQL,0 means fasle,1 means true.
    11 
    12 (root@localhost mysql3306.sock)[zlm]>SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
    13 +----------+-----------+----------+----------+
    14 | 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
    15 +----------+-----------+----------+----------+
    16 | NULL | NULL | NULL | NULL |
    17 +----------+-----------+----------+----------+
    18 1 row in set (0.00 sec)
    19 
    20 //It cannot be compared with number.
    21 //In MySQL,null means false,too.

    It truns null as a result if any expression contains null value.

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

     1 (root@localhost mysql3306.sock)[zlm]>selectifnull(null,'First is null'),ifnull(null+10,'First is null'),ifnull(concat('abc',null),'First is null');
     2 +------------------------------+---------------------------------+--------------------------------------------+
     3 | ifnull(null,'First is null') | ifnull(null+10,'First is null') | ifnull(concat('abc',null),'First is null') |
     4 +------------------------------+---------------------------------+--------------------------------------------+
     5 | First is null | First is null | First is null |
     6 +------------------------------+---------------------------------+--------------------------------------------+
     7 1 row in set (0.00 sec)
    8
    9 //nullvalue needs to be disposed withifnull() function,what usually causes sqlstatement more complex.
    10 //As we all know,MySQL does not support funcion index.Therefore,indexeson 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(*) .

     1 (root@localhost mysql3306.sock)[zlm]>selectcount(*),count(namefrom test_null;
     2 +----------+-------------+
     3 | count(*) | count(name) |
     4 +----------+-------------+
     5 | 2 | 1 |
     6 +----------+-------------+
     7 1 row in set (0.00 sec)
    8
    9 //count(*) returnsallrowsignore the nullwhilecount(namereturns the non-nullrowsincolumn"name".
    10 // This will also leads to uncertainty if someone is unaware of the details above.
     如果使用者對NULL內容不熟悉,很容易統計出錯誤的結果.

    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 又被認為是相同 .

     1 (root@localhost mysql3306.sock)[zlm]>insertinto test_null values(3,null);
     2 Query OK, 1 row affected (0.00 sec)
     3 
     4 (root@localhost mysql3306.sock)[zlm]>selectdistinctnamefrom test_null;
     5 +------+
     6 | name |
     7 +------+
     8 | zlm |
     9 | NULL |
    10 +------+
    11 2 rows in set (0.00 sec)
    12
    13 //Two rowsofnullvalue returned one and the result became two.
    14
    15 (root@localhost mysql3306.sock)[zlm]>selectnamefrom test_null groupbyname;
    16 +------+
    17 | name |
    18 +------+
    19 | NULL |
    20 | zlm |
    21 +------+
    22 2 rows in set (0.00 sec)
    23
    24 //Two rowsofnullvalue were put into the same group.
    25 //Bydefault,groupby will also sort the result(nullrow showed first).
    26
    27 (root@localhost mysql3306.sock)[zlm]>selectid,namefrom test_null orderbyname;
    28 +----+------+
    29 | id | name |
    30 +----+------+
    31 | 2 | NULL |
    32 | 3 | NULL |
    33 | 1 | zlm |
    34 +----+------+
    35 3 rows in set (0.00 sec)
    36
    37 //Three rows were sorted(two nullrows showed first). 

    MySQL supports to use index on column which contains null value(what’s different from oracle).

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

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

     1 (root@localhost mysql3306.sock)[sysbench]>showtables;
     2 +--------------------+
     3 | Tables_in_sysbench |
     4 +--------------------+
     5 | sbtest1 |
     6 | sbtest10 |
     7 | sbtest2 |
     8 | sbtest3 |
     9 | sbtest4 |
    10 | sbtest5 |
    11 | sbtest6 |
    12 | sbtest7 |
    13 | sbtest8 |
    14 | sbtest9 |
    15 +--------------------+
    16 10 rows in set (0.00 sec)
    17
    18 (root@localhost mysql3306.sock)[sysbench]>showcreatetable sbtest1\G
    19 *************************** 1.row ***************************
    20Table: sbtest1
    21CreateTableCREATETABLE`sbtest1` (
    22`id`int(11NOTNULL AUTO_INCREMENT,
    23`k`int(11NOTNULLDEFAULT'0',
    24`c`char(120NOTNULLDEFAULT'',
    25`pad`char(60NOTNULLDEFAULT'',
    26 PRIMARY KEY (`id`),
    27KEY`k_1` (`k`)
    28 ) ENGINE=InnoDB AUTO_INCREMENT=100001DEFAULTCHARSET=utf8
    291rowinset (0.00 sec)
    30
    31 (root@localhost mysql3306.sock)[sysbench]>altertable sbtest1 modify k intnull,modify c char(120null,modifypadchar(60null;
    32 Query OK, 0 rows affected (4.14 sec)
    33 Records: 0 Duplicates: 0 Warnings: 0
    34 
    35 (root@localhost mysql3306.sock)[sysbench]>insertinto sbtest1 values(100001,null,null,null);
    36 Query OK, 1 row affected (0.00 sec)
    37 
    38 (root@localhost mysql3306.sock)[sysbench]>explainselectid,k from sbtest1 whereid=100001;
    39 +----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
    40 | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
    41 +----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
    42 | 1 | SIMPLE | sbtest1 | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
    43 +----+-------------+---------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
    44 1 row in set1warning (0.00 sec)
    45
    46 (root@localhost mysql3306.sock)[sysbench]>explainselectid,k from sbtest1 where k isnull;
    47 +----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
    48 | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
    49 +----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
    50 | 1 | SIMPLE | sbtest1 | NULL | ref | k_1 | k_1 | 5 | const | 1 | 100.00 | Using where; Using index |
    51 +----+-------------+---------+------------+------+---------------+------+---------+-------+------+----------+--------------------------+
    52 1 row in set1warning (0.00 sec)
    53
    54 //In the firstquery,the newly added rowis retrieved(檢索) by primary key.
    55 //In the secondquery,the newly added rowis retrieved by secondary key"k_1"
    56 // It has been proved that indexes can be used on the columns which contain null value.
    透過explain 可以看到 mysql支持含有NULL值的列上使用索引 
    57 //column"k"isint datatype which occupies 4bytes,but the valueof"key_len" turn outto 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> explainselect * from test_1 where code isNULL;
    +----+-------------+--------+------------+------+---------------+------------+---------+-------+------+----------+-----------------------+
    | 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 set1warning (0.00 sec)
    mysql> explainselect * from test_1 where code isnotNULL;
    +----+-------------+--------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
    | 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 set1warning (0.00 sec)
    mysql> explainselect * 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 set1warning (0.00 sec)
    mysql> explainselect * 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 set1warning (0.00 sec)


    總結

    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

    >>

    END

    精品資料,超贊福利,免費領

    微信掃碼/長按辨識 添加【技術交流群

    群內每天分享精品學習資料

    最近開發整理了一個用於速刷面試題的小程式;其中收錄了上千道常見面試題及答案(包含基礎並行JVMMySQLRedisSpringSpringMVCSpringBootSpringCloud訊息佇列等多個型別),歡迎您的使用。

    👇👇

    👇點選"閱讀原文",獲取更多資料(持續更新中