在 Oracle 中多次加入相同的 table

joining same table multiple times in Oracle

嗨,我是 tuning 一个遗留代码。我们在一个大查询中有不到 2 tables。

  fnd_currencies, and pa_commitment_txns

我从子句中看到了

  fnd_currencies fca,
  fnd_currencies fcr,
  fnd_currencies fcp,
  pa_commitment_txns pct
 

第一个 table 已被使用三次,并且已与相同的 column 和相同的 table.

进行了外部连接
 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 table 处理一次吗?有没有更聪明的方法来做到这一点?

您可以通过使用子查询分解子句确保只查询 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

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