我有兩個部分的問題。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
採樣數據輸出:
您是否檢查過執行計劃? – user1413
我剛剛做了,我不熟悉我在看什麼。不是一個簡單的方法來分享它。大量的數據。 XML輸出是5k行。我應該確定高成本的運營並找到減少運營的方法嗎? – Willee5586
我不是那個意思。 SSMS有一個圖標。您甚至可以在查詢菜單 - >顯示估計執行計劃中找到它,這將有助於您根據顯示的百分比花費更多時間。 – user1413