以下存储过程运行4小时。无论如何我可以优化它以将执行时间降低到10分钟以下吗?
表#TbTemp有150k行。光标循环150k次。
由于我处理Temp表,我将完全控制创建任何索引-etc。
由于应该避免游标,任何替代解决方案?
declare tmpCur cursor
for
select LnNo,instDrftCreatnDt
from #TbTemp
for read only
open tmpCur
while (1 = 1)
begin --{
fetch tmpCur into @LnNo,@Dt
insert into #TmpLnPmt
(RecTyp,InstNo,LnNo,TotCurChrgdFactr,PmtTyp,
CurrChrgdFactr,CurrPmtAmt,PrevCmptdPmtAmt,NetCmptdPmtAmt)
select '01', @InstNo, @LnNo, @TotCurChrgdFactr, pc.PmtTyp,
0, 0, pc.cmptdPmtAmt, 0-pc.cmptdPmtAmt
from Ln l, Pmt pt, PmtCmpnt pc
where l.LnNo = @LnNo
and pt.mbsLoanPID = l.identifier
and pt.instDrftCreatnDt = @Dt
and pc.paymentComponentsPaymentPID = pt.identifier
and pc.PmtTyp not in (select PmtTyp
from #TbTemp
where LnNo = @LnNo)
and not exists (select 1 from #TmpLnPmt
where LnNo = @LnNo
and PmtTyp = pc.PmtTyp)
end --}
close tmpCur
deallocate cursor tmpCur
答案 0 :(得分:0)
以下是不使用CURSOR
INSERT INTO #tmplnpmt
(rectyp,
instno,
lnno,
totcurchrgdfactr,
pmttyp,
currchrgdfactr,
currpmtamt,
prevcmptdpmtamt,
netcmptdpmtamt)
SELECT '01',
@InstNo,
a.lnno,
@TotCurChrgdFactr,
pc.pmttyp,
0,
0,
pc.cmptdpmtamt,
0 - pc.cmptdpmtamt
FROM ln l
INNER JOIN pmt pt
ON pt.mbsloanpid = l.identifier
INNER JOIN pmtcmpnt pc
ON pc.paymentcomponentspaymentpid = pt.identifier
INNER JOIN #tbtemp a
ON l.lnno = a.lnno
AND pt.instdrftcreatndt = a.instdrftcreatndt
WHERE NOT EXISTS (SELECT pmttyp
FROM #tbtemp t
WHERE t.pmttyp = pc.pmttyp)
AND NOT EXISTS (SELECT 1
FROM #tmplnpmt a
INNER JOIN #tbtemp b
ON a.lnno = b.lnno
AND pmttyp = pc.pmttyp)