我有一個存儲過程,當前需要4分鐘才能運行。我試圖把它降到大約2分鐘。查詢基本上查看大約25,000行的表。它抓取每個項目,並嘗試找到與約230行的表匹配。使多個連接更好地執行SQL查詢
SET NOCOUNT ON;
DECLARE @Iterator INT
DECLARE @RowCount INT
DECLARE @tempView TABLE (viewID int identity,
intCarrierCode int,
chrDlrNum varchar(50),
chrPgmCode varchar(5),
chrCvgCode varchar(5),
chrTransType varchar(5),
chrFeeType varchar(5),
chrPayeeType varchar(5),
intPayeeCode int,
Count int,
Amount smallmoney);
DECLARE @tempTable TABLE (tableID int identity,
inbAcctgID int,
chrCarrierDesc varchar(50),
chrCarrierDescSht varchar(50),
intAcctCustID int,
intAcctCo int,
intAcctCoRec int,
chrPgmCode varchar(5),
intCarrierCode int,
chrDlrNum varchar(50),
chrCvgCode varchar(5),
chrPayeeType varchar(5),
chrFeeType varchar(5),
intPayeeCode int,
chrTransType varchar(5),
Count int,
Amount smallmoney)
Truncate Table tblPayeeTransactionsMatchMonthEnd
INSERT into @tempView (intCarrierCode,chrDlrNum,chrPgmCode,chrCvgCode,chrTransType,chrFeeType,chrPayeeType,intPay eeCode,Count,Amount)
SELECT * FROM dbo.viewMonthendArchiveGroupByCovg Where chrPgmCode <> 'BIW' and chrPayeeType <> 'M' and chrTransType Not in ('CXS','VOD','SUS','DEN') and Amount <> 0
SET @Iterator = 1
SET @RowCount = (SELECT COUNT(*) FROM @tempView)
WHILE (@Iterator <= @RowCount)
BEGIN
IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and (A.intPayeeCode = V.intPayeeCode OR A.intPayeeCode is NULL)
and (A.chrCvgCode = V.chrCvgCode OR A.chrCvgCode is NULL)
and (A.chrDlrNum = V.chrDlrNum OR A.chrDlrNum is NULL)
and (A.chrTransType = V.chrTransType OR A.chrTransType IS NULL)
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and (A.intPayeeCode = V.intPayeeCode OR A.intPayeeCode is NULL)
and (A.chrCvgCode = V.chrCvgCode OR A.chrCvgCode is NULL)
and (A.chrDlrNum = V.chrDlrNum OR A.chrDlrNum is NULL)
and (A.chrTransType = V.chrTransType OR A.chrTransType IS NULL)
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and (A.intPayeeCode = V.intPayeeCode OR A.intPayeeCode is NULL)
and (A.chrCvgCode = V.chrCvgCode OR A.chrCvgCode is NULL)
and (A.chrDlrNum = V.chrDlrNum OR A.chrDlrNum is NULL)
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and (A.intPayeeCode = V.intPayeeCode OR A.intPayeeCode is NULL)
and (A.chrCvgCode = V.chrCvgCode OR A.chrCvgCode is NULL)
and (A.chrDlrNum = V.chrDlrNum OR A.chrDlrNum is NULL)
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and (A.intPayeeCode = V.intPayeeCode OR A.intPayeeCode is NULL)
and (A.chrCvgCode = V.chrCvgCode OR A.chrCvgCode is NULL)
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and (A.intPayeeCode = V.intPayeeCode OR A.intPayeeCode is NULL)
and (A.chrCvgCode = V.chrCvgCode OR A.chrCvgCode is NULL)
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and A.intPayeeCode = V.intPayeeCode
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and (A.intPayeeCode = V.intPayeeCode)
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and A.intPayeeCode is NULL
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
and A.intPayeeCode is NULL
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
and A.chrFeeType = V.chrFeeType
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
and A.chrPayeeType = V.chrPayeeType
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
and A.intCarrierCode = V.intCarrierCode
where V.viewID = @Iterator)
END
ELSE IF (Select COUNT(intActgAcctID)
From dbo.tblActgPayeeTransactionAccounts2 as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
where V.viewID = @Iterator) = 1
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
From dbo.tblActgPayeeTransactionAccounts as A INNER JOIN @tempView as V
ON A.chrPgmCode = V.chrPgmCode
where V.viewID = @Iterator)
END
ELSE
BEGIN
INSERT INTO @tempTable (inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
(SELECT NULL,NULL,NULL,NULL,NULL,NULL,V.chrPgmCode,V.intCarrierCode, V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount
FROM @tempView as V
where V.viewID = @Iterator)
END
Set @Iterator = @Iterator + 1
END
Begin
Insert into tblPayeeTransactionsMatchMonthEnd(inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount)
Select inbAcctgID,chrCarrierDesc,chrCarrierDescSht,intAcctCustID,intAcctCo,intAcctCoRec,chrPgmCode,intCarrierCode,chrDlrNum,chrCvgCode,chrPayeeType,chrFeeType,intPayeeCode,chrTransType,Count,Amount from @tempTable
End`
對不起長度。任何幫助將非常感激。我試過從選擇計數(*)計算一列,但它沒有差異。
在我看來,除了'ELSE'之外,每個條件都插入完全相同的數據'Select A.inbAcctgID,A.chrCarrierDesc,A.chrCarrierDescSht,A.intAcctCustID,A.intAcctCo ,A.intAcctCoRec,A.chrPgmCode,A.intCarrierCode,V.chrDlrNum,V.chrCvgCode,V.chrPayeeType,V.chrFeeType,V.intPayeeCode,V.chrTransType,V.Count,V.Amount'into @tempTable I'我錯過了什麼? – Serpiton
這不是一個查詢 - 這是在基於集合的環境中的程序代碼。如:SLOW。讓它基於設置並對此感到滿意。檢查執行計劃並添加所有相關的所需索引。成品。而且不需要臨時表。 – TomTom
你是什麼意思「讓它設置爲基礎」? – Zingo