Reputation: 6431
I have few questions in context of the older row_number (SQL Server 2008) and the newer OFFSET + FETCH (SQL Server 2012) paging mechanism provided by SQL Server 2012.
Thanks.
Upvotes: 26
Views: 13102
Reputation: 1213
I found this article which presents 3 techniques of paging compares their results in a chart. Might be helpful in deciding which approach you want to follow.
Paging Function Performance in SQL Server 2012
All of the paging methods discussed work fine for smaller amounts of records, but not so much for larger quantities of data.
Upvotes: 0
Reputation: 31
By definition ROW_NUMBER
is a temporary value calculated when the query is run. OFFSET / FETCH
is an option that you can specify for the ORDER BY
clause.
In terms of speed, they both achieve great performance and the difference between each method depends on the columns that you specify in the SELECT
clause and the Indexes that you have on your tables.
In the following 2 examples, you can see a difference between the two methods:
1. Case when OFFSET / FETCH is faster:
SELECT
Id
FROM Orders
ORDER BY
Id
OFFSET 50000 ROWS FETCH NEXT 5000 ROWS ONLY
SELECT
A.Id
FROM
(
SELECT
Id,
ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber FROM Orders
) AS A
WHERE
A.RowNumber BETWEEN 50001 AND 55000
2. Case when ROW_NUMBER() is faster:
SELECT
*
FROM Orders
ORDER BY
Id
OFFSET 50000 ROWS FETCH NEXT 5000 ROWS ONLY
SELECT
A.*
FROM
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber FROM Orders
) AS A
WHERE
A.RowNumber BETWEEN 50001 AND 55000
Upvotes: 3
Reputation: 755063
Using ROW_NUMBER()
works fine - it's just more work than necessary; you need to write a "skeleton" CTE around your actual query, add the ROW_NUMBER()
column to your output set, and then filter on that.
Using the new OFFSET / FETCH
is simpler - and yes, it's also better for performance, as these two links can show you:
So overall: if you're using SQL Server 2012 - then you should definitely use OFFSET/FETCH
rather than ROW_NUMBER()
for paging
Upvotes: 21