【MYSQL 索引筆記】
阿新 • • 發佈:2018-11-03
Where 條件為IS NOT NULL OR IS NULL
【欄位屬性為null】
- where 條件中有is null 時 索引起作用
例子1
SELECT COUNT(1) FROM ***_repair_sheet_zuche_info a WHERE a.`loss_assessment_status` IS NULL -- 資料量大約為4w SELECT * FROM ***_repair_sheet_zuche_info WHERE loss_assessment_status IS NULL -- 4w 走索引
例子 2
SELECT COUNT(1) FROM ***_repair_sheet_zuche_info WHERE claim_nos IS
NULL; – 資料量大約為11SELECT * FROM ***_repair_sheet_zuche_info WHERE claim_nos IS NULL; -- 11 走索引
- where 條件中有 is not null 時候 索引起作用(資料量差異性不會很大)
例子1
SELECT COUNT(1) FROM ***_repair_sheet_zuche_info a WHERE a.`loss_assessment_contacts` IS NOT NULL -- 資料量大約為667 SELECT * FROM ***_repair_sheet_zuche_info WHERE loss_assessment_status IS NOT NULL -- 667 走索引
例子2
-- 4w 資料量遠遠大於為空的資料 SELECT COUNT(1) FROM ***_repair_sheet_zuche_info a WHERE a.claim_nos IS NOT NULL; -- 4w 資料量遠遠大於為空的資料 不走索引,全表掃描 比走索引更好 SELECT * FROM ***_repair_sheet_zuche_info a WHERE a.claim_nos IS NOT NULL;-
綜述: 欄位屬性為null 時, where 條件中 is not null 和 is null 都走索引 (前提有建索引)(除了資料差異性太大除外)
【欄位屬性為not null】
- where 條件中有 is not null 時 索引不起作用
– 4w 全表掃描 欄位型別已定義為not null ,加索引對於where 條件中的is not null 沒有什麼意義
SELECT * FROM ***_repair_sheet_zuche_info a WHERE a.repair_sheet_id IS NOT NULL;
- where 條件中有 is null 時 索引不起作用
SELECT * FROM t_vrms_repair_sheet_zuche_info WHERE repair_sheet_id IS NULL; -- 0 同理
綜述 欄位屬性為not null 時,where 條件中 is not null 和 is null 都不走索引