not in、outer joins与not exists

上一篇 / 下一篇  2010-06-22 17:43:50 / 个人分类:ORACLE

51Testing软件测试网4t*Q%I9RP_i q

NOT IN:
Lr8@5QW1Y0select *51Testing软件测试网WH@%`| G#[5S+c6kQ
  from rateparam1.Work_Order_Def a
o I"?2Y.Q_L0 where workorder_id not in
EO2T-HC`T)b0       (select b.workorder_id from rateparam1.work_order_process_log b);

uV't/l}-`9L051Testing软件测试网(cv2E(G"iMuM/^

OUTER JOINS:        51Testing软件测试网 N9~loo%mh(j@'V
select * from rateparam1.Work_Order_Def a,rateparam1.work_order_process_log b 
a8z Qi Oa0where a.workorder_id = b.workorder_id(+)51Testing软件测试网.t'O u6`.glr]
      and b.workorder_id is null;51Testing软件测试网6f)IE {6sR E

b7?5q.]an0U9m0NOT EXISTS:51Testing软件测试网"I(o!wL1Gs(fuh P
select *51Testing软件测试网6Ir{ Fd1|)h7\&p
  from rateparam1.Work_Order_Def a
#G9P+bN1L&K!j0 where not exists (select *51Testing软件测试网XJRb+t3p2`
          from rateparam1.work_order_process_log b51Testing软件测试网"S4i5^(Q0jVQF&pe
         where a.workorder_id = b.workorder_id)51Testing软件测试网3rg#O6rX5X

ew-M+a_ `u&X.v051Testing软件测试网/r%g!ZZ5Re_{n2x
A表大于B表,查找A>B的部分(A中存在,B中不存在)。51Testing软件测试网;| s}5BC
select * from A
?t'}O-k4bb0        where not exists (select * from B51Testing软件测试网7G\LBu T2K
                        where A.** = B.**51Testing软件测试网8\%c!SS@
                              and ...)---B表=A表的部分51Testing软件测试网u ak R;g-\ O

51Testing软件测试网9w(@/neb)a*c e

NOT IN < OUTER JOIN < NOT EXISTS51Testing软件测试网,Q3b+T4vZ-KL?

,bO4u1_9C3M0   在子查询中,NOT IN子句将执行一个内部的排序和合并. 无论在哪种情况下,NOT IN都是最低效的 (因为它对子查询中的表执行了一个全表遍历). 为了避免使用NOT IN ,我们可以把它改写成外连接(Outer Joins)或NOT EXISTS
yi7yu$c*a0    如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引;而not extsts 的子查询依然能用到表上的索引。
e_ sf"w s9l0  通过使用EXIST,Oracle系统会首先检查主查询,然后运行子查询直到它找到第一个匹配项,这就节省了时间。Oracle系统在执行IN子查询时,首先执行子查询,并将获得的结果列表存放在在一个加了索引的临时表中。在执行子查询之前,系统先将主查询挂起,待子查询执行完毕,存放在临时表中以后再执行主查询。这也就是使用EXISTS比使用IN通常查询速度快的原因。  
$C"ssN w+n2t.l)b1r0  同时应尽可能使用NOT EXISTS来代替NOT IN,尽管二者都使用了NOT(不能使用索引而降低速度),NOT   EXISTS要比NOT IN查询效率更高。

(K9fx0`@]Q5od0

TAG:

 

评分:0

我来说两句

Open Toolbar