【发布时间】:2012-09-17 17:57:30
【问题描述】:
我是甲骨文的新手。 我正在尝试创建一个具有多种功能的包。 这是我想做的伪代码
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 是一个非常大的表,我只能从表中获得每条记录的 1 个条目。
我想在包中创建一个临时表(TempTableA,TempTableB),它将根据 startdate 和 enddate 临时存储所有记录,这样当我尝试检索每个记录的 TransactionDate 和 Amount 时,我只会参考 TempTables(比 TableA 和 TableB 小)。
如果在 TableA 和 TableB 中找不到 UserID,我也想考虑一下。所以基本上,当在TableA和TableB中都没有找到记录时,我也想要输出中的条目,但是表明用户处于非活动状态。
感谢您的所有帮助。
【问题讨论】:
-
为什么不优化每张表使用的SQL呢?如果您在每个选择中仅获得一行,则意味着您的用户 ID 是唯一的。您在该列上有(唯一)索引吗?如果没有,您应该添加一个(甚至是主键)。基于唯一索引检索单行将非常快(并且几乎与表的大小无关)
-
您的附加伪代码坦率地说是垃圾。这是无法编译的大杂烩。如果您希望有人为您编写代码,您应该发布您的实际完整需求,而不是让人们从一些笨拙的 PL/SQL 中猜测它们。当然,SO 上的受访者的角色不是为你做你的工作,但也许有人会觉得特别慷慨。
标签: performance oracle packages