0
以下是我的查詢。處理需要12秒。我創建了T.DataViewId
的索引,但由於Count(distinct())
和Sum
,它仍然需要很長時間。提前致謝。SQl Server 2008性能問題計數(distinct())和SUM。如何避免這個問題?
;WITH my_cte
AS (SELECT T.name AS name,
T.id AS id,
Count(DISTINCT(DD.dynamictableid)) AS counts,
Round(Sum(D.[employees]), 0) AS measure1
FROM dbo.treehierarchy T
LEFT JOIN dbo.dynamicdatatableid DD
ON T.id = DD.hierarchyid
AND T.dataviewid = DD.dataviewid
LEFT JOIN dbo.demo1 D
ON D.[demo1id] = DD.dynamictableid
WHERE T.dataviewid = 2
AND T.parentid = 0
GROUP BY T.id,
T.name)
SELECT name, id, counts, row_num, measure1
FROM (SELECT name,
id,
counts,
Row_number()
OVER(
ORDER BY counts DESC) AS row_num,
measure1
FROM my_cte) innertable
WHERE (row_num BETWEEN 1 AND 15)