時,對於下面的腳本:性能訂貨
DECLARE @StartDate DATETIME,
@EndDate DATETIME,
@SearchSt INT
SET @StartDate = '2014-02-01'
SET @EndDate = '2014-02-20'
SET @SearchSt = 2;
WITH DSAEvent
AS
(
SELECT ROW_NUMBER() OVER
(
PARTITION BY e.TT_TAG_ID
ORDER BY e.TT_TAG_ID,
e.DSA_Event_Date,
e.DSA_Time_In,
e.DSA_Time_Out,
e.TAG_Inter_DSA_Seq_No
) AS rn,
e.EventID, e.TT_TAG_ID, e.DSA_ID, e.DSA_Purpose_ID, e.DSA_Time_In,
e.DSA_Time_Out, e.DSA_Duration, e.Sh_DSA_Event_Record_Type,
e.TAG_Inter_DSA_Seq_No,
ISNULL((
SELECT CASE
WHEN e.DSA_Purpose_ID = 3 THEN ISNULL(T_No, 0)
ELSE -1
END
FROM DSAs
WHERE St_ID = @SearchSt
AND DSA_ID = e.DSA_ID
), -1) AS T_No
FROM SH_DSA_EVENT_WORKING e
WHERE e.Store_ID = @SearchSt
AND e.DSA_Event_Date >= @StartDate
AND e.DSA_Event_Date <= @EndDate
AND e.Sh_ID IS NULL
)
SELECT p.TT_TAG_ID, p.DSA_ID, p.DSA_Purpose_ID, p.DSA_Time_In,
p.DSA_Time_Out, p.DSA_Duration, p.Sh_DSA_Event_Record_Type,
p.TAG_Inter_DSA_Seq_No, p.Till_No,
ABS(DATEDIFF(
second,
ISNULL(n.DSA_Time_In, '2000-01-01 00:00:00'),
ISNULL(p.DSA_Time_Out, '2000-01-01 00:00:00'))
),
td.TAG_Asset_Type_ID, p.EventID
FROM DSAEvent p
LEFT JOIN DSAEvent n ON p.rn = n.rn - 1 AND p.TT_TAG_ID = n.TT_TAG_ID
LEFT JOIN TAG_DETAIL td ON td.TT_TAG_ID = p.TT_TAG_ID
WHERE ISNULL(td.St_ID, @SearchSt) = @SearchSt
ORDER BY td.TT_TAG_ID, p.DSA_Time_In, p.DSA_Time_Out,
p.TAG_Inter_DSA_Seq_No, p.rn
這似乎一直無法進行運行。我已經縮小到最後的ORDER子句。
如果我註釋掉ORDER子句,我會在13秒內返回741594行。如果我讓腳本以ORDER子句運行,它將運行超過13個小時。
我錯過了什麼,或者如何找出導致延遲的原因?
你必須簡化這個測試。拔出連接並選擇標準和度量。一次添加一個項目並重新測量以查看問題出在哪裏。另外,學習執行計劃。 – n8wrl
你有沒有'TT_TAG_ID'上的索引? –
TT_TAG_ID上確實有一個索引 – RobbieE