我有一個查詢,我試圖優化但沒有太多成功。有兩張表,一張是主要數據,另一張是特定事件發生時的時間戳。這些表格使用adID的公共密鑰進行關聯。我試圖執行一個查詢,從主表中獲取所有時間戳和其他數據。我有它的工作,但我想優化,所以它運行得更快。如何優化具有多個子查詢的SELECT語句
SELECT a.ID,a.repID,a.artistID,
(
SELECT TOP 1 c.timestamp
FROM Tracking AS c
WHERE statusID = 4
AND c.ID = a.ID
ORDER BY c.timestamp ASC
)
AS created,
(
SELECT TOP 1 d.timestamp
FROM Tracking AS d
WHERE statusID = 5
AND d.ID = a.ID
ORDER BY d.timestamp ASC
)
AS claimed,
(
SELECT TOP 1 p.timestamp
FROM Tracking AS p
WHERE statusID = 6
AND p.ID = a.ID
ORDER BY p.timestamp ASC
)
AS proof,
(
SELECT TOP 1 v.timestamp
FROM Tracking AS v
WHERE statusID = 8
AND v.ID = a.ID
ORDER BY v.timestamp ASC
)
AS approved,
(
SELECT count(ID)
FROM Tracking AS t
WHERE statusID = 6
AND t.ID = a.ID
)
AS proofcount
FROM Advertising AS a
WHERE a.statusID = 8
對此的任何幫助表示讚賞。我對SQL Server不太熟悉,所以我不太熟悉如何優化這些查詢。
有多少行呢查詢回報? 「Tracking」有哪些索引? – 2013-02-15 20:09:36