电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

MySQL為何不建議使用默認值為null列

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

通常能聽到的答案是使用了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 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值是一種對列的特殊約束,我們創建一個新列時,如果沒有明確的使用關鍵字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,除了<=>.

(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代表一個不確定的值,就算是兩個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.任何有返回值的表達式中有NULL參與時,都會得到另外一個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)結果不同,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.

如果使用者對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又被認為是相同值.

(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不支持.這就是我們平時所說的如果列上含有NULL那么將會使索引失效.嚴格來說,這句話對與MySQL來說是不準確的.

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

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

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.

參考資料

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

到此這篇關于MySQL為何不建議使用默認值為null列的文章就介紹到這了,更多相關MySQL默認值為null內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: MySQL 數據庫
相關文章:
主站蜘蛛池模板: 政府园区专业委托招商平台_助力企业选址项目快速落地_东方龙商务集团 | 五轴加工中心_数控加工中心_铝型材加工中心-罗威斯 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 课件导航网_ppt课件_课件模板_课件下载_最新课件资源分享发布平台 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 隐形纱窗|防护纱窗|金刚网防盗纱窗|韦柏纱窗|上海青木装潢制品有限公司|纱窗国标起草单位 | 油冷式_微型_TDY电动滚筒_外装_外置式电动滚筒厂家-淄博秉泓机械有限公司 | 石英砂矿石色选机_履带辣椒色选机_X光异物检测机-合肥幼狮光电科技 | 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 365文案网_全网创意文案句子素材站 | 中空玻璃生产线,玻璃加工设备,全自动封胶线,铝条折弯机,双组份打胶机,丁基胶/卧式/立式全自动涂布机,玻璃设备-山东昌盛数控设备有限公司 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | 太空舱_民宿太空舱厂家_移动房屋太空舱价格-豪品建筑 | 河北凯普威医疗器材有限公司,高档轮椅系列,推车系列,座厕椅系列,协步椅系列,拐扙系列,卫浴系列 | 精密机械零件加工_CNC加工_精密加工_数控车床加工_精密机械加工_机械零部件加工厂 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 全自动过滤器_反冲洗过滤器_自清洗过滤器_量子除垢环_量子环除垢_量子除垢 - 安士睿(北京)过滤设备有限公司 | 济南铝方通-济南铝方通价格-济南方通厂家-山东鲁方通建材有限公司 | 桁架机器人_桁架机械手_上下料机械手_数控车床机械手-苏州清智科技装备制造有限公司 | 大_小鼠elisa试剂盒-植物_人Elisa试剂盒-PCR荧光定量试剂盒-上海一研生物科技有限公司 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | OpenI 启智 新一代人工智能开源开放平台 | 动物麻醉机-数显脑立体定位仪-北京易则佳科技有限公司 | 成都租车_成都租车公司_成都租车网_众行宝 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 粉末冶金-粉末冶金齿轮-粉末冶金零件厂家-东莞市正朗精密金属零件有限公司 | 幂简集成 - 品种超全的API接口平台, 一站搜索、试用、集成国内外API接口 | 陕西安玻璃自动感应门-自动重叠门-磁悬浮平开门厂家【捷申达门业】 | 丽陂特官网_手机信号屏蔽器_Wifi信号干扰器厂家_学校考场工厂会议室屏蔽仪 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 柴油发电机组_柴油发电机_发电机组价格-江苏凯晨电力设备有限公司 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 带式压滤机_污泥压滤机_污泥脱水机_带式过滤机_带式压滤机厂家-河南恒磊环保设备有限公司 | 不锈钢螺丝 - 六角螺丝厂家 - 不锈钢紧固件 - 万千紧固件--紧固件一站式采购 | 杭州火蝠电商_京东代运营_拼多多全托管代运营【天猫代运营】 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 |