Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 冲浪宝典 > 网络资源 > IN&EXISTS与NOT IN&NOT EXISTS 的优化原则的讨论
【标  题】:IN&EXISTS与NOT IN&NOT EXISTS 的优化原则的讨论
【关键字】:IN,EXISTS,NOT,IN,NOT,EXISTS
【来  源】:http://www.cublog.cn/u/11598/showart.php?id=253204

IN&EXISTS与NOT IN&NOT EXISTS 的优化原则的讨论

Your Ad Here
1.
EXISTS的执行流程       
select * from t1 where exists ( select null from t2 where y = x )
可以理解为:
   for x in ( select * from t1 )
   loop
      if ( exists ( select null from t2 where y = x.x )
      then
         OUTPUT THE RECORD
      end if
   end loop
对于in 和 exists的性能区别:
   如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in,反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists
   其实我们区分in和exists主要是造成了驱动顺序的改变(这是性能变化的关键),如果是exists,那么以外层表为驱动表,先被访问,如果是IN,那么先执行子查询,所以我们会以驱动表的快速返回为目标,那么就会考虑到索引及结果集的关系了
                           
另外IN时不对NULL进行处理
如:
select 1 from dual where null  in (0,1,2,null)
为空
 
2.NOT IN 与NOT EXISTS:       
NOT EXISTS的执行流程
select .....
  from rollup R
where not exists ( select 'Found' from title T
                             where R.source_id = T.Title_ID);
可以理解为:
for x in ( select * from rollup )
      loop
          if ( not exists ( that query ) ) then
                 OUTPUT
          end if;
       end;

注意:NOT EXISTS 与 NOT IN 不能完全互相替换,看具体的需求。如果选择的列可以为空,则不能被替换。

例如下面语句,看他们的区别:
select x,y from t;
x              y
------         ------
1              3
3        1
1        2
1        1
3        1
5
select * from t where  x not in (select y from t t2  )
no rows
       
select * from t where  not exists (select null from t t2
                                                  where t2.y=t.x )
x       y
------  ------
5       NULL
所以要具体需求来决定

对于not in 和 not exists的性能区别:
   not in 只有当子查询中,select 关键字后的字段有not null约束或者有这种暗示时用not in,另外如果主查询中表大,子查询中的表小但是记录多,则应当使用not in,并使用anti hash join.
   如果主查询表中记录少,子查询表中记录多,并有索引,可以使用not exists,另外not in最好也可以用/*+ HASH_AJ */或者外连接+is null
NOT IN 在基于成本的应用中较好

比如:
select .....
from rollup R
where not exists ( select 'Found' from title T
                           where R.source_id = T.Title_ID);

改成(佳)

select ......
from title T, rollup R
where R.source_id = T.Title_id(+)
    and T.Title_id is null;
                                 
或者(佳)
sql> select /*+ HASH_AJ */ ...
        from rollup R
        where ource_id NOT IN ( select ource_id
                                               from title T
                                              where ource_id IS NOT NULL )
 
建两个表,outer大表,外层表/inner小表,内层表

SQL> create table inner as select rownum as id from dba_tables;

Table created.

SQL> create table outer as select rownum*2 as id from dba_objects;

Table created.

SQL> select count(*) from inner;

COUNT(*)
----------
531

SQL> select count(*) from outer;

COUNT(*)
----------
27774

SQL> set timing on
SQL> set autotrace traceonly explain

SQL> select * from outer where id in (select id from inner);

265 rows selected.

Elapsed: 00:00:00.03

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 MERGE JOIN
2 1 SORT (JOIN)
3 2 TABLE ACCESS (FULL) OF 'OUTER'
4 1 SORT (JOIN)
5 4 VIEW OF 'VW_NSO_1'
6 5 SORT (UNIQUE)
7 6 TABLE ACCESS (FULL) OF 'INNER'

SQL> select * from outer where exists (select 'x' from inner where inner.id=outer.id);

265 rows selected.

Elapsed: 00:00:08.02

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE
1 0 FILTER
2 1 TABLE ACCESS (FULL) OF 'OUTER'
3 1 TABLE ACCESS (FULL) OF 'INNER'


没有对表分析之前:从上面看出(select id from inner)返回值比较少的话,用in比较快的;
第一句相当于select * from outer,(select distinct id from inner) where inner.id=outer.id

你可以试试,如果(select id from inner)返回值比(select id from outer)多的话,用exists快。

对表进行分析后,看看效果。使用了hash jion,两种方式效果相同了

SQL> analyze table inner compute statistics;

Table analyzed.

Elapsed: 00:00:00.06
SQL> analyze table outer compute statistics;

Table analyzed.

Elapsed: 00:00:00.08


SQL> select * from outer where id in (select id from inner);

265 rows selected.

Elapsed: 00:00:00.03

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=11 Card=531 Bytes=3717)
1 0 HASH JOIN (Cost=11 Card=531 Bytes=3717)
2 1 SORT (UNIQUE)
3 2 TABLE ACCESS (FULL) OF 'INNER' (Cost=2 Card=531 Bytes=1593)
4 1 TABLE ACCESS (FULL) OF 'OUTER' (Cost=6 Card=27774 Bytes=111096)

SQL> select * from outer where exists (select 'x' from inner where inner.id=outer.id);

265 rows selected.

Elapsed: 00:00:00.03

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=11 Card=531 Bytes=3717)
1 0 HASH JOIN (Cost=11 Card=531 Bytes=3717)
2 1 SORT (UNIQUE)
3 2 TABLE ACCESS (FULL) OF 'INNER' (Cost=2 Card=531 Bytes=1593)
4 1 TABLE ACCESS (FULL) OF 'OUTER' (Cost=6 Card=27774 Bytes=111096)

北京益久科技-电脑维护,电脑外包服务,IT外包服务,计算机维护服务:【上一篇】
logminer(补):【下一篇】
【相关文章】
  • 开始linux编程:解读linux启动过程
  • linux 内核分析方法,两种
  • Linux下raid技术以及几种Raid介绍
  • linux 高手帮忙解决一下 万分感谢
  • 一周内学会linux网络服务
  • Linux内核升级 2.4.x to 2.6.x
  • 浅谈Linux内核编译过程
  • 在linux下映射thinkpad的win键
  • Linux 引导过程
  • swing界面设计<一>
  • 【随机文章】
  • 通过修改VCL源码实现自定义输入对话框
  • 工业控制--串口通讯方法(WINAPI实现)
  • VirtualTreeView V3.8.3 -> 4.0.7 (树视图)
  • Oracle 9.2.0.1在hpux下的安装过程
  • 手把手教你制作Google Sitemap(详细制作教程和协议讲解)
  • laszlo webtop正在接受试用申请
  • 改变Windows XP启动画面
  • Boost库安装配置
  • 保护SQL2000安全的十个技巧
  • linux c 基础学习总结之文件IO
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.