【MYSQL 索引笔记】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zzjfengz/article/details/82773617

Where 条件为IS NOT NULL OR IS NULL

【字段属性为null】

  1. 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; – 数据量大约为11

SELECT * FROM ***_repair_sheet_zuche_info WHERE claim_nos IS NULL; -- 11 走索引
  1. 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】

  1. 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;
  1. 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 都不走索引

猜你喜欢

转载自blog.csdn.net/zzjfengz/article/details/82773617