Fast SQL server paging
In 2025 MSSQL Server paging with `OFFSET… FETCH` still works, but for large datasets this approach slows down on deep pages. Modern best practice is to use keyset/seek pagination instead, and only fetch total row counts when needed.
Just a quick tip if you ever need to implement paging in MSSQL. Here is a simple example of the query, which gets page x of size y and gets the total row count as well.
SELECT ID, Name, [RowCount] = COUNT(*) OVER()
FROM MyTable
WHERE Name LIKE '%Example%'
ORDER BY ID
OFFSET @Page ROWS
FETCH NEXT @PageSize ROWS ONLY
This must be used with an ORDER BY
clause. The @Page
and @PageSize
determine which set of records are returned out of the total result set.