在where子句中优化SQL子查询

时间:2015-07-16 20:45:07

标签: sql oracle oracle11g query-optimization

我使用下面的Oracle SQL Developer的sql工作正常,但是我担心在更大的实时数据库上的性能。

请原谅所有连接表,这显示了与_aud表和revision_table的连接,以保持审计历史记录。

select cust.forename, cust.surname
from customer cust
join section_details sd on cust.section = sd.section
where
-- this value is substituted programatically
sd.type = 5 and
(
  (select max(rt.timestamp)
  from
  customer cust_tmp1
  join section_details sd on cust_tmp1.section = sd.section
  join measure msr on sd.measure = msr.id
  join measure_aud msr_a on msr.id = msr_a.id
  join revision_table rt on msr_a.rev = rt.id
  where
  cust_tmp1.id = cust.id and msr.measure = 'Last Contact Date')
> 
  (select max(rt.timestamp)
  from
  customer cust_tmp2
  join section_details sd on cust_tmp2.section = sd.section
  join measure msr on sd.measure = msr.id
  join measure_aud msr_a on msr.id = msr_a.id
  join revision_table rt on msr_a.rev = rt.id
  where
  cust_tmp2.id = cust.id and  msr.measure = 'Last Order Date')
);

简而言之,如果“最后联系日期”比“最后订单日期”更新,我只想检索客户详细信息。我最终检查一个select的max(时间戳)大于另一个select的max(timestamp)。

一切正常,我得到了我期待的结果。

除了msr.measure ='最后联系日期'或'最后订单日期'之外,时间戳比较的每一面都是重复的。

我尝试了一些从未真正起作用的替代方案,因为它们导致了多级嵌套子查询,并且我无法传入外部cust记录。

非常感谢任何进一步的想法。

3 个答案:

答案 0 :(得分:1)

你确定需要所有这些连接吗?

具有不同WHERE条件的相同查询通常转换为CASE:

select cust.forename, cust.surname
from customer cust
where
  -- this value is substituted programatically
   sd.type = 5 
and exists
 ( select *
   from
   customer cust_tmp1
   join section_details sd on cust_tmp1.section = sd.section
   join measure msr on sd.measure = msr.id
   join measure_aud msr_a on msr.id = msr_a.id
   join revision_table rt on msr_a.rev = rt.id
   where cust_tmp1.id = cust.id

   having max(case when msr.measure = 'Last Contact Date' then rt.timestamp end)
        > max(case when msr.measure = 'Last Order Date'   then rt.timestamp end)
 )

或简化删除子查询:

select cust.forename, cust.surname
from customer cust
join section_details sd on cust_tmp1.section = sd.section
join measure msr on sd.measure = msr.id
join measure_aud msr_a on msr.id = msr_a.id
join revision_table rt on msr_a.rev = rt.id
where
  -- this value is substituted programatically
   sd.type = 5 
group by cust.forename, cust.surname
having max(case when msr.measure = 'Last Contact Date' then rt.timestamp end)
     > max(case when msr.measure = 'Last Order Date'   then rt.timestamp end)

答案 1 :(得分:0)

您可以将其切换为exists

exists (select max(rt.timestamp)
        from customer cust_tmp1 join
             section_details sd
             on cust_tmp1.section = sd.section join
             measure msr
             on sd.measure = msr.id join
             measure_aud msr_a
             on msr.id = msr_a.id join
             revision_table rt
             on msr_a.rev = rt.id
        where cust_tmp1.id = cust.id and
              msr.measure in ( 'Last Contact Date', 'Last Order Date')
        having max(case when msr.measure = 'Last Contact Date' then rt.timestamp end) >
               max(case when msr.mesure = 'Last Order Date' then rt.timestamp end)
       );

我不确定是否会有很大的性能提升。如果您在数据库中设置了正确的索引,那么您的原始版本应该非常快且可扩展。

答案 2 :(得分:-1)

我尝试了这里发布的解决方案,它们似乎都有效,非常感谢您的回复 - 我之前没有调查过有条款。

我添加了所有必需的索引并对所有选项运行了解释计划,我的原始查询成本最低。所以我想我会继续使用这个选项。