Performance 包中的临时表-Oracle

Performance 包中的临时表-Oracle,performance,oracle,packages,Performance,Oracle,Packages,我对甲骨文有点陌生。 我正在尝试创建一个包含多个函数的包。 这是我想做的伪代码 function FunctionA(UserID, startdate, enddate) /* Select TransactionDate, Amount from TableA where TransactionDate between startdate and enddate and TableA.UserID = UserID */ Return Transacti

我对甲骨文有点陌生。 我正在尝试创建一个包含多个函数的包。 这是我想做的伪代码

function FunctionA(UserID, startdate, enddate)
  /* Select TransactionDate, Amount
     from TableA
     where TransactionDate between startdate and enddate
     and TableA.UserID = UserID */
  Return TransactionDate, Amount
end FunctionA

function FunctionB(UserID, startdate, enddate)
  /* Select TransactionDate, Amount
     from TableB
     where TransactionDate between startdate and enddate
     and TableB.UserID = UserID */
  Return TransactionDate, Amount
end FunctionA

TYPE TRANSACTION_REC IS RECORD(
          TransactionDate    DATE,
          TransactionAmt     NUMBER);

function MainFunction(startdate, enddate)
  return TBL
  is
  vTrans TRANSACTION_REC;
begin
  FOR rec IN
    ( Select UserID, UserName, UserStatus
      from UserTable
      where EntryDate between startdate and enddate )
  LOOP
    vTrans := FunctionA(rec.UserID, startdate, enddate)

    if vTrans.TransactionDate is null then
       vTrans := FunctionB(rec.UserID, startdate, enddate)

       if vTrans.TransactionDate is null then
           rec.UserStatus := 'Inactive'
       endif;
    endif;
  END Loop;

  PIPE ROW(USER_OBJ_TYPE(rec.UserID,
                       rec.UserName,
                       rec.UserStatus,
                       vTrans.TransactionDate,
                       vTtans.TransactionAmt));
end MainFunction
运行这类代码需要很长时间,因为TableA和TableB是一个非常大的表,我从表中只得到每条记录的一个条目

我希望在包中创建一个临时表(EntertableA,EntertableB),该表将临时存储基于startdate和enddate的所有记录,这样当我尝试检索每个rec的TransactionDate和Amount时,我将只引用Entertables(比TableA和TableB小)

我还想考虑是否在TableA和TableB中找不到用户ID。因此,基本上,当在TableA和TableB中没有找到记录时,我也希望在输出中找到条目,但它表明用户处于非活动状态


谢谢您的帮助。

一次查询可能更好,例如:

Select UserTable.UserID, UserTable.UserName, UserTable.UserStatus
      ,TableA.TransactionDate AS ATransactionDate
      ,TableA.Amount          AS AAmount
      ,TableB.TransactionDate AS BTransactionDate
      ,TableB.Amount          AS BAmount
from UserTable
left join TableA
  on (UserTable.UserID = TableA.UserID)
left join TableB
  on (UserTable.UserID = TableB.UserID)
where UserTable.EntryDate between startdate and enddate

SQL是一种基于集合的语言。执行一条语句返回所需的所有行要比执行多条语句(每条语句返回一行)有效得多

下面是一种同时获取所有行的方法。它使用一个公共表表达式,因为您读取整个UserTable,并且应该只读取一次

with cte as 
  (select UserID
         , UserStatus
   from UserTable )
select cte.UserID
       , cte.UserStatus
       , TableA.TransactionDate 
       , TableA.Amount  
from cte join TableA   
     on (cte.UserID = TableA.UserID)
where cte.UserStatus = 'A'
and TableA.TransactionDate between startdate and enddate
union
select cte.UserID
       , cte.UserStatus
       , TableB.TransactionDate 
       , TableB.Amount  
from cte join TableB  
     on (cte.UserID = TableB.UserID)
where cte.UserStatus != 'A'
and TableB.TransactionDate between startdate and enddate

顺便说一下,要小心临时桌子。它们不像t-SQL中的临时表。它们是永久堆表,只是它们的数据是临时的。这意味着填充临时表是一个昂贵的过程,因为数据库会将所有这些行写入磁盘。因此,我们需要确保从临时表中读取数据集所获得的性能增益值得所有这些写入的开销


你的代码肯定不是这样的。事实上,性能问题的答案是“使用全局临时表”的情况非常罕见,至少在Oracle中不是这样。更好的查询是前进的方向,尤其是拥抱集合的乐趣

为什么不优化每个表使用的SQL?如果您在每个select中只得到一行,那么这意味着您的userid是唯一的。该列上有(唯一)索引吗?如果没有,您应该添加一个(甚至一个主键)。基于唯一索引检索单行将非常快(并且几乎与表的大小无关),您额外的伪代码显然是垃圾。这是一堆无法编译的东西。如果您希望有人为您编写代码,那么您应该发布实际的完整需求,而不是让人们从一些拙劣的PL/SQL中猜测它们。当然,为你工作不是受访者的职责,但也许有人会觉得特别慷慨。谢谢@APC。我很感激你的帮助:不过我还有另一件事要考虑。我忘了考虑在表A和表B中找不到记录的情况(请参阅我问题中更新的伪代码),抱歉造成混淆。