我有这个查询来获取活动之间的时间差距,其中只显示间隔小于10小时的活动。 它运行非常非常慢。大约有300项活动,差距不到10小时。对性能的任何建议?
WITH GapsBetweenActivities AS
(
SELECT ACTIVITY_ID
,ACTIVITY_START_TIME
,DATEDIFF(MINUTE, ACTIVITY_START_TIME, LAG(ACTIVITY_START_TIME) OVER (ORDER BY ACTIVITY_START_TIME)) AS PREVIOUS_ACTIVITY
FROM ACTIVITIES
WHERE ACTIVITY_START_TIME > GETDATE() -- FUTURE ACTIVITY
)
SELECT *
FROM GapsBetweenActivities
WHERE PREVIOUS_ACTIVITY IS NULL -- NULL PREVIOUS ACTIVITY (FIRST ONE IN TABLE)
OR
PREVIOUS_ACTIVITY > -600; -- DATE DIFF IS NEGATIVE MINUTES