【问题标题】:joining same table multiple times in Oracle在Oracle中多次加入同一个表
【发布时间】:2021-04-01 06:41:44
【问题描述】:

嗨,我是 tuning 一个遗留代码。我们在一个大查询中有以下 2 个表。

  fnd_currencies, and pa_commitment_txns

我在from子句中看到过

  fnd_currencies fca,
  fnd_currencies fcr,
  fnd_currencies fcp,
  pa_commitment_txns pct
 

第一个表已经被使用了三次,并且已经与同一个表的 column 进行了外部连接。

 AND fca.currency_code(+) = pct.acct_currency_code
  AND fcr.currency_code( +) = pct.receipt_currency_code
  AND fcp.currency_code(+) = pct.project_currency_code

以上 3 行只能使用fnd_currencies 表处理一次。有没有更聪明的方法来做到这一点?

【问题讨论】:

  • 不,至少不容易。但是,您的代码应该使用 LEFT JOIN 而不是过时的外连接语法。
  • 当然..谢谢戈登。
  • 可能使用 OR 但取决于完整的查询。
  • @CetinBasoz 查询非常复杂。所以我只放了这一部分。有什么解决方案可以查看我放置的部分吗?
  • 尝试使用 OR。您有原始查询,删除额外的别名可能会产生副作用。

标签: sql database oracle plsql oracle11g


【解决方案1】:

您可以使用子查询分解子句确保您只查询一次fnd_currencies。看起来像这样(并通过使用 ANSI 92 语法让 @gordonlinoff 满意):

with ccy as ( select * 
              from fnd_currencies )
select fca.descr as acct_currency
      ,fcr.descr as receipt_currency
      ,fcp.descr as project_currency
      ,pct.*
from pa_commitment_txns pct
left outer join ccy     fca on fca.currency_code = pct.acct_currency_code
left outer join ccy     fcr on fcr.currency_code = pct.receipt_currency_code
left outer join ccy     fcp on fcp.currency_code = pct.project_currency_code

这是否真的会缩短执行时间取决于您尚未向我们保证的数据详细信息。

【讨论】:

  • 很好的答案。有一些想法我该如何重写查询。非常感谢 APC。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
相关资源
最近更新 更多