具有条件的查询优化

时间:2013-02-08 16:44:52

标签: sql oracle query-optimization

我想优化以下查询,不使用子查询来获取最大值:

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));

有什么想法吗?如果您想获得更多信息,请与我们联系。

1 个答案:

答案 0 :(得分: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