背景:
要做一个birt报表,数据库是oracle10g,但数据源是从多个数据表中得到的,所以只能写存储过程,把数据整合到一个临时表中去。完成的存储过程大致如下(只保留主要代码):
[sql] view plaincopycreateor replace procedure Rpt_Dyhgl_year(
rptyear in varchar2, --年yyyy
--rpttype in varchar2, --报表类型,1年报,2季报
ref_cursor out sys_refcursor --返回游标
)
As
-- 变量声明
。。。。。
begin
delete from YWTJ_RPT_DYHGL_YEAR where RPTYEAR = rptyear and RPTTYPE = '1';
commit;
--查询原始数据
v_sql := 'select rptdate, jcdlb, jcdlbName, sum(nvl(runtime,0)) sumruntime,sum(nvl(buhegeTime,0)) buhegeTime, count(jcdcount) jcdcount,
sum(nvl(tongbiRuntime,0)) tongbiRuntime, sum(nvl(tongbiBhg,0))tongbiBhg from (
select rptdate, runtime,r.chaoshangxian+r.chaoxiaxian buhegeTime, r.objid, 1 jcdcount,
(select runtime fromywtj_rpt_dyhgl_month tb whereto_char(add_months(to_date(tb.rptdate,''yyyymm''),12),''yyyymm'') = r.rptdateand tb.objid = r.objid) tongbiRuntime,
(select chaoshangxian + chaoxiaxian fromywtj_rpt_dyhgl_month tb whereto_char(add_months(to_date(tb.rptdate,''yyyymm''),12),''yyyymm'') = r.rptdateand tb.objid = r.objid) tongbiBhg,
(select jcdlb from jczl_dwjg d whered.objid = r.objid ) jcdlb,
(select jcdlbname from dic_jcdlb wherejcdlbid = (select jcdlb from jczl_dwjg where objid = r.objid)) jcdlbName
from ywtj_rpt_dyhgl_month r where rptdate = :rptYear)
group by rptdate, jcdlb, jcdlbName
order by rptdate ,jcdlb ';
-- 插入新数据
v_sqlRptInsert := 'Insert IntoYWTJ_RPT_DYHGL_YEAR (RPTYEAR,RPTDATE,RPTTYPE) values(:RPTYEAR,:RPTDATE,:RPTTYPE)';
v_sqlRptUpdate := '';
-- 计算1--12月的当月数据
for rptmonth in 1..12 loop
execute immediate v_sqlRptInsertusing rptyear, rptmonth, '1';
commit;
open v_cur for v_sql using rptyear ||LPAD(rptmonth, 2, '0');
loop
fetch v_cur intov_rptDate,v_jcdlb,v_jcdName,v_sumRuntime,v_sumbuhegeTime,v_jcdCount,v_tongbiRuntime,v_tongbiBhg;
exit when v_cur%notfound;
--计算合格率
。。。。。。
-- 更新当月数据
。。。。。。
end loop;
close v_cur;
end loop;
-- 更新累计数据
。。。。。。
commit;
-- 计算综合合格率
open ref_cursor for select * fromYWTJ_RPT_DYHGL_YEAR r where r.rptyear = rptyear and r.rpttype = 1 order byr.rptdate;
end Rpt_Dyhgl_year;
功能运行正常,查询时间大约30-40秒。
然后,把报表部署到运行环境中后,验证功能时,发现2、3 分钟报表没有出来!多次尝试,后来终于出来结果了:940多秒,15分钟多!这样用户肯定是不能接受的!使用pl/sql dev的性能分析对Rpt_Dyhgl_year的执行过程进行分析,发现几乎所有的时间都化在了
fetch v_cur into v_rptDate,v_jcdlb,v_jcdName,v_sumRuntime,v_sumbuhegeTime,v_jcdCount,v_tongbiRuntime,v_tongbiBhg;
(期间把过程转成程序执行,性能分析时发现,主要时间都是由dr.Read()占用,和对过程的分析一致。)
那问题应该是在fetch对应的查询上,把
[sql] view plaincopyv_sql :='select rptdate, jcdlb, jcdlbName,sum(nvl(runtime,0)) sumruntime, sum(nvl(buhegeTime,0)) buhegeTime,count(jcdcount) jcdcount,
sum(nvl(tongbiRuntime,0)) tongbiRuntime, sum(nvl(tongbiBhg,0))tongbiBhg from (
select rptdate, runtime,r.chaoshangxian+r.chaoxiaxian buhegeTime, r.objid, 1 jcdcount,
(select runtime fromywtj_rpt_dyhgl_month tb whereto_char(add_months(to_date(tb.rptdate,''yyyymm''),12),''yyyymm'') = r.rptdateand tb.objid = r.objid) tongbiRuntime,
(select chaoshangxian + chaoxiaxian fromywtj_rpt_dyhgl_month tb whereto_char(add_months(to_date(tb.rptdate,''yyyymm''),12),''yyyymm'') = r.rptdateand tb.objid = r.objid) tongbiBhg,
(select jcdlb from jczl_dwjg d whered.objid = r.objid ) jcdlb,
(select jcdlbname from dic_jcdlb wherejcdlbid = (select jcdlb from jczl_dwjg where objid = r.objid)) jcdlbName
from ywtj_rpt_dyhgl_month r where rptdate = :rptYear)
group by rptdate, jcdlb, jcdlbName
order by rptdate ,jcdlb '; ①
单独拿出来执行,大约5.7秒多。
再把其中的子查询:
[sql] view plaincopyselectrptdate, runtime, r.chaoshangxian+r.chaoxiaxian buhegeTime, r.objid, 1jcdcount,
(select runtime fromywtj_rpt_dyhgl_month tb whereto_char(add_months(to_date(tb.rptdate,''yyyymm''),12),''yyyymm'') = r.rptdateand tb.objid = r.objid) tongbiRuntime,