SQL> create
table a (a number);
SQL> insert into a values (1);
SQL> insert
into a values (2);
SQL> create
table b (a number );
SQL> insert
into b values (1);
SQL> select
* from a where a.a not in (select a from b);
A
-----
2
SQL> select
* from a where not exists (select 1 from b where b.a=a.a);
A
-----
2
这个时候是没有什么区别的.
SQL> insert
into b values(null);
SQL> commit ;
SQL> select
* from a where a.a not in (select a from b);
no
rows selected;
SQL> select
* from a where not exists (select 1 from b where b.a=a.a);
A
-----
2
我们发现当NOT IN 的subquery 中存在null 的情况下,会返回空行,所以我们再写SQL 的not in 的subquery 的时候,要加入 where a is not null;
在oracle CBO中,not exists 与 not in 都是优化器都是选择anti_join的连接方式.
Trackback URL: http://blog.itpub.net//trackback.php?id=86651
| « | 十一月 2008 | » | ||||
|---|---|---|---|---|---|---|
| 一 | 二 | 三 | 四 | 五 | 六 | 日 |
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |