<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          面試高頻題,程序員必看!MySQL 為何不推薦默認(rèn)值為 null ?

          共 15903字,需瀏覽 32分鐘

           ·

          2020-12-27 22:26


          點(diǎn)擊上方?泥瓦匠 關(guān)注我!

          老家浙江東海邊,靠海吃海,目前經(jīng)營(yíng)一個(gè)小品牌,讓普通人吃到最新鮮的海鮮。有興趣可以點(diǎn)擊了解:《浙里有漁,鮮人一步!》???


          通常能聽(tīng)到的答案是使用了NULL值的列將會(huì)使索引失效,但是如果實(shí)際測(cè)試過(guò)一下,你就知道IS NULL會(huì)使用索引,所以上述說(shuō)法有漏洞。

          著急的人拉到最下邊看結(jié)論

          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值是一種對(duì)列的特殊約束,我們創(chuàng)建一個(gè)新列時(shí),如果沒(méi)有明確的使用關(guān)鍵字not null聲明該數(shù)據(jù)列,MySQL會(huì)默認(rèn)的為我們添加上NULL約束。

          有些開(kāi)發(fā)人員在創(chuàng)建數(shù)據(jù)表時(shí),由于懶惰直接使用Mysql的默認(rèn)推薦設(shè)置.(即允許字段使用NULL值).而這一陋習(xí)很容易在使用NULL的場(chǎng)景中得出不確定的查詢(xún)結(jié)果以及引起數(shù)據(jù)庫(kù)性能的下降。

          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并不意味著什么都沒(méi)有,我們要注意?NULL?跟?''(空值)是兩個(gè)完全不一樣的值,MySQL中可以操作NULL值操作符主要有三個(gè)。

          • IS NULL
          • IS NOT NULL
          • <=>?太空船操作符,這個(gè)操作符很像=,select NULL<=>NULL可以返回true,但是select NULL=NULL返回false.
          • IFNULL?一個(gè)函數(shù).怎么使用自己查吧…反正我會(huì)了

          Example

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

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

          ?1?(root@localhost?mysql3306.sock)[zlm]>create?table?test_null(
          ?2?????->?id?int?not?null,
          ?3?????->?name?varchar(10)
          ?4?????->?);
          ?5?Query?OK,?0?rows?affected?(0.02?sec)
          ?6?
          ?7?(root@localhost?mysql3306.sock)[zlm]>insert?into?test_null?values(1,'zlm');
          ?8?Query?OK,?1?row?affected?(0.00?sec)
          ?9?
          10?(root@localhost?mysql3306.sock)[zlm]>insert?into?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?//?-------------------------------------->這個(gè)很有代表性<----------------------
          22?(root@localhost?mysql3306.sock)[zlm]>select?*?from?test_null?where?name=null;
          23?Empty?set?(0.00?sec)
          24?
          25?(root@localhost?mysql3306.sock)[zlm]>select?*?from?test_null?where?name?is?null;
          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?where?name?is?not?null;
          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?where?null=null;
          42?Empty?set?(0.00?sec)
          43?
          44?(root@localhost?mysql3306.sock)[zlm]>select?*?from?test_null?where?null<>null;
          45?Empty?set?(0.00?sec)
          46?
          47?(root@localhost?mysql3306.sock)[zlm]>select?*?from?test_null?where?null<=>null;
          48?+----+------+
          49?|?id?|?name?|
          50?+----+------+
          51?|??1?|?zlm??|
          52?|??2?|?NULL?|
          53?+----+------+
          54?2?rows?in?set?(0.00?sec)
          55??//null<=>null?always?return?true,it's?equal?to?"where?1=1".??
          12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455

          Null means “a missing and unknown value”.Let’s see details below.

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

          ?1?(root@localhost?mysql3306.sock)[zlm]>SELECT?0?IS?NULL,?0?IS?NOT?NULL,?''?IS?NULL,?''?IS?NOT?NULL;
          ?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;
          13?+----------+-----------+----------+----------+
          14?|?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.
          123456789101112131415161718192021

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

          任何有返回值的表達(dá)式中有NULL參與時(shí),都會(huì)得到另外一個(gè)NULL值.

          ?1?(root@localhost?mysql3306.sock)[zlm]>select?ifnull(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???//null?value?needs?to?be?disposed?with?ifnull()?function,what?usually?causes?sql?statement?more?complex.
          ?10??//As?we?all?know,MySQL?does?not?support?funcion?index.Therefore,indexes?on?the?column?may?not?be?used.That's?really?worse.
          12345678910

          It’s diffrent when using count() & count(null column).

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

          ?1?(root@localhost?mysql3306.sock)[zlm]>select?count(*),count(name)?from?test_null;
          ?2?+----------+-------------+
          ?3?|?count(*)?|?count(name)?|
          ?4?+----------+-------------+
          ?5?|????????2?|???????????1?|
          ?6?+----------+-------------+
          ?7?1?row?in?set?(0.00?sec)
          ?8?
          ?9?//count(*)?returns?all?rows?ignore?the?null?while?count(name)?returns?the?non-null?rows?in?column?"name".
          10?//?This?will?also?leads?to?uncertainty?if?someone?is?unaware?of?the?details?above.
          ?如果使用者對(duì)NULL屬性不熟悉,很容易統(tǒng)計(jì)出錯(cuò)誤的結(jié)果.
          1234567891011

          When using distinct,group by,order by,all null values are considered as the same value.

          雖然select NULL=NULL的結(jié)果為false,但是在我們使用distinct,group by,order by時(shí),NULL又被認(rèn)為是相同.

          ?1?(root@localhost?mysql3306.sock)[zlm]>insert?into?test_null?values(3,null);
          ?2?Query?OK,?1?row?affected?(0.00?sec)
          ?3?
          ?4?(root@localhost?mysql3306.sock)[zlm]>select?distinct?name?from?test_null;
          ?5?+------+
          ?6?|?name?|
          ?7?+------+
          ?8?|?zlm??|
          ?9?|?NULL?|
          10?+------+
          11?2?rows?in?set?(0.00?sec)
          12?
          13?//Two?rows?of?null?value?returned?one?and?the?result?became?two.
          14?
          15?(root@localhost?mysql3306.sock)[zlm]>select?name?from?test_null?group?by?name;
          16?+------+
          17?|?name?|
          18?+------+
          19?|?NULL?|
          20?|?zlm??|
          21?+------+
          22?2?rows?in?set?(0.00?sec)
          23?
          24?//Two?rows?of?null?value?were?put?into?the?same?group.
          25?//By?default,group?by?will?also?sort?the?result(null?row?showed?first).
          26?
          27?(root@localhost?mysql3306.sock)[zlm]>select?id,name?from?test_null?order?by?name;
          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?null?rows?showed?first).?
          12345678910111213141516171819202122232425262728293031323334353637

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

          MySQL中支持在含有NULL值的列上使用索引,但是Oracle不支持.這就是我們平時(shí)所說(shuō)的如果列上含有NULL那么將會(huì)使索引失效。

          嚴(yán)格來(lái)說(shuō),這句話(huà)對(duì)與MySQL來(lái)說(shuō)是不準(zhǔn)確的。這個(gè)題我在 MySQL 系列面試題中也分享過(guò)答案,關(guān)注公眾號(hào)Java技術(shù)棧回復(fù)面試,可以獲取更多 MySQL 面試題。

          ?1?(root@localhost?mysql3306.sock)[sysbench]>show?tables;
          ?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]>show?create?table?sbtest1\G
          19?***************************?1.?row?***************************
          20????????Table:?sbtest1
          21?Create?Table:?CREATE?TABLE?`sbtest1`?(
          22???`id`?int(11)?NOT?NULL?AUTO_INCREMENT,
          23???`k`?int(11)?NOT?NULL?DEFAULT?'0',
          24???`c`?char(120)?NOT?NULL?DEFAULT?'',
          25???`pad`?char(60)?NOT?NULL?DEFAULT?'',
          26???PRIMARY?KEY?(`id`),
          27???KEY?`k_1`?(`k`)
          28?)?ENGINE=InnoDB?AUTO_INCREMENT=100001?DEFAULT?CHARSET=utf8
          29?1?row?in?set?(0.00?sec)
          30?
          31?(root@localhost?mysql3306.sock)[sysbench]>alter?table?sbtest1?modify?k?int?null,modify?c?char(120)?null,modify?pad?char(60)?null;
          32?Query?OK,?0?rows?affected?(4.14?sec)
          33?Records:?0??Duplicates:?0??Warnings:?0
          34?
          35?(root@localhost?mysql3306.sock)[sysbench]>insert?into?sbtest1?values(100001,null,null,null);
          36?Query?OK,?1?row?affected?(0.00?sec)
          37?
          38?(root@localhost?mysql3306.sock)[sysbench]>explain?select?id,k?from?sbtest1?where?id=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?set,?1?warning?(0.00?sec)
          45?
          46?(root@localhost?mysql3306.sock)[sysbench]>explain?select?id,k?from?sbtest1?where?k?is?null;
          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?set,?1?warning?(0.00?sec)
          53?
          54?//In?the?first?query,the?newly?added?row?is?retrieved(檢索)?by?primary?key.
          55?//In?the?second?query,the?newly?added?row?is?retrieved?by?secondary?key?"k_1"
          56?//?It?has?been?proved?that?indexes?can?be?used?on?the?columns?which?contain?null?value.
          ???通過(guò)explain?可以看到?mysql支持含有NULL值的列上使用索引?
          57?//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.
          1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859

          這個(gè)是我自己測(cè)試的例子:

          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)
          12345678910111213141516171819202122232425262728293031323334353637383940414243444546

          Summary 總結(jié)

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

          列中使用NULL值容易引發(fā)不受控制的事情發(fā)生,有時(shí)候還會(huì)嚴(yán)重托慢系統(tǒng)的性能.

          例如:

          • null value will not be estimated in aggregate function() which may cause inaccurate results. 對(duì)含有NULL值的列進(jìn)行統(tǒng)計(jì)計(jì)算,eg.?count(),max(),min(),結(jié)果并不符合我們的期望值.
          • null value will influence the behavior of the operations such as “distinct”,“group by”,“order by” which causes wrong sort. 干擾排序,分組,去重結(jié)果.
          • null value needs ifnull() function to do judgement which makes the program code more complex. 有的時(shí)候?yàn)榱讼?code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;font-size: 14px;border-radius: 4px;color: rgb(30, 107, 184);background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;">NULL帶來(lái)的技術(shù)債務(wù),我們需要在SQL中使用IFNULL()來(lái)確保結(jié)果可控,但是這使程序變得復(fù)雜.
          • null value needs a extra 1 byte to store the null information in the rows.NULL值并是占用原有的字段空間存儲(chǔ),而是額外申請(qǐng)一個(gè)字節(jié)去標(biāo)注,這個(gè)字段添加了NULL約束.(就像額外的標(biāo)志位一樣)

          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.

          根據(jù)以上缺點(diǎn),我們并不推薦在列中設(shè)置NULL作為列的默認(rèn)值,你可以使用NOT NULL消除默認(rèn)設(shè)置,使用0或者''空字符串來(lái)代替NULL。這個(gè)題我在 MySQL 系列面試題中也分享過(guò)答案,關(guān)注公眾號(hào)并回復(fù)1024,獲取更多技術(shù)資料!

          往期推薦

          這滿(mǎn)屏的 if/ else,交接的兄弟快被逼瘋!

          深入源碼分析,緩存之王 Caffeine 為何這么猛?

          如何手動(dòng)獲取 Spring 容器中的 Bean?

          開(kāi)發(fā)中,那些常用的 MySQL 優(yōu)化

          老家浙江東海邊,靠海吃海,目前經(jīng)營(yíng)一個(gè)小品牌,讓普通人吃到最新鮮的海鮮。有興趣可以點(diǎn)擊了解:《浙里有漁,鮮人一步!》???

          點(diǎn)擊領(lǐng)?。撼绦騿T最新學(xué)習(xí)資料!

          下方二維碼關(guān)注我

          技術(shù)草根,堅(jiān)持分享?編程,算法,架構(gòu)

          朋友助力下!點(diǎn)個(gè)在看!
          瀏覽 44
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  无码免费视频在线观看 | 日韩激情网 | 中文在线а√在线8 | 欧美做爱视频免费播放 | 蜜桃在线视频人妻 |