我有一個存儲過程使用while循環臨時表和光標,通過它我得到了客戶的老化平衡,但是我的SP工作正常,但我有一些性能問題,因爲它需要15秒從一小塊數據產生結果。我正在尋找一種更有效的方式來做到這一點。存儲過程的性能問題
在此先感謝。
這是我的存儲過程。
CREATE TABLE #Customer_Temp (
AccountCode varchar(50),
AccountTitle varchar(50),
CurrentBalance int,
FirstBalance int,
SecondBalance int,
ThirdBalance int,
FourthBalance int,
FifthBalance int,
SixthBalance int,
SeventhBalance int,
EighthBalance int,
OpeningBalance int
)
INSERT INTO #customer_temp (AccountCode, AccountTitle, OpeningBalance)
SELECT
Customer.AccountCode,
Customer.Name,
COA.OpeningBalance
FROM Customers AS Customer
INNER JOIN ChartOfAccount AS COA
ON COA.CompanyId = @Companyid
AND COA.BusinessUnitId = @BusinessUnitId
AND COA.ChartAccount = Customer.AccountCode
--Create Table And Duplicate Customers Data In it ENDED
DECLARE @DrAmount AS int
DECLARE @CrAmount AS int
DECLARE @Balance AS int
DECLARE @FBalance AS int
DECLARE @SBalance AS int
DECLARE @TBalance AS int
DECLARE @FoBalance AS int
DECLARE @FIBalance AS int
DECLARE @SIBalance AS int
DECLARE @SEBalance AS int
DECLARE @EBalance AS int
DECLARE @FSDate AS date
DECLARE @FLDate AS date
DECLARE @SSDate AS date
DECLARE @SLDate AS date
DECLARE @TSDate AS date
DECLARE @TLDate AS date
DECLARE @FOSDate AS date
DECLARE @FOLDate AS date
DECLARE @FISDate AS date
DECLARE @FILDate AS date
DECLARE @SISDate AS date
DECLARE @SILDate AS date
DECLARE @SESDate AS date
DECLARE @SELDate AS date
DECLARE @ESDate AS date
SET @FSDate = DATEADD(DAY, -1, @StartDate)
SET @FLDate = DATEADD(DAY, -6, @FSDate)
SET @SSDate = DATEADD(DAY, -1, @FLDate)
SET @SLDate = DATEADD(DAY, -6, @SSDate)
SET @TSDate = DATEADD(DAY, -1, @SLDate)
SET @TLDate = DATEADD(DAY, -14, @TSDate)
SET @FOSDate = DATEADD(DAY, -1, @TLDate)
SET @FOLDate = DATEADD(DAY, -14, @FOSDate)
SET @FISDate = DATEADD(DAY, -1, @FOLDate)
SET @FILDate = DATEADD(DAY, -14, @FISDate)
SET @SISDate = DATEADD(DAY, -1, @FILDate)
SET @SILDate = DATEADD(DAY, -29, @SISDate)
SET @SESDate = DATEADD(DAY, -1, @SILDate)
SET @SELDate = DATEADD(DAY, -89, @SESDate)
SET @ESDate = DATEADD(DAY, -1, @SELDate)
DECLARE @TempCCode AS varchar(50)
DECLARE @TempOBalance AS float
DECLARE CustomerCursor CURSOR FOR
SELECT
AccountCode,
OpeningBalance
FROM #Customer_Temp
OPEN CustomerCursor
FETCH NEXT FROM CustomerCursor INTO @TempCCode, @TempOBalance
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC @FBalance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@FSDate,
@FLDate,
@Fyear
EXEC @SBalance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@SSDate,
@SLDate,
@Fyear
EXEC @TBalance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@TSDate,
@TLDate,
@Fyear
EXEC @FoBalance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@FOSDate,
@FOLDate,
@Fyear
EXEC @FIBalance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@FISDate,
@FILDate,
@Fyear
EXEC @SIBalance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@SISDate,
@SILDate,
@Fyear
PRINT @SESDate
PRINT @SELDate
EXEC @SEBalance =
GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@SESDate,
@SELDate,
@Fyear
EXEC @EBalance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@ESDate,
@EndDate,
@Fyear
EXEC @Balance = GetBalanceOfAgingOnDate @BusinessUnitId,
@Companyid,
@TempCCode,
@StartDate,
@EndDate,
@Fyear
UPDATE #Customer_Temp
SET CurrentBalance = (@Balance + @TempOBalance),
FirstBalance = @FBalance,
SecondBalance = @SBalance,
ThirdBalance = @TBalance,
FourthBalance = @FoBalance,
FifthBalance = @FIBalance,
SixthBalance = @SIBalance,
SeventhBalance = @SEBalance,
EighthBalance = @EBalance
WHERE AccountCode = @TempCCode
FETCH NEXT FROM CustomerCursor INTO @TempCCode, @TempOBalance
END
CLOSE CustomerCursor
DEALLOCATE CustomerCursor
,這裏是在光標稱爲存儲過程
CREATE PROCEDURE [dbo].[GetBalanceOfAgingOnDate]
@BusinessUnitId int,
@Companyid int,
@ChartAccount as varchar (50),
@StartDate as DateTime,
@EndDate as DateTime,
@Fyear as varchar(50)
AS BEGIN
Declare @DrAmount as int
Declare @CrAmount as int
Declare @Balance as int
set @DrAmount=(select sum(Dr_Amount) from AccountVocherMaster AS AM ,
AccountVocherChild AS AC Where AM.CompanyId = @Companyid AND
AM.BusinessUnitId = @BusinessUnitId AND AM.FYear = @Fyear AND
AM.VocherId = AC.VocherId AND [email protected] AND
AC.CreatedOn Between @EndDate AND @StartDate);
set @CrAmount=(select sum(Cr_Amount) from AccountVocherMaster AS AM ,
AccountVocherChild AS AC Where AM.CompanyId = @Companyid AND
AM.BusinessUnitId = @BusinessUnitId AND AM.FYear = @Fyear AND
AM.VocherId = AC.VocherId AND [email protected] AND
AC.CreatedOn Between @EndDate AND @StartDate);
set @Balance = @DrAmount - @CrAmount ;
return ISNULL(@Balance,0)
END
這很明顯,擺脫了光標和程序。以基於集合的方式處理它,而不是基於行。 –
由於詹姆斯躲過了,循環和遊標通常很慢。此外,如果不知道這些存儲過程是如何寫入的,則不能排除這些存儲過程成爲瓶頸。也許你可以擴展你正在努力完成的任務,如果沒有這個背景,這可能是一個非常大的討論,涉及逐個拆開你的查詢。 – Nicarus
@Nicarus我已經添加了被調用的存儲過程中的問題,現在請幫助我 問候 – faizzyy