【发布时间】:2014-07-04 14:23:11
【问题描述】:
我一直在尝试优化以下查询的性能。我请求这个领域的所有专家给我一个帮助和建议。
我有应用程序。 70k 条记录,我的要求是删除重复项。我需要提高以下查询的性能。
select *
from x.vw_records
where id not in
(select distinct id
from x.vw_datarecords
where effective_date >= trunc(sysdate - 30)
and book in (select book_shortname from x.vw_datarecords))
union
select distinct id
from x.vw_historyrecords
where effective_date >= trunc(sysdate - 30)
and book in (select book_shortname from x.vw_datarecords)
union
select distinct id
from x.vw_transactiondata
where effective_date >= trunc(sysdate - 30)
and book in (select book_shortname from x.vw_datarecords);
union
select distinct id
from x.vw_cashdata
where effective_date >= trunc(sysdate - 30)
and book in (select book_shortname from x.vw_datarecords)
目前数一数需要十分钟。使用计数(*)的行数。建议我调整此查询的性能。
提前致谢。
【问题讨论】:
-
你能读懂这个查询吗?我不能。
-
我认为您在第一个联合之前缺少左括号,并且查询末尾的左括号不应该存在,仅供参考。看起来无效
-
我会看看你是否真的需要在所有地方都使用 DISTINCT,因为这可能会对性能产生很大影响。如果 1 块的结果自然不会与第二块重叠,也请使用 UNION ALL 而不是 UNION,因为您正在让它做额外的工作来确定是否有任何重叠。
-
@BrianDeMilia 抱歉出现错误,查询正在执行。我需要的只是提高查询的性能。如果您有任何想法,请建议我。
-
我只是出于格式化目的对其进行了编辑。为每个表或至少一个列和索引列表提供 DDL 会有所帮助。如果您可以通过以一种或另一种方式使用其他列来避免使用 DISTINCT 并使用 UNION ALL 而不是 UNION 来避免可能会提高性能的重复项。 DISTINCT 通常不利于性能。
标签: sql oracle loops nested-queries notin