2014-09-20 71 views
-1

我有以下查詢優化查詢包含了太多的內部聯接和行

SELECT * 
INTO ##TempStaffT 
FROM Staff HF 
WHERE HF.[businessline_id]='T' 
AND HF.[offices_id] IN (SELECT * FROM ##TempParamOffice) 
AND HF.[specialism_id]IN (SELECT * FROM ##TempParamSpecialism) 
AND HF.[onetouch]IN (SELECT * FROM ##TempParamConsultant) 

SET @sql4 = N' 

INSERT INTO ##TempFees(' + @columns1 + ',CALF_AN ,BusinessName) 
SELECT distinct ' + @columns2 + ',p.CALF_AN ,CB.[TempBusinessName] as BusinessName 
    FROM 
    dbo.WTEFAC EF 
    inner JOIN dbo.WTFAC F ON EF.EFAC_NUM = F.EFAC_NUM 
    inner JOIN dbo.WTFACINFO BS ON F.FAC_NUM = BS.FAC_NUM 
    inner JOIN dbo.WTLFAC LF ON F.FAC_NUM = LF.FAC_NUM 
    inner JOIN dbo.WTRUBVARIANTEFAC WRU ON LF.RINT_ID = WRU.RINT_ID 
    inner JOIN dbo.WTACUMFAC WTA ON WRU.RUV_ID = WTA.RUV_ID 
    inner JOIN ##CUM_CODEHT WTA1 ON WTA.CUM_ID = WTA1.CUM_ID 
    inner JOIN dbo.WTVTAT TAT ON BS.TIE_ID = TAT.TIE_ID AND BS.RFAN_ID = TAT.RFAN_ID AND BS.PER_ID = TAT.PER_ID AND BS.CNT_ID = TAT.CNT_ID 
    inner JOIN dbo.PYCONTRAT CC ON TAT.PER_ID = CC.PER_ID AND TAT.CNT_ID = CC.CNT_ID 
    inner JOIN dbo.WTMISS M ON CC.PER_ID = M.PER_ID AND CC.CNT_ID = M.CNT_ID 
    inner JOIN dbo.WTCNTI COT1 ON M.PER_ID = COT1.PER_ID AND M.CNT_ID = COT1.CNT_ID 
    inner JOIN dbo.WTQUAEU Q ON COT1.TIE_ID = Q.TIE_ID AND COT1.QEU_CDE = Q.QEU_CDE 
    inner JOIN dbo.WTSCCT C ON CC.RGPCNT_ID = C.RGPCNT_ID AND CC.PER_ID = C.PER_ID AND CC.CNT_ID = C.CNT_ID --AND''SECT3'' = C.STTQ_COD 
    INNER JOIN ##TempStaffT HF ON C.VAPO_CODE = HF.onetouch COLLATE Latin1_General_CI_AS 
    inner JOIN 
##TempA AS p ON p.CNT_ID = COT1.CNT_ID inner JOIN 
##TempB AS p1 ON p1.CNT_ID = COT1.CNT_ID 
    CROSS JOIN [dbo].[CustBusinessTable] CB 
WHERE CB.[TempBusinessName]=''Pure Temp Fees'' 

GROUP BY p.CALF_AN,CB.[TempBusinessName] 
' 
; 
PRINT @sql4; 

我的問題是,上面的查詢用了20分鐘,因爲## TempStaffT包含太多列執行,如何優化它呢?我使用很多臨時表,但似乎沒有工作,任何想法?提前謝謝了 。

+0

您可能需要添加更多的細節,比如有多少行是在'## TempStaffT'什麼執行計劃說是最昂貴的部件創建索引。 – 2014-09-20 20:22:53

回答

1

##TempStaffT

CREATE INDEX IDX_TempStaffT_onetouch ON ##TempStaffT(onetouch)