«July 2025»
12345
6789101112
13141516171819
20212223242526
2728293031


公告
暂无公告...

我的分类(专题)

首页(52)
Oracle学习笔记(2)
开发(18)
其他(5)
项目管理(0)
奶爸手记(11)
小故事(3)
随笔(6)
手机开发(7)


最新日志
Jbuilder 2005光标问题的解决
明码标价:谈待遇的要领和禁忌
CVS使用手册
3G标志性业务介绍
请教:如何正确获得table的子结点数量
RE:UNIX常用命令
如何退订手机收费业务
女儿翻身了
从Excle倒入数据到oracle的方法
java.io.IOException:

最新回复
回复:如何退订手机收费业务
回复:如何退订手机收费业务
回复:如何退订手机收费业务
回复:如何退订手机收费业务
回复:如何退订手机收费业务
回复:什么是MISC?
回复:麻省理工学院的开放式课程计划
回复:如何退订手机收费业务
回复:麻省理工学院的开放式课程计划
回复:B/S开发常用JavaScript

留言板
签写新留言


统计
blog名称:V-加油
日志总数:52
评论数量:83
留言数量:0
访问次数:250643
建立时间:2005年2月13日

链接

免费软件



本站首页    管理页面    写新日志    退出

[Oracle学习笔记]关于in和exists的效率
vacobo@sogou.com 发表于 2004/12/1 18:35:33

askTom上总是看到一些以前与大多的观点不尽相同的观点,这次关于in和exists的效率的讨论,实在是有启发。Tom said:be aware of their differences, try them both when tuning, understand conceptually what they do and you'll be able to use them to maximum effect. 这确实是在这里最大的收获,谢谢Tom 下面摘录一些有用的片段: i verified it and the "rule of thumb" holds true.  BIG outer query and SMALL inner query = IN.  SMALL outer query and BIG inner query = WHERE EXISTS.  Remember -- thats is a RULE OF THUMB and rules of thumb always have infinitely many exceptions to the rule. the WHERE EXISTS will find the first row faster in general then the IN will -- the IN will get the LAST row (all rows) faster then the where exists。 Well -- I'm not going to use EMP and DEPT as I would  have to generate tons of EMP and DEPT data to illustrate my point (feel free to do that if you want to ;) I'll use BIG and SMALL to make the point. I ran: rem create table big as select * from all_objects;rem insert /*+ append */ into big select * from big;rem commit;rem insert /*+ append */ into big select * from big;rem commit;rem insert /*+ append */ into big select * from big;rem create index big_idx on big(object_id);remremrem create table small as select * from all_objects where rownum < 100;rem create index small_idx on small(object_id);remrem analyze table big compute statisticsrem for tablerem for all indexesrem for all indexed columnsrem /rem analyze table small compute statisticsrem for tablerem for all indexesrem for all indexed columnsrem / so, small has 99 rows, big has 133,000+ select count(subobject_name)  from big where object_id in ( select object_id from small ) call     count       cpu    elapsed       disk      query    current        rows------- ------  -------- ---------- ---------- ---------- ----------  ----------Parse        1      0.01       0.01          0          0          0           0Execute      1      0.00       0.00          0          0          0           0Fetch        2      0.02       0.02          0        993          0           1------- ------  -------- ---------- ---------- ---------- ----------  ----------total        4      0.03       0.03          0        993          0           1 Rows     Execution Plan-------  ---------------------------------------------------      0  SELECT STATEMENT   GOAL: CHOOSE      1   SORT (AGGREGATE)    792    MERGE JOIN    100     SORT (JOIN)    100      VIEW OF 'VW_NSO_1'     99       SORT (UNIQUE)    792        INDEX   GOAL: ANALYZED (FULL SCAN) OF 'SMALL_IDX'                   (NON-UNIQUE)    891     SORT (JOIN)      0      TABLE ACCESS   GOAL: ANALYZED (FULL) OF 'BIG' versus: select count(subobject_name)  from big where exists ( select null from small where small.object_id = big.object_id ) call     count       cpu    elapsed       disk      query    current        rows------- ------  -------- ---------- ---------- ---------- ----------  ----------Parse        1      0.00       0.00          0          0          0           0Execute      1      0.00       0.00          0          0          0           0Fetch        2      4.12       4.12          0     135356         15           1------- ------  -------- ---------- ---------- ---------- ----------  ----------total        4      4.12       4.12          0     135356         15           1 Rows     Execution Plan-------  ---------------------------------------------------      0  SELECT STATEMENT   GOAL: CHOOSE      1   SORT (AGGREGATE)    792    FILTER 135297     TABLE ACCESS   GOAL: ANALYZED (FULL) OF 'BIG' 133504     INDEX   GOAL: ANALYZED (RANGE SCAN) OF 'SMALL_IDX'                (NON-UNIQUE) That shows if the outer query is "big" and the inner query is "small", in is generally more efficient then NOT EXISTS. Now: select count(subobject_name)  from small where object_id in ( select object_id from big ) call     count       cpu    elapsed       disk      query    current        rows------- ------  -------- ---------- ---------- ---------- ----------  ----------Parse        1      0.01       0.01          0          0          0           0Execute      2      0.00       0.00          0          0          0           0Fetch        2      0.51       0.82         50        298         22           1------- ------  -------- ---------- ---------- ---------- ----------  ----------total        5      0.52       0.83         50        298         22           1   Rows     Execution Plan-------  ---------------------------------------------------      0  SELECT STATEMENT   GOAL: CHOOSE      1   SORT (AGGREGATE)     99    MERGE JOIN  16913     SORT (JOIN)  16912      VIEW OF 'VW_NSO_1'  16912       SORT (UNIQUE) 135296        INDEX   GOAL: ANALYZED (FAST FULL SCAN) OF 'BIG_IDX'                   (NON-UNIQUE)     99     SORT (JOIN)     99      TABLE ACCESS   GOAL: ANALYZED (FULL) OF 'SMALL' versus:select count(subobject_name)  from small where exists ( select null from big where small.object_id = big.object_id ) call     count       cpu    elapsed       disk      query    current        rows------- ------  -------- ---------- ---------- ---------- ----------  ----------Parse        1      0.00       0.00          0          0          0           0Execute      1      0.00       0.00          0          0          0           0Fetch        2      0.01       0.01          0        204         12           1------- ------  -------- ---------- ---------- ---------- ----------  ----------total        4      0.01       0.01          0        204         12           1 EGATE)     99    FILTER    100     TABLE ACCESS   GOAL: ANALYZED (FULL) OF 'SMALL'     99     INDEX   GOAL: ANALYZED (RANGE SCAN) OF 'BIG_IDX' (NON-UNIQUE) shows that is the outer query is "small" and the inner query is "big" -- a WHERE EXISTS can be quite efficient.

阅读全文(1968) | 回复(0) | 编辑 | 精华


发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144768659 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号