MySQL為Null會導致5個問題,個個致命!
阿新 • • 發佈:2020-12-31
在正式開始之前,我們先來看下 MySQL 伺服器的配置和版本號資訊,如下圖所示:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609331580357-f0108445-c85c-449f-9950-4f2b6c075d1e.png#align=left&display=inline&height=279&margin=%5Bobject%20Object%5D&name=image.png&originHeight=558&originWidth=1096&size=70337&status=done&style=none&width=548)
“兵馬未動糧草先行”,看完了相關的配置之後,我們先來建立一張測試表和一些測試資料。
```sql
-- 如果存在 person 表先刪除
DROP TABLE IF EXISTS person;
-- 建立 person 表,其中 username 欄位可為空,併為其設定普通索引
CREATE TABLE person (
id INT PRIMARY KEY auto_increment,
name VARCHAR(20),
mobile VARCHAR(13),
index(name)
) ENGINE='innodb';
-- person 表新增測試資料
insert into person(name,mobile) values('Java','13333333330'),
('MySQL','13333333331'),
('Redis','13333333332'),
('Kafka','13333333333'),
('Spring','13333333334'),
('MyBatis','13333333335'),
('RabbitMQ','13333333336'),
('Golang','13333333337'),
(NULL,'13333333338'),
(NULL,'13333333339');
select * from person;
```
構建的測試資料,如下圖所示:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609334360387-b21af2f9-ed74-413d-bbea-6c0aa0b00869.png#align=left&display=inline&height=333&margin=%5Bobject%20Object%5D&name=image.png&originHeight=666&originWidth=530&size=55894&status=done&style=none&width=265)
有了資料之後,我們就來看當列中存在 `NULL` 值時,究竟會導致哪些問題?
# 1.count 資料丟失
當某列存在 `NULL` 值時,再使用 `count` 查詢該列,就會出現資料“丟失”問題,如下 SQL 所示:
```sql
select count(*),count(name) from person;
```
查詢執行結果如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609334410752-bc9bd6ca-9aee-4d1c-a930-c0d098751bf7.png#align=left&display=inline&height=56&margin=%5Bobject%20Object%5D&name=image.png&originHeight=112&originWidth=326&size=5890&status=done&style=none&width=163)
從上述結果可以看出,當使用的是 `count(name)` 查詢時,就丟失了兩條值為 `NULL` 的資料丟失。
#### 解決方案
如果某列存在 `NULL` 值時,就是用 `count(*)` 進行資料統計。
#### 擴充套件知識:不要使用 count(常量)
> 阿里巴巴《Java開發手冊》強制規定:不要使用 count(列名) 或 count(常量) 來替代 count(*),count(*) 是 SQL92 定義的標準統計行數的語法,跟資料庫無關,跟 NULL 和非 NULL 無關。
>
> 說明:count(*) 會統計值為 NULL 的行,而 count(列名) 不會統計此列為 NULL 值的行。
# 2.distinct 資料丟失
當使用 `count(distinct col1, col2)` 查詢時,如果其中一列為 `NULL`,那麼即使另一列有不同的值,那麼查詢的結果也會將資料丟失,如下 SQL 所示:
```sql
select count(distinct name,mobile) from person;
```
查詢執行結果如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609334455638-43ff8d29-4963-44bd-b331-174b93832f1e.png#align=left&display=inline&height=54&margin=%5Bobject%20Object%5D&name=image.png&originHeight=108&originWidth=336&size=5988&status=done&style=none&width=168)
資料庫的原始資料如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609334659475-5a4e8691-d083-4244-96fb-8900fbf3d1dc.png#align=left&display=inline&height=335&margin=%5Bobject%20Object%5D&name=image.png&originHeight=670&originWidth=538&size=58717&status=done&style=none&width=269)
從上述結果可以看出手機號一列的 10 條資料都是不同的,但查詢的結果卻為 8。
# 3.select 資料丟失
如果某列存在 `NULL` 值時,如果執行非等於查詢(<>/!=)會導致為 `NULL` 值的結果丟失。
比如以下這個資料:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609336414923-78962e7f-e789-4fa1-9445-077a597e7327.png#align=left&display=inline&height=339&margin=%5Bobject%20Object%5D&name=image.png&originHeight=678&originWidth=580&size=57554&status=done&style=none&width=290)
我需要查詢除 name 等於“Java”以外的所有資料,預期返回的結果是 id 從 2 到 10 的資料,但當執行以下查詢時:
```sql
select * from person where name<>'Java' order by id;
-- 或
select * from person where name!='Java' order by id;
```
查詢結果均為以下內容:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609336573893-50feecb0-e944-46fe-b4e5-ab4ce7845dec.png#align=left&display=inline&height=269&margin=%5Bobject%20Object%5D&name=image.png&originHeight=538&originWidth=536&size=41888&status=done&style=none&width=268)
可以看出為 `NULL` 的兩條資料憑空消失了,這個結果並不符合我們的正常預期。
### 解決方案
要解決以上的問題,只需要在查詢結果中拼加上為 `NULL` 值的結果即可,執行 SQL 如下:
```sql
select * from person where name<>'Java' or isnull(name) order by id;
```
最終的執行結果如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609336783906-9c4663ef-af27-4af0-b96d-cf4b18ebfb80.png#align=left&display=inline&height=307&margin=%5Bobject%20Object%5D&name=image.png&originHeight=614&originWidth=552&size=52225&status=done&style=none&width=276)
# 4.導致空指標異常
如果某列存在 `NULL` 值時,可能會導致 `sum(column)` 的返回結果為 `NULL` 而非 0,如果 `sum` 查詢的結果為 `NULL` 就可以能會導致程式執行時空指標異常(NPE),我們來演示一下這個問題。
首先,我們先構建一張表和一些測試資料:
```sql
-- 如果存在 goods 表先刪除
DROP TABLE IF EXISTS goods;
-- 建立 goods 表
CREATE TABLE goods (
id INT PRIMARY KEY auto_increment,
num int
) ENGINE='innodb';
-- goods 表新增測試資料
insert into goods(num) values(3),(6),(6),(NULL);
select * from goods;
```
表中原始資料如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609335198639-dbce071a-81e6-4dab-97e7-591e40e2e7e2.png#align=left&display=inline&height=146&margin=%5Bobject%20Object%5D&name=image.png&originHeight=292&originWidth=318&size=11106&status=done&style=none&width=159)
接下來我們使用 `sum` 查詢,執行以下 SQL:
```sql
select sum(num) from goods where id>4;
```
查詢執行結果如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609335521443-859a5429-bd87-46cc-a054-f8acf2fa2f01.png#align=left&display=inline&height=51&margin=%5Bobject%20Object%5D&name=image.png&originHeight=102&originWidth=192&size=4774&status=done&style=none&width=96)
當查詢的結果為 `NULL` 而非 0 時,就可以能導致空指標異常。
### 解決空指標異常
可以使用以下方式來避免空指標異常:
```sql
select ifnull(sum(num), 0) from goods where id>4;
```
查詢執行結果如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609335659208-1181702b-66d9-474a-8e92-8da30c8e36bc.png#align=left&display=inline&height=59&margin=%5Bobject%20Object%5D&name=image.png&originHeight=118&originWidth=286&size=4975&status=done&style=none&width=143)
# 5.增加了查詢難度
當某列值中有 `NULL` 值時,在進行 `NULL` 值或者非 `NULL` 值的查詢難度就增加了。
所謂的查詢難度增加指的是當進行 `NULL` 值查詢時,必須使用 `NULL` 值匹配的查詢方法,比如 `IS NULL` 或者 `IS NOT NULL` 又或者是 `IFNULL(cloumn)` 這樣的表示式進行查詢,而傳統的 `=、!=、<>...` 等這些表示式就不能使用了,這就增加了查詢的難度,尤其是對小白程式設計師來說,接下來我們來演示一下這些問題。
還是以 `person` 表為例,它的原始資料如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609337488772-bdd4465b-0b13-4c9e-b39c-d4e8aed9630a.png#align=left&display=inline&height=340&margin=%5Bobject%20Object%5D&name=image.png&originHeight=680&originWidth=598&size=57387&status=done&style=none&width=299)
#### 錯誤用法 1:
```sql
select * from person where name<>null;
```
執行結果為空,並沒有查詢到任何資料,如下圖所示:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609337528911-4349d7e3-987b-4a64-a9c0-f53a4991b127.png#align=left&display=inline&height=75&margin=%5Bobject%20Object%5D&name=image.png&originHeight=150&originWidth=592&size=5715&status=done&style=none&width=296)
#### 錯誤用法 2:
```sql
select * from person where name!=null;
```
執行結果也為空,沒有查詢到任何資料,如下圖所示:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609337528911-4349d7e3-987b-4a64-a9c0-f53a4991b127.png#align=left&display=inline&height=75&margin=%5Bobject%20Object%5D&name=image.png&originHeight=150&originWidth=592&size=5715&status=done&style=none&width=296)
#### 正確用法 1:
```sql
select * from person where name is not null;
```
執行結果如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609337620037-806f72a0-e7d5-44ea-9a86-5701bef85ec1.png#align=left&display=inline&height=275&margin=%5Bobject%20Object%5D&name=image.png&originHeight=550&originWidth=616&size=46381&status=done&style=none&width=308)
#### 正確用法 2:
```sql
select * from person where !isnull(name);
```
執行結果如下:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609337620037-806f72a0-e7d5-44ea-9a86-5701bef85ec1.png#align=left&display=inline&height=275&margin=%5Bobject%20Object%5D&name=image.png&originHeight=550&originWidth=616&size=46381&status=done&style=none&width=308)
#### 推薦用法
**阿里巴巴《Java開發手冊》推薦我們使用 `ISNULL(cloumn)` 來判斷 `NULL` 值**,原因是在 SQL 語句中,如果在 null 前換行,影響可讀性;而 `ISNULL(column)` 是一個整體,簡潔易懂。從效能資料上分析 `ISNULL(column)` 執行效率也更快一些。
# 擴充套件知識:NULL 不會影響索引
細心的朋友可能發現了,我在建立 `person` 表的 `name` 欄位時,為其建立了一個普通索引,如下圖所示:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609337990811-051a80ec-8cfd-495c-b3a0-af2f66c4ddc4.png#align=left&display=inline&height=191&margin=%5Bobject%20Object%5D&name=image.png&originHeight=382&originWidth=1256&size=63266&status=done&style=none&width=628)
然後我們用 `explain` 來分析查詢計劃,看當 `name` 中有 `NULL` 值時是否會影響索引的選擇。
`explain` 的執行結果如下圖所示:
![image.png](https://cdn.nlark.com/yuque/0/2020/png/92791/1609338106540-8fd804cf-0cef-4d42-b122-e5d6d12e93d5.png#align=left&display=inline&height=180&margin=%5Bobject%20Object%5D&name=image.png&originHeight=360&originWidth=1960&size=64177&status=done&style=none&width=980)
從上述結果可以看出,即使 `name` 中有 `NULL` 值也不會影響 MySQL 使用索引進行查詢。
# 總結
本文我們講了當某列為 `NULL` 時可能會導致的 5 種問題:丟失查詢結果、導致空指標異常和增加了查詢的難度。因此在最後提倡大家在建立表的時候儘量設定 `is not null` 的約束,如果某列確實沒有值,可以設定空值('')或 0 作為其預設值。
> 最後:大家還有因為 NULL 而造成的各種坑嗎?歡迎評論區補充留言。
##### 參考 & 鳴謝
阿里巴巴《Java開發手冊》
> 關注公眾號「Java中文社群」發現更多幹貨。檢視 Github 發現更多精彩:https://github.com/vipstone/a