2014-06-12 56 views
0

我有一個存儲過程,當前需要4分鐘才能運行。我試圖把它降到大約2分鐘。查詢基本上查看大約25,000行的表。它抓取每個項目,並嘗試找到與約230行的表匹配。使多個連接更好地執行SQ​​L查詢

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` 

對不起長度。任何幫助將非常感激。我試過從選擇計數(*)計算一列,但它沒有差異。

+0

在我看來,除了'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

+1

這不是一個查詢 - 這是在基於集合的環境中的程序代碼。如:SLOW。讓它基於設置並對此感到滿意。檢查執行計劃並添加所有相關的所需索引。成品。而且不需要臨時表。 – TomTom

+0

你是什麼意思「讓它設置爲基礎」? – Zingo

回答

1

確實很難說出什麼是錯誤的,但首先你應該檢查你在where子句和ON子句中使用的表中字段的索引。 我也看到這樣的語法,如:

if(select count(1) from t where something= something) = 1 
begin 
    insert into A from t where something= something 
end 

這是不好的做法。使用就像這樣:

begin tran 
     insert into A from t where something= something 
if @@rowcount > 1 -- this is you requirement that needs just one row needs instead of if(select count(1) from t where something= something) = 1 
    rollback tran 
else 
    commit tran 

在這種情況下,你將只有一個select而不是兩個。有很多這樣的情況,我的事情你會得到馬麗娟性能,如果改變它仔細

此外,還可以包括客戶端統計(I/O和時間)

set statistics time on 
go 
set statistics io on 
go 

,你會看到什麼是完全慢的查詢工作的一部分。

+0

我現在就試試這個 – Zingo

+0

這種方式到目前爲止需要較長的時間 – Zingo

+0

你真的檢查索引是否存在?你能展示一部分客戶統計數據嗎? –

0

IF結構相當於

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 

沒有需要檢查每列列如果chrPgmCode檢查是足夠多的數據傳遞給@tempTable

這意味着,如果tblActgPayeeTransactionAccounts2tblActgPayeeTransactionAccounts 具有相同的數據,這相當於

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 

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 NOT EXISTS (SELECT 1 
        FROM dbo.tblActgPayeeTransactionAccounts as A 
          INNER JOIN @tempView as V ON A.chrPgmCode = V.chrPgmCode 
       ) 

沒有任何遊標或迭代器

+0

等於JUST chrPgmCode ISNT足以將它傳遞給臨時表,它的最後手段 – Zingo

+0

我跑了這一點,它花了大約一分鐘,並返回了我的第一個表 – Zingo

+0

@Zingo沒有測試數據很難說爲什麼我可以知道'tblActgPayeeTransactionAccounts'中的所有行都與已過濾的viewMonthendArchiveGroupByCovg中的行匹配,如果您可以創建一個SQLFiddle,其中的數據匹配且不匹配,我會檢查它 – Serpiton