我有一个查询,我正在尝试优化,但没有取得太大的成功。有两张表,一张包含主要数据,一张包含特定事件发生时间的时间戳。这些表是使用 adID 的公共键的关系表。我正在尝试执行从主表中提取所有时间戳和其他数据的查询。我让它工作,但我正在尝试优化,以便它运行得更快。
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 不太熟悉,所以我不太擅长优化诸如此类的查询。