2017-03-15 76 views
1

我有兩個部分的問題。SQL Server 2014 - 多個SUM/COUNT與子查詢

第一部分是關於我在下面做的事情的速度。我正在採集一個數據集並獲取每個DISTINCT,然後加入一個子查詢,並與標準相加/重複,然後重複這五次。第一個LEFT JOIN後我有一個02秒的運行時間,在第二個LEFT JOIN後我有一個04秒的運行時間,但在第三個LEFT JOIN我的運行時間達到44秒。如果把這個查詢放在一邊並單獨運行,它只是01秒。完成的查詢的總運行時間爲70秒。

第一部分的問題:

什麼讓這個查詢在第三個顯着慢加入?

第二部分:我確信我正以低效的方式完成此任務。我環顧了一會兒,無法找到一種方法來以簡單的方式完成我正在做的事情,同時仍然可以爲每個子查詢制定不同的標準。

第二部分的問題:

是否有完成此查詢的更好的辦法?

編輯 user1413的評論後。

SELECT DISTINCT開始時在SQL Server執行計劃視圖中顯示了49%的成本。我改變了SELECT DISTINCT正在選擇的表格,我的運行時間降到了01秒以下。

SELECT DISTINCT 
    od.location, currentTires, currentAlignments, currentLubes, 
    currentBatteries,currentSiping,currentCarcount 
FROM 
    comm.dbo.ordetail as od 
LEFT JOIN 
    (SELECT 
     od2.location, SUM(od2.qty_shipped) as currentTires 
    FROM 
     comm.dbo.ordetail as od2 
    JOIN 
     comm.dbo.invmas as invmas2 ON invmas2.item_num = od2.item# 
    JOIN 
     comm.dbo.ordhrd as oh2 ON oh2.location = od2.location 
           AND oh2.inv_date = od2.inv_date 
           AND oh2.invoice# = od2.invoice# 
    WHERE 
     (oh2.type_of_rec = '01' OR oh2.type_of_rec = '02') 
     AND od2.inv_date >= '2017-02-01 00:00:00.000' 
     AND od2.inv_date <= '2017-02-28 00:00:00.000' 
     AND (invmas2.category = 'uhp' OR invmas2.category = 'tour' OR invmas2.category = 'mass' OR invmas2.category = 'suv' OR invmas2.category = 'ltat' OR invmas2.category = 'ltmt' OR invmas2.category = 'lthwy' OR invmas2.category = 'snow' OR invmas2.category = 'stdls') 
    GROUP BY 
     od2.location) as currentTires ON od.location = currentTires.location 
LEFT JOIN 
    (SELECT 
     od3.location, SUM(od3.qty_shipped) as currentAlignments 
    FROM 
     comm.dbo.ordetail as od3 
    JOIN 
     comm.dbo.invmas as invmas3 ON invmas3.item_num = od3.item# 
    JOIN 
     comm.dbo.ordhrd as oh3 ON oh3.location = od3.location 
           AND oh3.inv_date = od3.inv_date 
           AND oh3.invoice# = od3.invoice# 
    WHERE 
     (oh3.type_of_rec = '01' OR oh3.type_of_rec = '02') 
     AND od3.inv_date >= '2017-02-01 00:00:00.000' 
     AND od3.inv_date <= '2017-02-28 00:00:00.000' 
     AND (od3.item# = '8501' OR od3.item# = '8502') 
    GROUP BY 
     od3.location) as currentAlignments ON od.location = currentAlignments.location 
LEFT JOIN 
    (SELECT 
     od4.location, SUM(od4.qty_shipped) as currentLubes 
    FROM 
     comm.dbo.ordetail as od4 
    JOIN 
     comm.dbo.invmas as invmas4 ON invmas4.item_num = od4.item# 
    JOIN 
     comm.dbo.ordhrd as oh4 ON oh4.location = od4.location 
           AND oh4.inv_date = od4.inv_date 
           AND oh4.invoice# = od4.invoice# 
    WHERE 
     (oh4.type_of_rec = '01' OR oh4.type_of_rec = '02') 
     AND od4.inv_date >= '2017-02-01 00:00:00.000' 
     AND od4.inv_date <= '2017-02-28 00:00:00.000' 
     AND (od4.item# = '200fs' OR od4.item# = '200c' OR od4.item# = '200m' OR od4.item# = '200s') 
    GROUP BY 
     od4.location) as currentLubes ON od.location = currentLubes.location 
    LEFT JOIN (SELECT od5.location,SUM(od5.qty_shipped) as currentBatteries 
     FROM comm.dbo.ordetail as od5 
     JOIN comm.dbo.invmas as invmas5 
      ON invmas5.item_num = od5.item# 
     JOIN comm.dbo.ordhrd as oh5 
      ON oh5.location = od5.location 
      AND oh5.inv_date = od5.inv_date 
      AND oh5.invoice# = od5.invoice# 
     WHERE (oh5.type_of_rec = '01' OR oh5.type_of_rec = '02') 
      AND od5.inv_date >= '2017-02-01 00:00:00.000' 
      AND od5.inv_date <= '2017-02-28 00:00:00.000' 
      AND invmas5.manufact = 'inter' 
     GROUP BY od5.location) as currentBatteries 
     ON od.location = currentBatteries.location 
    LEFT JOIN (SELECT od6.location,SUM(od6.qty_shipped) as currentSiping 
     FROM comm.dbo.ordetail as od6 
     JOIN comm.dbo.invmas as invmas6 
      ON invmas6.item_num = od6.item# 
     JOIN comm.dbo.ordhrd as oh6 
      ON oh6.location = od6.location 
      AND oh6.inv_date = od6.inv_date 
      AND oh6.invoice# = od6.invoice# 
     WHERE (oh6.type_of_rec = '01' OR oh6.type_of_rec = '02') 
      AND od6.inv_date >= '2017-02-01 00:00:00.000' 
      AND od6.inv_date <= '2017-02-28 00:00:00.000' 
      AND invmas6.manufact = 'inter' 
     GROUP BY od6.location) as currentSiping 
     ON od.location = currentSiping.location 
    LEFT JOIN (SELECT od7.location,COUNT(DISTINCT oh7.invoice#) as currentCarcount 
     FROM comm.dbo.ordetail as od7 
     JOIN comm.dbo.ordhrd as oh7 
      ON oh7.location = od7.location 
      AND oh7.inv_date = od7.inv_date 
      AND oh7.invoice# = od7.invoice# 
     WHERE (oh7.type_of_rec = '01' OR oh7.type_of_rec = '02') 
      AND od7.inv_date >= '2017-02-01 00:00:00.000' 
      AND od7.inv_date <= '2017-02-28 00:00:00.000' 
      AND oh7.veh_make != '' 
      AND od7.item# != '' 
     GROUP BY od7.location) as currentCarcount 
     ON od.location = currentCarcount.location 
    ORDER BY od.location 

採樣數據輸出:

enter image description here

+1

您是否檢查過執行計劃? – user1413

+0

我剛剛做了,我不熟悉我在看什麼。不是一個簡單的方法來分享它。大量的數據。 XML輸出是5k行。我應該確定高成本的運營並找到減少運營的方法嗎? – Willee5586

+1

我不是那個意思。 SSMS有一個圖標。您甚至可以在查詢菜單 - >顯示估計執行計劃中找到它,這將有助於您根據顯示的百分比花費更多時間。 – user1413

回答

2

有很多的事情可以做更好地在這裏。對於初學者來說,以下連接中的一個可以被刪除,因爲od5和od6都返回相同的項目。

LEFT JOIN (SELECT od5.location,SUM(od5.qty_shipped) as currentBatteries 
    FROM comm.dbo.ordetail as od5 
    JOIN comm.dbo.invmas as invmas5 
     ON invmas5.item_num = od5.item# 
    JOIN comm.dbo.ordhrd as oh5 
     ON oh5.location = od5.location 
     AND oh5.inv_date = od5.inv_date 
     AND oh5.invoice# = od5.invoice# 
    WHERE (oh5.type_of_rec = '01' OR oh5.type_of_rec = '02') 
     AND od5.inv_date >= '2017-02-01 00:00:00.000' 
     AND od5.inv_date <= '2017-02-28 00:00:00.000' 
     AND invmas5.manufact = 'inter' 
    GROUP BY od5.location) as currentBatteries 
    ON od.location = currentBatteries.location 
LEFT JOIN (SELECT od6.location,SUM(od6.qty_shipped) as currentSiping 
    FROM comm.dbo.ordetail as od6 
    JOIN comm.dbo.invmas as invmas6 
     ON invmas6.item_num = od6.item# 
    JOIN comm.dbo.ordhrd as oh6 
     ON oh6.location = od6.location 
     AND oh6.inv_date = od6.inv_date 
     AND oh6.invoice# = od6.invoice# 
    WHERE (oh6.type_of_rec = '01' OR oh6.type_of_rec = '02') 
     AND od6.inv_date >= '2017-02-01 00:00:00.000' 
     AND od6.inv_date <= '2017-02-28 00:00:00.000' 
     AND invmas6.manufact = 'inter' 
    GROUP BY od6.location) as currentSiping 
    ON od.location = currentSiping.location 

至於性能和可讀性嘗試下面的東西。注意:我無法對此進行測試,但是,它可能會讓有更多時間調整查詢性能的人知道。

DECLARE @LowDate DATETIME = '2017-02-01 00:00:00.000' 
DECLARE @HighDate DATETIME = '2017-02-28 00:00:00.000' 


SELECT 
    DetailList.location, 
    currentTires = SUM(currentTires), 
    currentAlignments = SUM(currentAlignments), 
    currentLubes = SUM(currentLubes), 
    currentBatteries = SUM(currentBatteries), 
    currentSiping = SUM(currentSiping),   
    carCount = SUM(hasCar) 
FROM 
(
    SELECT 
     od.location, 
     currentLubes=CASE WHEN (od.item# = '200fs' OR od.item# = '200c' OR od.item# = '200m' OR od.item# = '200s') THEN od.qty_shipped ELSE NULL END, 
     currentAlignments=CASE WHEN (od.item# = '8501' OR od.item# = '8502') THEN od.qty_shipped ELSE NULL END, 
     currentSiping = CASE WHEN (invmas.item_num = 'p15' OR invmas.item_num = 'u15') THEN od.qty_shipped ELSE NULL END, 
     currentBatteries = CASE WHEN invmas.manufact = 'inter' THEN od.qty_shipped ELSE NULL END, 
     currentTires= CASE WHEN (invmas.category = 'uhp' OR invmas.category = 'tour' OR invmas.category = 'mass' OR invmas.category = 'suv' OR invmas.category = 'ltat' OR 
        invmas.category = 'ltmt' OR invmas.category = 'lthwy' OR invmas.category = 'snow' OR invmas.category = 'stdls') THEN od.qty_shipped ELSE NULL END, 
    hasCar= CASE WHEN (oh.veh_make != '' AND od.item# !='') THEN 1 ELSE 0 END 
    FROM 
     comm.dbo.ordetail as od 
     LEFT JOIN comm.dbo.invmas as invmas ON invmas.item_num = od.item# --FOR currentCarcount 
     INNER JOIN comm.dbo.ordhrd as oh ON oh.location = od.location AND oh.inv_date = od.inv_date AND oh.invoice# = od.invoice# 
    WHERE 
     od.inv_date BETWEEN '2017-02-01 00:00:00.000' AND '2017-02-28 00:00:00.000' 
     AND 
     oh.type_of_rec IN('01','02') 
)AS DetailList 
GROUP BY 
    DetailList.location 
+0

我不得不做幾個更改才能使其工作。這兩個'CASE WHEN'是一樣的,這是我的一個錯誤。我修正了這些。數據集是完整的,並且運行時間小於01秒。非常感謝您花時間爲我提供答案。 – Willee5586

+0

很高興我能幫到你。 –