What is the best way to paginate results in SQL Server

前端 未结 19 2453
我寻月下人不归
我寻月下人不归 2020-11-22 01:36

What is the best way (performance wise) to paginate results in SQL Server 2000, 2005, 2008, 2012 if you also want to get the total number of results (before paginating)?

相关标签:
19条回答
  • 2020-11-22 02:20

    For SQL Server 2000 you can simulate ROW_NUMBER() using a table variable with an IDENTITY column:

    DECLARE @pageNo int -- 1 based
    DECLARE @pageSize int
    SET @pageNo = 51
    SET @pageSize = 20
    
    DECLARE @firstRecord int
    DECLARE @lastRecord int
    SET @firstRecord = (@pageNo - 1) * @pageSize + 1 -- 1001
    SET @lastRecord = @firstRecord + @pageSize - 1   -- 1020
    
    DECLARE @orderedKeys TABLE (
      rownum int IDENTITY NOT NULL PRIMARY KEY CLUSTERED,
      TableKey int NOT NULL
    )
    
    SET ROWCOUNT @lastRecord
    INSERT INTO @orderedKeys (TableKey) SELECT ID FROM Orders WHERE OrderDate >= '1980-01-01' ORDER BY OrderDate
    
    SET ROWCOUNT 0
    
    SELECT t.*
    FROM Orders t
      INNER JOIN @orderedKeys o ON o.TableKey = t.ID
    WHERE o.rownum >= @firstRecord
    ORDER BY o.rownum
    

    This approach can be extended to tables with multi-column keys, and it doesn't incur the performance overhead of using OR (which skips index usage). The downside is the amount of temporary space used up if the data set is very large and one is near the last page. I did not test cursor performance in that case, but it might be better.

    Note that this approach could be optimized for the first page of data. Also, ROWCOUNT was used since TOP does not accept a variable in SQL Server 2000.

    0 讨论(0)
提交回复
热议问题