2013-02-08 81 views
0

我想要的,進而優化查詢follwing,不使用子查詢得到最大值:查詢優化與條件

select c.ida2a2 from table1 m, table2 c 
where c.ida3a5 = m.ida2a2 
and (c.createstampa2 < (select max(cc.createstampa2) 
         from table2 cc where cc.ida3a5 = c.ida3a5)); 

任何想法?請讓我知道,如果你想獲得更多的信息。

回答

3

這可能是編寫查詢更有效的方式:

select c.ida2a2 
from table1 m join 
    (select c.*, MAX(createstampa2) over (partition by ida3a5) as maxcs 
     from table2 c 
    ) c 
    on c.ida3a5 = m.ida2a2 
where c.createstampa2 < maxcs 

我敢肯定正確的Oracle優化這個(過濾加入前行)。如果你想更清楚:

select c.ida2a2 
from table1 m join 
    (select c.* 
     from (select c.*, MAX(createstampa2) over (partition by ida3a5) as maxcs 
      from table2 c 
      ) c 
     where c.createstamp2 < c.maxcs 
    ) c 
    on c.ida3a5 = m.ida2a2 
+0

@rs。 。 。 。你是對的。出於某種原因,我在腦海中使用了'm'表而不是'c'表,因此我有'where'的條件。 – 2013-02-08 16:56:09