현재 실행하는 데 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`
죄송합니다. 어떤 도움이라도 대단히 감사 할 것입니다. Select Count (*)에서 하나의 열만 계산하려고 시도했지만 0 차이를 만들었습니다.
모든 조건, 그 다음에는 'ELSE'이외의 다른 데이터는 정확히 같은 데이터를 삽입합니다. 'A.inbAcctgID, A.chrCarrierDesc, 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 ' 뭔가 빠졌어? – Serpiton
이것은 쿼리가 아니며 집합 기반 환경에서 절차 적 코드입니다. 마찬가지로 : SLOW. 그것을 기반으로 설정하고 행복하게. 실행 계획을 확인하고 필요한 모든 관련 지표를 추가하십시오. 끝마친. 임시 테이블이 필요 없습니다. – TomTom
"make it set based"란 무엇을 의미합니까? – Zingo