在MySQL中is null, is not null對索引的影響【測試】

本次測試使用的數(shù)據(jù)庫版本為5.7.26
image.png
準(zhǔn)備測試的兩張表數(shù)據(jù)如下:
 表a

CREATE TABLE `a_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;



INSERT INTO `a_user` (`id`, `name`, `age`, `sex`) VALUES ('1', 'tom', '20', '男');
INSERT INTO `a_user` (`id`, `name`, `age`, `sex`) VALUES ('2', 'bob', '40', '男');
INSERT INTO `a_user` (`id`, `name`, `age`, `sex`) VALUES ('3', 'lucy', '30', '女');


alter table a_user add index index_name(name(20));

 表b

CREATE TABLE `b_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;



INSERT INTO `b_user` (`id`, `name`, `age`, `sex`) VALUES ('1', 'tom', '20', '男');
INSERT INTO `b_user` (`id`, `name`, `age`, `sex`) VALUES ('2', 'bob', '40', '男');
INSERT INTO `b_user` (`id`, `name`, `age`, `sex`) VALUES ('3', 'lucy', '30', '女');


alter table b_user add index index_name(name(20));

一纤虽、索引字段不為null
使用select *查詢
select * from a_user where name is null;

image.png

select * from a_user where name is not null;

image.png

結(jié)果
select * from a_user where name is not null; 未使用索引商模;
select * from a_user where name is null; 未使用索引樊诺;

查詢索引字段
select name from a_user where name is null;

image.png

select name from a_user where name is not null;

image.png

結(jié)果
select name from a_user where name is null; 未使用索引;
select name from a_user where name is not null; 使用索引布朦;

多字段查詢(索引字段+非索引字段)
select name,sex from a_user where name is null;

image.png

select name,sex from a_user where name is not null;

image.png

結(jié)果
select name,sex from a_user where name is null;未使用索引煞烫;
select name,sex from a_user where name is not null;未使用索引;

二猴鲫、索引字段為null 時
使用select *查詢
select * from b_user where name is null;


image.png

select * from b_user where name is not null;


image.png

結(jié)果:
select * from b_user where name is null;使用索引
select * from b_user where name is not null;未使用索引

查詢索引字段
select name from b_user where name is null;


image.png

select name from b_user where name is not null;


image.png

結(jié)果:
select name from b_user where name is null;使用索引
select name from b_user where name is not null;使用索引

多字段查詢(索引字段+非索引字段)
select name,sex from b_user where name is null;


image.png

select name,sex from b_user where name is not null;


image.png

結(jié)果:
select name,sex from b_user where name is null;使用索引
select name,sex from b_user where name is not null;未使用索引

總結(jié)以上測試:
1对人、當(dāng)索引字段不為null 時,只有使用is not null 返回的結(jié)果集中只包含索引字段時,才使用索引拂共;
2牺弄、當(dāng)索引字段為null時候,使用 is null 不影響覆蓋索引宜狐,但是使用 is not null 只有完全返回索引字段時才會使用索引

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末势告,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子抚恒,更是在濱河造成了極大的恐慌咱台,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件俭驮,死亡現(xiàn)場離奇詭異回溺,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進(jìn)店門馅而,熙熙樓的掌柜王于貴愁眉苦臉地迎上來祥诽,“玉大人,你說我怎么就攤上這事瓮恭⌒燮海” “怎么了?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵屯蹦,是天一觀的道長维哈。 經(jīng)常有香客問我,道長登澜,這世上最難降的妖魔是什么阔挠? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮脑蠕,結(jié)果婚禮上购撼,老公的妹妹穿的比我還像新娘。我一直安慰自己谴仙,他們只是感情好迂求,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著晃跺,像睡著了一般揩局。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掀虎,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天凌盯,我揣著相機(jī)與錄音,去河邊找鬼烹玉。 笑死驰怎,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的春霍。 我是一名探鬼主播砸西,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼址儒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起衅疙,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤莲趣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后饱溢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體喧伞,經(jīng)...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了潘鲫。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片翁逞。...
    茶點(diǎn)故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖溉仑,靈堂內(nèi)的尸體忽然破棺而出挖函,到底是詐尸還是另有隱情,我是刑警寧澤浊竟,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布怨喘,位于F島的核電站,受9級特大地震影響振定,放射性物質(zhì)發(fā)生泄漏必怜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一后频、第九天 我趴在偏房一處隱蔽的房頂上張望梳庆。 院中可真熱鬧,春花似錦卑惜、人聲如沸靠益。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽胧后。三九已至,卻和暖如春抱环,著一層夾襖步出監(jiān)牢的瞬間壳快,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工镇草, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留眶痰,地道東北人。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓梯啤,卻偏偏與公主長得像竖伯,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子因宇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內(nèi)容

  • ORACLE自學(xué)教程 --create tabletestone ( id number, --序號usernam...
    落葉寂聊閱讀 1,081評論 0 0
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,451評論 0 13
  • Student(S#,Sname,Sage,Ssex) 學(xué)生表 Course(C#,Cname,T#) 課程表 S...
    忘了呼吸的那只貓閱讀 2,864評論 0 8
  • 接下來的一些內(nèi)容七婴,我們需要提前學(xué)一些簡單的sql語句,方便大家理解接下來的知識察滑。 DDL—數(shù)據(jù)定義語言(Creat...
    不排版閱讀 418評論 0 1
  • 做人如果沒有夢想打厘,跟咸魚有什么區(qū)別?一首五月天的「咸魚」贺辰,送給追夢路上累了户盯、倦了嵌施、疲了、孤單了的你莽鸭。 我是一只咸魚...
    零點(diǎn)陸一捌閱讀 278評論 0 5