2015 年 12 月 17 日 - 更新
我问这个问题已经有一段时间了,但还没有真正得到答案,所以我想我会发布我的团队最终使用的解决方案。
我们最初使用动态 SQL 选项来获得智能排序。虽然实用,但感觉真的很脏。我们最终提出了以下实现:
--Params
DECLARE
@pageSize INT
, @pageIndex INT
, @sortBy varchar(30)
, @sortDirection varchar(4);
SET @pageIndex = 1;
SET @pageSize = 10;
SET @sortBy = 'PolicyCode';
SET @sortDirection = 'ASC';
--Vars
DECLARE @start INT,
@end INT;
--Page 1-n
SET @pageIndex = 1;
SET @pageSize = 10;
SET @start = (@pageSize * @pageIndex) - (@pageSize - 1);
SET @end = (@pageSize * @pageIndex);
;WITH PolicyCTE AS (
SELECT p.PolicyId, p.PolicyTypeId, p.PolicyCode, p.PolicyDesc, p.EffectiveDate, p.ExpirationDate
FROM Policy p INNER JOIN PolicyOrg po ON p.PolicyId = po.PolicyId
)
, PagedResultsCTE AS (
SELECT
[PolicyId]
, [PolicyTypeId]
, [PolicyCode]
, [PolicyDesc]
, [EffectiveDate]
, [ExpirationDate]
, CASE
WHEN @sortBy = 'PolicyTypeId' AND @sortDirection = 'ASC' THEN ROW_NUMBER() OVER (ORDER BY [PolicyTypeId] ASC)
WHEN @sortBy = 'PolicyTypeId' AND @sortDirection = 'DESC' THEN ROW_NUMBER() OVER (ORDER BY [PolicyTypeId] DESC)
WHEN @sortBy = 'PolicyCode' AND @sortDirection = 'ASC' THEN ROW_NUMBER() OVER (ORDER BY [PolicyCode])
WHEN @sortBy = 'PolicyCode' AND @sortDirection = 'DESC' THEN ROW_NUMBER() OVER (ORDER BY [PolicyCode] DESC)
WHEN @sortBy = 'PolicyDesc' AND @sortDirection = 'ASC' THEN ROW_NUMBER() OVER (ORDER BY [PolicyDesc])
WHEN @sortBy = 'PolicyDesc' AND @sortDirection = 'DESC' THEN ROW_NUMBER() OVER (ORDER BY [PolicyDesc] DESC)
WHEN @sortBy = 'EffectiveDate' AND @sortDirection = 'ASC' THEN ROW_NUMBER() OVER (ORDER BY [EffectiveDate])
WHEN @sortBy = 'EffectiveDate' AND @sortDirection = 'DESC' THEN ROW_NUMBER() OVER (ORDER BY [EffectiveDate] DESC)
WHEN @sortBy = 'ExpirationDate' AND @sortDirection = 'ASC' THEN ROW_NUMBER() OVER (ORDER BY [ExpirationDate])
WHEN @sortBy = 'ExpirationDate' AND @sortDirection = 'DESC' THEN ROW_NUMBER() OVER (ORDER BY [ExpirationDate] DESC)
END AS RowNumber
FROM PolicyCTE
)
SELECT PolicyId, PolicyTypeId, PolicyCode, PolicyDesc, EffectiveDate, ExpirationDate
FROM PagedResultsCTE
WHERE RowNumber BETWEEN @start AND @end
ORDER BY RowNumber
GO
通过结合 CASE 语句和另一个 CTE,我们能够按照传入的排序标准进行排序,并获得正确的 ROWNUMBER() 值。然后,在我们的最终选择语句中,我们可以简单地按行号列排序。
我将避免暂时将问题标记为已回答,以防有人想加入此解决方案。绝对欢迎反馈。
原帖
也许有更多 DBA 知识的人可以为我阐明这一点。基本前提:使用存储过程中的优化查询获取分页数据以提高页面加载性能(获取记录 1-10、11-20 等)。这些表有数千条记录。
我正在使用 SQL Server 2008 R2 并想出了以下 T-SQL 用于存储过程:
--Params
DECLARE @pageSize INT,
@pageIndex INT;
SET @pageIndex = 1;
SET @pageSize = 10;
--Vars
DECLARE @start INT,
@end INT
--Page 1-n
SET @pageIndex = 1;
SET @pageSize = 10;
SET @start = (@pageSize * @pageIndex) - (@pageSize - 1);
SET @end = (@pageSize * @pageIndex)
;WITH PolicyCTE AS
(
SELECT ROW_NUMBER() OVER (ORDER BY p.PolicyCode) AS RowNumber,
p.PolicyId, p.PolicyTypeId, p.PolicyCode, p.PolicyDesc, p.EffectiveDate, p.ExpirationDate
FROM Policy p INNER JOIN PolicyOrg po ON p.PolicyId = po.PolicyId
)
SELECT PolicyId, PolicyTypeId, PolicyCode, PolicyDesc, EffectiveDate, ExpirationDate
FROM PolicyCTE
WHERE RowNumber BETWEEN @start AND @end
ORDER BY PolicyCode
GO
在寻找有关如何执行此操作的最佳实践的过程中,我遇到了这篇旧的MSDN 模式和实践文章:如何:.NET 应用程序中的页面记录(适用于 SQL 2000,并声明该文章已过时)。参考用户特定记录部分,有点等价的 T-SQL 如下所示:
DECLARE
@pageIndex int,
@pageSize int,
@sortBy nvarchar(30)
SET @pageIndex = 0;
SET @pageSize = 10;
SET @sortBy = 'PolicyCode';
DECLARE @rowsToRetrieve int,
@sortDirFlipped nvarchar(4);
IF @pageIndex < 1 SET @pageIndex = 1;
IF @pageSize < 1 SET @pageSize = 10;
SET @rowsToRetrieve = @pageIndex * @pageSize
DECLARE @sqlString nvarchar(1000);
SET @sqlString = N'
SELECT PolicyId, PolicyTypeId, PolicyCode, PolicyDesc, EffectiveDate, ExpirationDate
FROM (
SELECT TOP ' + CAST(@pageSize AS VARCHAR(10)) + ' PolicyId, PolicyTypeId, PolicyCode, PolicyDesc, EffectiveDate, ExpirationDate
FROM (
SELECT TOP ' + CAST(@rowsToRetrieve AS VARCHAR(10)) + ' PolicyId, PolicyTypeId, PolicyCode, PolicyDesc, EffectiveDate, ExpirationDate
FROM (
SELECT TOP ' + CAST(@rowsToRetrieve AS VARCHAR(10)) + ' p.PolicyId, p.PolicyTypeId, p.PolicyCode, p.PolicyDesc, p.EffectiveDate, p.ExpirationDate
FROM Policy p INNER JOIN PolicyOrg po ON p.PolicyId = po.PolicyId
ORDER BY p.' + @sortBy + '
) AS T2
ORDER BY ' + @sortBy + ' DESC
) AS T3
) AS T4
ORDER BY ' + @sortBy + ' ASC';
EXEC(@sqlString)
GO
我已经在执行计划和客户统计数据的情况下运行了这两种方法,在我看来(绝对不是分析这些结果的专家)MSDN 版本的性能更好。据我所知,部分原因是 MSDN 版本开始时记录集较小。但是,我真的不喜欢 MSDN 版本使用动态查询这一事实。我的理解是它们更容易受到 SQL 注入攻击。
我发现的大多数示例都更喜欢第一个示例的形式。有人可以帮我解释一下这个原因和网站原因,除了已经列出的那些之外,第一种形式会比第二种更好吗?