日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL_列值为null对索引的影响

發布時間:2024/1/23 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL_列值为null对索引的影响 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一.首先看一個我在某公眾號看到的一個關于數據庫優化的舉措

二.如果where子句中查詢的列執行了 “is null” 或者 “is not null” 或者 “<=> null” 會不會使用索引呢?

先列出結論:where子句中使用上述對null的判斷,如果判斷的列設置了索引,那就可以使用到索引

三.測試:

1.建表

1 CREATE TABLE`test_null_index` (2 `id` int(11) DEFAULT NULL,3 `mark` varchar(20) DEFAULT NULL,4 `name` varchar(11) DEFAULT NULL

5 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.插數據

create procedure test_null_index(in num int)BEGIN

DECLARE i int;set i=1;while (i

DOif mod(i,10)!=0 then

insert into test_null_index values (i,concat('aaa',i),null);else

insert into test_null_index values (null,concat('aaa',i),'bbb');end if;set i=i+1;END while;END;

call test_null_index(10000);

3.沒加索引時,type為All,全表掃描

mysql> explain SELECT * from test_null_index WHERE id is null;+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+

| 1 | SIMPLE | test_null_index | ALL | NULL | NULL | NULL | NULL | 10589 | Using where |

+----+-------------+-----------------+------+---------------+------+---------+------+-------+-------------+

1 row in set (0.00 sec)

4.加上索引,可以看到,索引起作用了

mysql> create index idx_test_null ontest_null_index(id);

Query OK,0 rows affected (0.12sec)

Records:0 Duplicates: 0 Warnings: 0mysql> explain SELECT * from test_null_index WHERE id is null;+----+-------------+-----------------+------+---------------+---------------+---------+-------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-----------------+------+---------------+---------------+---------+-------+------+-------------+

| 1 | SIMPLE | test_null_index | ref | idx_test_null | idx_test_null | 5 | const | 998 | Using where |

+----+-------------+-----------------+------+---------------+---------------+---------+-------+------+-------------+

1 row in set (0.00 sec)

四.注意,只能使用針對一個字段的關于null的判斷,如果同時在兩個字段對null進行判斷,還是會走全表掃描

1.測試,可以看到,在name字段加上索引,并查詢name為空的語句,同樣會走索引

mysql> create index idx_test_null2 ontest_null_index(name);

Query OK,0 rows affected (0.13sec)

Records:0 Duplicates: 0 Warnings: 0mysql> explain SELECT * from test_null_index WHERE name is null;+----+-------------+-----------------+------+----------------+----------------+---------+-------+------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-----------------+------+----------------+----------------+---------+-------+------+-------------+

| 1 | SIMPLE | test_null_index | ref | idx_test_null2 | idx_test_null2 | 36 | const | 8994 | Using where |

+----+-------------+-----------------+------+----------------+----------------+---------+-------+------+-------------+

1 row in set (0.00 sec)

2.同時針對id和name查詢為空的語句,走的是全表掃描

mysql> explain SELECT * from test_null_index WHERE id is null or name is null;+----+-------------+-----------------+------+------------------------------+------+---------+------+-------+-------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+-----------------+------+------------------------------+------+---------+------+-------+-------------+

| 1 | SIMPLE | test_null_index | ALL | idx_test_null,idx_test_null2 | NULL | NULL | NULL | 10589 | Using where |

+----+-------------+-----------------+------+------------------------------+------+---------+------+-------+-------------+

1 row in set (0.01 sec)

相關資源:MySQL中NULL對索引的影響深入講解_mysqlnull對索引的影響,null對...
————————————————
版權聲明:本文為CSDN博主「尋古詩詞網」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_36180068/article/details/114754393

總結

以上是生活随笔為你收集整理的MySQL_列值为null对索引的影响的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。