如何存储 select 语句的结果并能够在另一个 select 语句中再次使用它 PL\SQL Oracle
Posted
技术标签:
【中文标题】如何存储 select 语句的结果并能够在另一个 select 语句中再次使用它 PL\\SQL Oracle【英文标题】:How to store the results of a select statement and be able to use it again in another select statement PL\SQL Oracle如何存储 select 语句的结果并能够在另一个 select 语句中再次使用它 PL\SQL Oracle 【发布时间】:2014-08-22 09:46:24 【问题描述】:我正在尝试存储 select 语句的结果(可能在临时内存中?),然后调用保存的结果并能够在其他选择中再次使用它,因此它不必一次又一次地执行,这将增加执行时间。我在想也许使用过程或游标会是解决方案,但我知道足够的 SQL 能够使用它,任何帮助都会有所帮助!
这是我要存储的选择语句:
select "PO Number2", "Total"
from (select distinct "PO Number2", sum("Amount") as "Total"
from (select distinct account as Account,
company as Company,
year_period_key as "Year Period Key",
voucher_no as "Voucher No",
voucher_type as "Vou Type",
voucher_date as "GRN/Invoice Date",
reference_number as "PO Number2",
code_c as "Project ID",
ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(reference_number) as "Supplier ID",
ifsapp.SUPPLIER_INFO_API.Get_Name(ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(reference_number)) as "Supplier Name",
sum(quantity) as "Quantity",
sum(amount) as "Amount",
sum(debet_amount) as "Debit Amount",
sum(credit_amount) as "Credit Amount"
from ifsapp.GEN_LED_VOUCHER_ROW_UNION_QRY
where account = '222010'
and Voucher_Type = '0'
group by voucher_type,
account,
year_period_key,
voucher_date,
reference_number,
code_c,
company,
voucher_no,
party_type_id
union all
select distinct account as Account,
company as Company,
year_period_key as "Year Period Key",
voucher_no as "Voucher No",
voucher_type as "Vou Type",
voucher_date as "GRN/Invoice Date",
ifsapp.MAN_SUPP_INVOICE_API.Get_Purchase_Order_Ref_Number(company,
party_type_id,
'Supplier',
reference_number) as "PO Number2",
code_c as "Project ID",
ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(ifsapp.MAN_SUPP_INVOICE_API.Get_Purchase_Order_Ref_Number(company,
party_type_id,
'Supplier',
reference_number)) as "Supplier ID",
ifsapp.SUPPLIER_INFO_API.Get_Name(ifsapp.PURCHASE_ORDER_API.Get_Vendor_No(ifsapp.MAN_SUPP_INVOICE_API.Get_Purchase_Order_Ref_Number(company,
party_type_id,
'Supplier',
reference_number))) as "Supplier Name",
sum(quantity) as "Quantity",
sum(amount) as "Amount",
sum(debet_amount) as "Debit Amount",
sum(credit_amount) as "Credit Amount"
from ifsapp.GEN_LED_VOUCHER_ROW_UNION_QRY
where account = '222010'
and Voucher_Type = 'J'
group by voucher_type,
account,
year_period_key,
voucher_date,
reference_number,
code_c,
company,
voucher_no,
party_type_id)
where "Year Period Key" <= '&YearPeriodKey'
group by "PO Number2")
where "Total" = 0
提前致谢! :)
【问题讨论】:
查看物化视图 使用提示/*+ RESULT_CACHE */
请注意:结果缓存仅适用于 Oracle 企业版
我建议你看看确定性函数
@tbone - ...或全局临时表...
【参考方案1】:
Oracle 拥有自己的优化器,可帮助您避免多次执行相同的查询。如果查询完全相同,它将结果存储在服务器内存(有限)上。因此,如果您在一分钟内执行此查询 5 次,它不会消耗很多资源。提前,你应该看看物化视图是否不能解决你的问题。
【讨论】:
【参考方案2】:您是要“存储”结果并在单个会话中重复使用,还是要存储结果以供其他会话重复使用。要物化查询数据,使其对其他会话可见,并一直保持到调用刷新(手动或调度)时,那么您实际上是在查看物化视图。如果这只是为了单个会话/作业的目的,并且避免重复重新查询数据,请记住,使用 ref-cursors 你只能前进。您可以将数据写入 PLSQL 表(数组/集合),但如果数据集很大,请注意内存。对于较大的数据集,最好的选择可能是临时表。
【讨论】:
以上是关于如何存储 select 语句的结果并能够在另一个 select 语句中再次使用它 PL\SQL Oracle的主要内容,如果未能解决你的问题,请参考以下文章