为什么使用CASE的subselect比在Oracle中使用OR的JOIN更快

时间:2015-07-01 10:03:40

标签: sql oracle left-join query-optimization case

我正在优化我们的一个糟糕的观点,令人惊讶的是,CASE语句的子选项之一的运行速度比LEFT JOIN的{​​{1}}快。原始视图要大得多,但我感兴趣的部分可以归结为以下查询

OR

enter image description here

执行计划

enter image description here

而加入的人一直比较慢

SELECT CASE 
     WHEN tdcurr.productid = 1 THEN (SELECT addressid 
                                     FROM   address a 
                                     WHERE  a.customerid = tm.customerid 
                                            AND a.addressid = 
                                                tdcurr.addressid 
                                            AND a.addresstypeid = 3) 
     WHEN tdcurr.productid = 2 THEN (SELECT addressid 
                                     FROM   address a 
                                     WHERE  a.customerid = tm.customerid 
                                            AND a.addressid = 
                                                tdcurr.addressid 
                                            AND a.addresstypeid = 4) 
   END AS t_buyselladdressid 
FROM   vleaf_transactiondetail_all tdcurr 
   inner join transactionmain tm 
           ON tm.transactionid = tdcurr.transactionid 

enter image description here

执行计划

enter image description here

为什么会出现这种情况?

1 个答案:

答案 0 :(得分:2)

带子选择的SQL可能会受益于标量子查询缓存。从解释计划来看,看起来好像没有做嵌套循环外连接!

有关标量子查询缓存的更多信息,请参阅https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2683853500346598211