我有一个查询性能问题,有时Select查询执行需要10分钟以上。表大小为26GB,包含800万个数据。 查询是:
SELECT X.[PRIMARY], E.[COLUMN_2], E.[COLUMN_3], E.[COLUMN_4], E.[COLUMN_5]...E.[COLUMN_N]
FROM(
SELECT E.[PRIMARY], ROW_NUMBER() OVER(ORDER BY COLUMN_SORT DESC) AS RowNumber
FROM dbo.[TABLE]E WITH(NOLOCK) WHERE COLUMN_WHERE_1 = 11943 AND COLUMN_WHERE_2 = 0) X
JOIN dbo.[TABLE] E WITH(NOLOCK) on E.[PRIMARY] = X.[PRIMARY]
WHERE RowNumber > 150 AND RowNumber <= 200
ORDER BY COLUMN_SORT
我有非聚集的INDEX(COLUMN_WHERE_1,COLUMN_WHERE_2)包括(COLUMN_SORT)。
执行计划看起来像
您有一个想法,如何优化查询或索引?
答案 0 :(得分:0)
如果创建的索引键与查询中 ORDER BY 子句中的键列匹配(您的情况下为嵌套键),则 SORT 运算符可以在查询计划中消除,查询计划更有效。因此,可能会创建以下 NONCLUSTERED 索引:
CREATE NONCLUSTERED INDEX IDX_ColumnSort
ON dbo.[TABLE]
(COLUMN_SORT DESC, COLUMN_WHERE_1, COLUMN_WHERE_2);
答案 1 :(得分:0)
从此开始:
SELECT E.[PRIMARY], E.[COLUMN_2], E.[COLUMN_3], E.[COLUMN_4], E.[COLUMN_5]...E.[COLUMN_N]
FROM(
SELECT E.[PRIMARY], E.[COLUMN_2], E.[COLUMN_3], E.[COLUMN_4], E.[COLUMN_5]...E.[COLUMN_N],
ROW_NUMBER() OVER(ORDER BY COLUMN_SORT DESC) AS RowNumber
FROM dbo.[TABLE]E
WHERE COLUMN_WHERE_1 = 11943 AND COLUMN_WHERE_2 = 0
) X
WHERE RowNumber > 150 AND RowNumber <= 200
ORDER BY RowNumber
RowNumber
选项的ORDER BY