您好我正在尝试优化以下查询。
查询工作正常,但需要花费大量时间才能获得结果。 我们的表中数据量很大 MessageCorrelationId是一个GUID OrginatingEventName是varchar(50)
目前没有创建索引,只是想知道MessageCorrelationId或/和OrginationEventName上的非聚集索引是否有帮助。
~/Library/Developer/Xcode/DerivedData/
答案 0 :(得分:0)
一种可能的方法是
Select MessageCorrelationId,BridgeName,ServiceName,OriginSystemName,CreatedDate,ErrorFlag,ErrorMessage
from (
select *,
COUNT(*) OVER (PARTITION BY MessageCorrelationId) as CountSteps,
ROW_NUMBER() OVER (PARTITION BY MessageCorrelationId ORDER BY CreatedDate DESC) AS rn
from Activity
where OriginatingEventName=@OriginatingEventName
AND CreatedDate >= @StartDate and ErrorFlag=0
) t
where CountSteps >= @StepCount and rn=1;