简体   繁体   English

查询优化[Oracle]

[英]Query optimization [Oracle]

How can I optimize the performance of this query? 如何优化此查询的性能?

SELECT Count(DISTINCT DT.id) 
FROM   pcwdeptrans DT 
       INNER JOIN pcwitemtotal IT 
               ON DT.id = IT.deposittransid 
       LEFT OUTER JOIN pcwdepreceipt DR 
                    ON DR.deposittransid = DT.id 
WHERE  (( ( DT.statecode IN ( :1, :2, :3, :4, 
                              :5, :6, :7, :8 ) 
             OR ( DT.statecode IN ( :9 ) 
                  AND IT.statecode = :10 ) ) 
          AND DR.requesttime >= :11 
          AND DR.requesttime <= :12 
          AND DR.userid = :13 )) 

Please help me with the right syntax if you think its incorrect. 如果您认为语法错误,请以正确的语法帮助我。

But this is an application query, identified from the AWR report as part of performance analysis 但这是一个应用程序查询,是从AWR报告中识别出来的,作为性能分析的一部分

Excerpt from AWR: AWR摘录:

Top SQL with TOP Events 带有TOP事件的热门SQL

Its Execution plan from test DB (prod will be different): 它来自测试数据库的执行计划(产品将有所不同):

Execution plan of the query 查询的执行计划

Join together with Where can be slow. 与“哪里很慢”一起加入。 following could be faster (but not the same... see comments and rethink if you need inner or outer join...) 以下操作可能会更快(但不相同...请参阅注释,然后重新考虑是否需要内部或外部连接...)

SELECT Count(DISTINCT DT.id) 
FROM pcwdeptrans DT 
  INNER JOIN pcwitemtotal IT 
      ON DT.id = IT.deposittransid 
     AND ( DT.statecode IN ( :1, :2, :3, :4, :5, :6, :7, :8 ) 
       OR ( DT.statecode IN ( :9 ) AND IT.statecode = :10 ) )
  LEFT OUTER JOIN pcwdepreceipt DR 
      ON DR.deposittransid = DT.id 
     AND DR.requesttime >= :11 
     AND DR.requesttime <= :12 
     AND DR.userid = :13

edit: The discussion whether it should be an inner or outer join was already started in the question's comments before. 编辑:关于是内部连接还是外部连接的讨论已在问题的注释中开始。 Transforming the question's outer join to a inner join with the where clause may or may not be intendet. 使用where子句可以将问题的外部联接转换为内部联接,也可以不是。

SELECT Count(DISTINCT DT.id) 
FROM pcwdeptrans DT 
  INNER JOIN pcwitemtotal IT 
      ON DT.id = IT.deposittransid 
     AND ( DT.statecode IN ( :1, :2, :3, :4, :5, :6, :7, :8 ) 
       OR ( DT.statecode IN ( :9 ) AND IT.statecode = :10 ) )
  INNER JOIN pcwdepreceipt DR 
      ON DR.deposittransid = DT.id 
     AND DR.requesttime >= :11 
     AND DR.requesttime <= :12 
     AND DR.userid = :13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM