[20121028]not in与NULL问题.txt

简介: [20121028]not in与NULL问题.txt在sql语句中使用not in,在遇到子表含有NULL的情况下,会出现没有行返回的情况,自己遇到过几次,好几次没有转过弯来。
[20121028]not in与NULL问题.txt

在sql语句中使用not in,在遇到子表含有NULL的情况下,会出现没有行返回的情况,自己遇到过几次,好几次没有转过弯来。
今天记录一下,避免以后再犯类似错误!

1.建立环境:

select * from v$version where rownum
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

drop table t1 purge;
drop table t2 purge;
create table t1 as select rownum id ,lpad('t1',6,'x') v1 from dual connect by level
create table t2 as select rownum+1 id ,lpad('t2',6,'x') v1 from dual connect by level

2.测试

SQL> select t1.* from t1 where t1.id not in ( select id from t2 );

        ID V1
---------- ------
         1 xxxxt1
        10 xxxxt1

select t1.* from t1 where not exists ( select 1 from t2  where t2.id=t1.id);

        ID V1
---------- ------
         1 xxxxt1
        10 xxxxt1

--如果插入NULL到T2后呢?

insert into t2 values (NULL,'t2yyyy');
commit;

SQL> select t1.* from t1 where t1.id not in ( select id from t2 );
no rows selected
--???没有行返回!

SQL> select t1.* from t1 where not exists ( select 1 from t2  where t2.id=t1.id);

        ID V1
---------- ------
         1 xxxxt1
        10 xxxxt1

在使用not in时,如果子查询的结果包含 NULL, NULL表示未知,not in里面包含NULL表示不在NULL里面,这样的查询结果也是未知,
这样主查询返回的结果为0。

select t1.* from t1 where t1.id not in ( NULL,1 );

要避免这个错误,修改如下:

SQL> select t1.* from t1 where t1.id not in ( select id from t2  where id is not null );

        ID V1
---------- ------
         1 xxxxt1
        10 xxxxt1

3.看看执行计划:
SQL> select t1.* from t1 where t1.id not in ( select id from t2  where id is not null );

        ID V1
---------- ------
         1 xxxxt1
        10 xxxxt1

SQL> @dpc

PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------
SQL_ID  cuy5z0y79wrn8, child number 0
-------------------------------------
select t1.* from t1 where t1.id not in ( select id from t2  where id is
not null )

Plan hash value: 1270581391

------------------------------------------------------------------------------------
| Id  | Operation          | Name | E-Rows | Cost (%CPU)|  OMem |  1Mem | Used-Mem |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |        |     8 (100)|       |       |          |
|*  1 |  HASH JOIN ANTI SNA|      |     10 |     8  (13)|  1180K|  1180K| 1425K (0)|
|   2 |   TABLE ACCESS FULL| T1   |     10 |     3   (0)|       |       |          |
|*  3 |   TABLE ACCESS FULL| T2   |      8 |     4   (0)|       |       |          |
------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("T1"."ID"="ID")
   3 - filter("ID" IS NOT NULL)

--  HASH JOIN ANTI SNA表示什么意思? SNA表示什么?

SQL> select t1.* from t1 where t1.id not in ( select id from t2   );

no rows selected

SQL> @dpc

PLAN_TABLE_OUTPUT
-------------------------------------------------------------------------------------
SQL_ID  3u24wpavnkahc, child number 0
-------------------------------------
select t1.* from t1 where t1.id not in ( select id from t2   )

Plan hash value: 1275484728

------------------------------------------------------------------------------------
| Id  | Operation          | Name | E-Rows | Cost (%CPU)|  OMem |  1Mem | Used-Mem |
------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |        |     8 (100)|       |       |          |
|*  1 |  HASH JOIN ANTI NA |      |     10 |     8  (13)|  1180K|  1180K| 1129K (0)|
|   2 |   TABLE ACCESS FULL| T1   |     10 |     3   (0)|       |       |          |
|   3 |   TABLE ACCESS FULL| T2   |      9 |     4   (0)|       |       |          |
------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("T1"."ID"="ID")

--  HASH JOIN ANTI NA表示什么意思? NA表示什么?

目录
相关文章
|
SQL Oracle 关系型数据库
[20170516]nvl与非NULL约束.txt
[20170516]nvl与非NULL约束.txt --前几天做的测试http://blog.itpub.net/267265/viewspace-2137853/,实际上差异没有这个大,因为第2个多数是常量.
854 0
|
索引 关系型数据库 Oracle
[20160704]NULL与主外键问题.txt
[20160704]NULL与主外键问题.txt --主外键的问题主要出现在阻塞等情况,有许多极端dba认为应该取消这个约束.当然从使用的角度出发有总比没有好.只是不要过度滥用.
773 0
|
存储 Oracle 关系型数据库
[20160619]NULL在数据库的存储.txt
[20160619]NULL在数据库的存储.txt --简单探究NULL在数据库的存储.这也是别人前几天问的问题,我自己学习oracle这么久,也没有仔细观察过. 1.
841 0
|
Oracle 关系型数据库 Linux
[20151207]filter( IS NULL).txt
[20151207]filter( IS NULL).txt --前一阵子别人问的问题,filter (IS NOT NULL)是什么意思? -- http://www.
778 0
|
Oracle 关系型数据库
[20140823]12c null与缺省值.txt
[20140823]12c null与缺省值.txt --12c 当插入NULL时可以指定缺省值.不知道为什么设置这个特性,有点怪怪的. SCOTT@test01p> @ver BANNER                                ...
725 0
|
SQL 物联网 索引
[20121028]IOT的第2索引-NULL的问题.txt
[20121028]IOT的第2索引-NULL的问题.txt IOT表实际上时索引结构,如果第2索引的键值为NULL,会是什么情况呢? 因为第2索引包含主键,而主键是不能为NULL的,这样即使第2索引的键值为NULL,会包括在第2索引中吗? 自己做一些测试验证看看: 1.
731 0
|
SQL 关系型数据库 Oracle
[20121020]主外键约束以及NULL问题.txt
[20121020]主外键约束以及NULL问题.txt 主外键约束可以一定程度保证数据完整性,但是如果外键输入的是NULL,情况会如何呢? SQL> select * from v$version ; BANNER -----------------------...
751 0
|
3月前
|
机器学习/深度学习 SQL 关系型数据库
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
39 0
|
3月前
|
SQL 关系型数据库 MySQL
总结 vue3 的一些知识点:MySQL NULL 值处理
总结 vue3 的一些知识点:MySQL NULL 值处理
|
5月前
|
SQL 关系型数据库 MySQL
MySQL NULL 值处理
MySQL NULL 值处理