楼主: rollingpig

[精华] 一次SQL Tuning引出来的not in , not exists 语句的N种写法

[复制链接]
论坛徽章:
4
授权会员
日期:2005-10-30 17:05:33ITPUB元老
日期:2005-11-28 09:50:23会员2006贡献徽章
日期:2006-04-17 13:46:34BLOG每日发帖之星
日期:2009-05-20 01:01:05
31#
发表于 2005-8-26 15:39 | 只看该作者
好贴,工作中经常好在几千万记录中删除几千条记录,好方法,收藏

使用道具 举报

回复
论坛徽章:
3
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:34
32#
发表于 2005-8-26 15:57 | 只看该作者
sql优化真的是经验的积累啊!

使用道具 举报

回复
论坛徽章:
3
授权会员
日期:2005-10-30 17:05:33ITPUB元老
日期:2007-10-01 20:37:26
33#
发表于 2005-8-26 17:43 | 只看该作者
我也要慢慢来学习呵呵

使用道具 举报

回复
论坛徽章:
1
会员2006贡献徽章
日期:2006-04-17 13:46:34
34#
发表于 2005-9-5 15:48 | 只看该作者

以前看过的tom的回答,或许能从中找到些启发

You Asked (Jump to Tom's latest followup)

Tom:

can you give me some example at which situation
IN is better than exist, and vice versa.


and we said...

Well, the two are processed very very differently.

Select * from T1 where x in ( select y from T2 )

is typically processed as:

select *
  from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;

The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then
joined to the original table -- typically.


As opposed to

select * from t1 where exists ( select null from t2 where y = x )

That is processed more like:


   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

It always results in a full scan of T1 whereas the first query can make use of
an index on T1(x).


So, when is where exists appropriate and in appropriate?

Lets say the result of the subquery
    ( select y from T2 )

is "huge" and takes a long time.  But the table T1 is relatively small and
executing ( select null from t2 where y = x.x ) is very very fast (nice index on
t2(y)).  Then the exists will be faster as the time to full scan T1 and do the
index probe into T2 could be less then the time to simply full scan T2 to build
the subquery we need to distinct on.


Lets say the result of the subquery is small -- then IN is typicaly more
appropriate.


If both the subquery and the outer table are huge -- either might work as well
as the other -- depends on the indexes and other factors.

使用道具 举报

回复
论坛徽章:
1
生肖徽章2007版:鸡
日期:2008-01-02 17:35:53
35#
发表于 2005-10-9 16:32 | 只看该作者
相比较起来out join应该是建议最少用的了。。。

使用道具 举报

回复
论坛徽章:
168
马上加薪
日期:2014-02-19 11:55:142012新春纪念徽章
日期:2012-02-13 15:10:582012新春纪念徽章
日期:2012-01-04 11:49:54蜘蛛蛋
日期:2011-12-05 16:08:56ITPUB十周年纪念徽章
日期:2011-11-01 16:19:41设计板块每日发贴之星
日期:2011-07-22 01:01:02ITPUB官方微博粉丝徽章
日期:2011-06-30 12:30:16管理团队成员
日期:2011-05-07 01:45:082011新春纪念徽章
日期:2011-01-25 15:42:562011新春纪念徽章
日期:2011-01-25 15:42:33
36#
发表于 2005-10-12 11:58 | 只看该作者
10g的话,oracle对exists/in的处理已经没有很大的区别了。

使用道具 举报

回复
37#
发表于 2005-10-12 16:41 | 只看该作者
谢谢楼主,提供了很好的思路

使用道具 举报

回复
论坛徽章:
0
38#
发表于 2005-11-18 23:37 | 只看该作者
UP

使用道具 举报

回复
论坛徽章:
4
设计板块每日发贴之星
日期:2008-03-26 01:07:45奥运会纪念徽章:乒乓球
日期:2008-10-24 13:13:51
39#
发表于 2005-11-19 15:51 | 只看该作者
楼主可真是高手啊。经验丰富的人就是不一样!

使用道具 举报

回复
论坛徽章:
0
40#
发表于 2005-11-29 17:31 | 只看该作者
a

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表