ROW_NUMBER() OVER Not Fast Enough With Large Result Set, any good solution?

前端 未结 2 671
萌比男神i
萌比男神i 2020-12-16 05:10

I use ROW_NUMBER() to do paging with my website content and when you hit the last page it timeout because the SQL Server takes too long to complete the search.

There

2条回答
  •  攒了一身酷
    2020-12-16 05:27

    The base logic of this method relies on the SET ROWCOUNT expression to both skip the unwanted rows and fetch the desired ones:

    DECLARE @Sort /* the type of the sorting column */
    SET ROWCOUNT @StartRow
    SELECT @Sort = SortColumn FROM Table ORDER BY SortColumn
    SET ROWCOUNT @PageSize
    SELECT ... FROM Table WHERE SortColumn >= @Sort ORDER BY SortColumn
    

    The issue is well covered in this CodeProject article, including scalability graphs.

    TOP is supported on SQL Server 2000, but only static values. Eg no "TOP (@Var)", only "TOP 200"

提交回复
热议问题