8.2.1.8 IS NULL Optimization IS NULL 优化 (索引不存储NULL值)
MySQL 可以执行相同的优化在col_name IS NULL,它可以使用col_name=常数,
比如, MySQL 可以使用索引和范围搜索找到NULL值
Examples:
SELECT * FROM tbl_name WHERE key_col IS NULL;
SELECT * FROM tbl_name WHERE key_col <=> NULL;
SELECT * FROM tbl_name
WHERE key_col=const1 OR key_col=const2 OR key_col IS NULL;
如果where 子句抱哈了一个col_name IS NULL 条件,列是申明为NULL,
表达式被优化。这个优化不发生当列可能产生NULL,例如, 如果它来自左连接的右侧表:
MySQL 也可以优化 组合的COL_NAME =expr 和col_name IS NUll,一个表格通常是解决子查询:
优化可以处理IS NULL 用于任何的key 部分:
有些查询例子是被优化的, 假设有一个索引在表t2列a和b:
SELECT * FROM t1 WHERE t1.a=expr OR t1.a IS NULL;
SELECT * FROM t1, t2 WHERE t1.a=t2.a OR t2.a IS NULL;
SELECT * FROM t1, t2
WHERE (t1.a=t2.a OR t2.a IS NULL) AND t2.b=t1.b;
SELECT * FROM t1, t2
WHERE t1.a=t2.a AND (t2.b=t1.b OR t2.b IS NULL);
SELECT * FROM t1, t2
WHERE (t1.a=t2.a AND t2.a IS NULL AND …)
OR (t1.a=t2.a AND t2.a IS NULL AND …);