成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

MySQL為何不建議使用默認(rèn)值為null列

瀏覽:193日期:2023-10-17 19:27:06

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

著急的人拉到最下邊看結(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 explicitlywhen creating the table.Many programmers like to define columns by defaultbecause of the conveniences(reducing the judgement code of nullibility) what consequentlycause some uncertainty of query and poor performance of database.

NULL值是一種對(duì)列的特殊約束,我們創(chuàng)建一個(gè)新列時(shí),如果沒有明確的使用關(guān)鍵字not null聲明該數(shù)據(jù)列,Mysql會(huì)默認(rèn)的為我們添加上NULL約束.有些開發(fā)人員在創(chuàng)建數(shù)據(jù)表時(shí),由于懶惰直接使用Mysql的默認(rèn)推薦設(shè)置.(即允許字段使用NULL值).而這一陋習(xí)很容易在使用NULL的場景中得出不確定的查詢結(jié)果以及引起數(shù)據(jù)庫性能的下降.

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 跟 ’’(空值)是兩個(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通過任一操作符與其它值比較都會(huì)得到NULL,除了<=>.

(root@localhost mysql3306.sock)[zlm]>create table test_null( -> id int not null, -> name varchar(10) -> );Query OK, 0 rows affected (0.02 sec)(root@localhost mysql3306.sock)[zlm]>insert into test_null values(1,’zlm’);Query OK, 1 row affected (0.00 sec)(root@localhost mysql3306.sock)[zlm]>insert into test_null values(2,null);Query OK, 1 row affected (0.00 sec)(root@localhost mysql3306.sock)[zlm]>select * from test_null;+----+------+| id | name |+----+------+| 1 | zlm || 2 | NULL |+----+------+2 rows in set (0.00 sec)(root@localhost mysql3306.sock)[zlm]>select * from test_null where name=null;Empty set (0.00 sec)(root@localhost mysql3306.sock)[zlm]>select * from test_null where name is null;+----+------+| id | name |+----+------+| 2 | NULL |+----+------+1 row in set (0.00 sec)(root@localhost mysql3306.sock)[zlm]>select * from test_null where name is not null;+----+------+| id | name |+----+------+| 1 | zlm |+----+------+1 row in set (0.00 sec)(root@localhost mysql3306.sock)[zlm]>select * from test_null where null=null;Empty set (0.00 sec)(root@localhost mysql3306.sock)[zlm]>select * from test_null where null<>null;Empty set (0.00 sec)(root@localhost 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代表一個(gè)不確定的值,就算是兩個(gè)NULL,它倆也不一定相等.(像不像C中未初始化的局部變量)

(root@localhost 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.(root@localhost 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.任何有返回值的表達(dá)式中有NULL參與時(shí),都會(huì)得到另外一個(gè)NULL值.

(root@localhost 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)結(jié)果不同,count(null column)<=count(*).

(root@localhost 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.

如果使用者對(duì)NULL屬性不熟悉,很容易統(tǒng)計(jì)出錯(cuò)誤的結(jié)果.

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)為是相同值.

(root@localhost mysql3306.sock)[zlm]>insert into test_null values(3,null);Query OK, 1 row affected (0.00 sec)(root@localhost 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.(root@localhost 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).(root@localhost 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不支持.這就是我們平時(shí)所說的如果列上含有NULL那么將會(huì)使索引失效.嚴(yán)格來說,這句話對(duì)與MySQL來說是不準(zhǔn)確的.

(root@localhost 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)(root@localhost mysql3306.sock)[sysbench]>show create table sbtest1G*************************** 1. row *************************** Table: sbtest1Create 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=utf81 row in set (0.00 sec)(root@localhost 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(root@localhost mysql3306.sock)[sysbench]>insert into sbtest1 values(100001,null,null,null);Query OK, 1 row affected (0.00 sec)(root@localhost 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)(root@localhost 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.

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

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 總結(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)榱讼齆ULL帶來的技術(shù)債務(wù),我們需要在SQL中使用IFNULL()來確保結(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或者’’空字符串來代替NULL.

參考資料

https://www.cnblogs.com/aaron8219/p/9259379.html

到此這篇關(guān)于MySQL為何不建議使用默認(rèn)值為null列的文章就介紹到這了,更多相關(guān)MySQL默認(rèn)值為null內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: MySQL 數(shù)據(jù)庫
相關(guān)文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
国产欧美一区二区精品性色 | 久久日韩精品| 久久无码av三级| 国产成a人亚洲| 欧美日韩不卡视频| 奇米精品一区二区三区在线观看| 国产九九精品| 亚洲一区二区三区小说| 亚洲免费激情| 最新国产成人在线观看| 91丨porny丨最新| 精品国产乱码久久久久久闺蜜 | 精久久久久久久久久久| 久久久夜精品| 日韩精品福利网| 色老汉一区二区三区| 视频在线在亚洲| 久热精品视频| 日本欧美一区二区三区| 性刺激综合网| 亚洲国产色一区| 亚洲欧美电影在线观看| 亚洲成人激情自拍| 性xx色xx综合久久久xx| 亚洲成人精品一区二区| 久久精选视频| 久久国产综合精品| 91黄色激情网站| 日韩电影在线一区| 欧美午夜片在线观看| 另类小说图片综合网| 欧美精品v国产精品v日韩精品| 国产精品一区二区三区99| 91精品国产色综合久久不卡蜜臀 | 99re66热这里只有精品4| 中文字幕一区三区| 亚洲精品美女91| 亚洲综合在线电影| 国产精品一区二区三区四区五区 | 亚洲一级不卡视频| 久久一区视频| 日本aⅴ亚洲精品中文乱码| 欧美日韩一区视频| 成人性生交大片免费看中文| 久久精品人人爽人人爽| 伊人影院久久| 亚洲aaa精品| 在线看不卡av| 国产精品自拍一区| 精品少妇一区二区三区免费观看| 91亚洲午夜精品久久久久久| 国产性做久久久久久| 国精品一区二区| 亚洲一区自拍偷拍| 欧美三级在线看| www.亚洲色图.com| 国产精品视频看| 亚洲欧美日韩专区| 久久精品国产一区二区| 91精品欧美福利在线观看| a4yy欧美一区二区三区| 中文字幕佐山爱一区二区免费| 亚洲一区黄色| 激情文学综合网| 国产日韩影视精品| 免费中文字幕日韩欧美| 国产成人综合亚洲网站| 国产无人区一区二区三区| 亚洲色诱最新| 国产在线精品一区二区夜色| 久久久久久电影| 国产精品入口66mio| 国产乱码一区二区三区| 日本一区二区三级电影在线观看| 国产亚洲一区在线| 国产一区二区在线视频| 中文一区二区完整视频在线观看 | 精品国产第一区二区三区观看体验| 国产精品xnxxcom| 水野朝阳av一区二区三区| 日韩三级伦理片妻子的秘密按摩| 欧美网站在线| 免费的成人av| 国产三级精品三级| 久久性天堂网| 99久久免费国产| 亚洲观看高清完整版在线观看| 在线不卡一区二区| 在线国产欧美| 麻豆精品精品国产自在97香蕉| 久久精品夜色噜噜亚洲a∨| 亚洲一区二区动漫| 成人亚洲精品久久久久软件| 亚洲午夜精品网| 26uuu亚洲| 久久都是精品| 99re成人精品视频| 亚洲va欧美va天堂v国产综合| 精品嫩草影院久久| 久久久精品网| 91免费版在线| 日本免费新一区视频| 久久毛片高清国产| 色88888久久久久久影院野外| 欧美va天堂| 免费成人深夜小野草| 欧美高清在线视频| 欧美日韩视频在线观看一区二区三区| 欧美日韩三区| 国产毛片精品国产一区二区三区| 亚洲日本乱码在线观看| 日韩一区二区电影| 噜噜噜躁狠狠躁狠狠精品视频| 91亚洲精品一区二区乱码| 日本va欧美va精品发布| 中文字幕在线一区二区三区| 9191久久久久久久久久久| 亚洲茄子视频| 91丨porny丨中文| 国产一区二区三区四区在线观看| 亚洲综合在线免费观看| 久久精品亚洲国产奇米99| 欧美日韩国产乱码电影| 国产伦精品一区二区三区视频黑人| 91免费小视频| 韩日av一区二区| 亚洲成人免费观看| 中文字幕一区av| 久久尤物电影视频在线观看| 欧美色手机在线观看| 国产伦理一区| 一区福利视频| 99久久国产综合精品色伊| 麻豆成人av在线| 亚洲国产成人91porn| 国产精品进线69影院| 日韩一级视频免费观看在线| 久久综合久久综合这里只有精品| 亚洲激情网址| 97精品久久久午夜一区二区三区| 国产最新精品免费| 五月天亚洲婷婷| 亚洲精品久久嫩草网站秘色| 欧美高清在线精品一区| 精品美女在线观看| 91精品国产日韩91久久久久久| 老牛嫩草一区二区三区日本 | 亚洲激情中文1区| 国产精品美女久久久久久久久久久 | 美女主播一区| 伊人激情综合| 欧美三级午夜理伦三级中文幕| 不卡视频免费播放| 国产成a人亚洲精| 国产一区二区三区最好精华液| 久久福利视频一区二区| 石原莉奈一区二区三区在线观看 | 精品理论电影在线观看| 欧美一区二视频| 欧美午夜精品久久久久久孕妇| 久久看片网站| 亚洲永久视频| 午夜亚洲视频| 99国产精品视频免费观看一公开| 国产精品va| 欧美日韩在线不卡一区| 91小视频在线| 91麻豆精东视频| 99视频精品在线| av成人老司机| 91浏览器在线视频| 欧美va天堂在线| 国产一区二区三区自拍| 国内精品福利| 伊大人香蕉综合8在线视| 亚洲黑丝一区二区| 一区二区亚洲| 在线一区欧美| 中日韩男男gay无套| 在线亚洲一区| 久久精品卡一| 91国偷自产一区二区三区成为亚洲经典 | 久草在线在线精品观看| 美女网站视频久久| 免费一级欧美片在线观看| 日本不卡一区二区三区高清视频| 秋霞国产午夜精品免费视频| 麻豆91在线观看| 成人午夜电影久久影院| 成人精品视频.| 91在线国产福利| 国产精品v一区二区三区 | 欧美一区二区成人6969| 欧美大度的电影原声| 久久蜜臀中文字幕| 中文字幕中文字幕一区二区| 玉足女爽爽91| 免费观看在线色综合| 国产精品夜夜爽| 欧美一区二区三区久久精品茉莉花|