2017-06-16 55 views
0

下面的查詢運行7小時。任何人都可以幫助我優化查詢?Query Optimitize

select count(1) 
FROM  temp_raw_appointments trp 
left join temp_raw_transactions_sep tra 
on  case when tra.[rndrng prvdr] in (SELECT [rndrng prvdr] 
              FROM temp_raw_changetosupprov) 
        then tra.[sup prvdr] 
       when tra.[rndrng prvdr] = 'P003_BMI_WidermannJ' 
        then 'P003_WiedermannJ' -- Kim Chirayil's customization, 06/16 
       when tra.[rndrng prvdr] = 'P014_HeerenK' 
        then 'P014_HarrisonL' -- Kim Chirayil's customization, 06/16 
       else tra.[rndrng prvdr] end 
      =trp.[appt schdlng prvdr] 
where  tra.[rndrng prvdr] is null 
+0

你的執行計劃是什麼樣的?你可以使用'where not exist'而不是'where null'來幫助你 – dbajtr

+0

什麼是表格的鍵?你有什麼指標在桌子上? –

+1

你可以在這裏粘貼我們的查詢執行計劃嗎? https://www.brentozar.com/pastetheplan/ – Hybris95

回答

0

嘗試將查詢分爲兩個部分

CREATE TABLE #inter 
    (
    temp_col VARCHAR(500) 
) 

INSERT INTO #inter 
SELECT Distinct CASE 
     WHEN tra.[rndrng prvdr] IN (SELECT [rndrng prvdr] 
            FROM temp_raw_changetosupprov) THEN tra.[sup prvdr] 
     WHEN tra.[rndrng prvdr] = 'P003_BMI_WidermannJ' THEN 'P003_WiedermannJ' -- Kim Chirayil's customization, 06/16 
     WHEN tra.[rndrng prvdr] = 'P014_HeerenK' THEN 'P014_HarrisonL' -- Kim Chirayil's customization, 06/16 
     ELSE tra.[rndrng prvdr] 
     END AS temp_col 
FROM temp_raw_transactions_sep tra 

CREATE NONCLUSTERED INDEX nix_inter 
    ON #inter (temp_col) 

SELECT Count(1) 
FROM temp_raw_appointments trp 
WHERE NOT EXISTS (SELECT 1 
        FROM #inter 
        WHERE trp.[appt schdlng prvdr] = temp_col) 

因爲它是運行近7小時,我感到沒有料到的執行計劃。如果你有一個請提供進一步建議

+0

我正在檢查您的答案以及配置文件。你會在一般的聊天中解釋如何解決與「優化」有關的問題嗎? –

+1

@PrabhatG - 分割代碼不是正確的方法來優化,但我覺得這可能會有所作爲。但只有通過測試我們才能得出結論。優化包括很多因素。它從表格設計,索引,統計,連接的正確使用等開始。 –

0

試試這個:更新加入條件的邏輯。這可能有助於性能調整。

select count(1) 
FROM temp_raw_appointments trp 
left join temp_raw_transactions_sep tra on (tra.[sup prvdr] = trp.[appt schdlng prvdr] AND EXIST (SELECT 1 FROM temp_raw_changetosupprov [rndrng prvdr] =tra.[rndrng prvdr]) 
OR (trp.[appt schdlng prvdr] = tra.[rndrng prvdr] AND tra.[rndrng prvdr] IN ('P003_BMI_WidermannJ','P014_HeerenK')) 
OR (tra.[rndrng prvdr] = trp.[appt schdlng prvdr]) 
where tra.[rndrng prvdr] is null 
0

當然,這將是很好看的執行計劃,執行統計和行數,但是,考慮這個猜測爲可能的解決方案:

1)確保TRA [ rndrng prvdr](理想情況下在索引中包含列或第二個鍵),temp_raw_changetosupprov。[rndrng prvdr],trp。[appt schdlng prvdr]列被索引。 2)重寫查詢以提高效率。既然你只對LEFT JOIN後的IS NULL行感興趣,我猜以下查詢的邏輯將是正確的:

WITH SupPrvdr([sup prvdr]) 
AS 
(
    SELECT tra.[sup prvdr] FROM temp_raw_transactions_sep tra 
    WHERE tra.[rndrng prvdr] IN 
    (
     SELECT [rndrng prvdr] FROM temp_raw_changetosupprov 
     UNION 
     SELECT 'P003_WiedermannJ' AS [rndrng prvdr] 
     UNION 
     SELECT 'P014_HarrisonL'AS [rndrng prvdr] 
    ) 
) 
SELECT COUNT(*) FROM temp_raw_appointments trp 
WHERE trp.[appt schdlng prvdr] NOT IN (SELECT [sup prvdr] FROM SupPrvdr) 
    OR trp.[appt schdlng prvdr] IS NULL -- if trp.[appt schdlng prvdr] is nullable column this condition can be omitted