SQL Server Pagination: When Keyset Paging Is a Better Fit Than OFFSET

Have you ever noticed that a SQL Server query returns the first page of results quickly, but becomes slower as you move through more pages?

One common reason is the use of OFFSET pagination. As you move to later pages, SQL Server may need to process an increasing number of rows just to skip them and return the next set of results.

This is where keyset pagination can be a better alternative. Instead of skipping rows, it uses the last retrieved key to determine where the next page should begin.

In this article, we’ll explore how both approaches work, why OFFSET pagination can become slower, and when keyset pagination is the better choice.

 

Give the results a complete order

Suppose an application lists orders by creation time. Multiple orders can share that timestamp, so ordering by CreatedAt alone does not uniquely identify each position.

Add a unique tie-breaker such as OrderId. The continuation token must carry both values, preserving the timestamp’s precision.

The following is an application-query example. It assumes non-null CreatedAt and OrderId columns, a unique OrderId, and parameters matching their SQL types.

SELECT TOP (20)
       OrderId, CreatedAt, TotalAmount
FROM dbo.Orders
WHERE CreatedAt > @LastCreatedAt
   OR (CreatedAt = @LastCreatedAt
       AND OrderId > @LastOrderId)
ORDER BY CreatedAt, OrderId;

The first page uses the same ordering without the continuation predicate. Subsequent pages use the final row’s two key values from the previous page.

 

Match the index to the access pattern

An index beginning with CreatedAt and OrderId is a candidate for this query shape. Additional filters, such as a customer scope, can change the appropriate key order.

Measure the actual plan, reads and duration with realistic data. The existence of a suitable-looking index does not guarantee that every variation of the query will be efficient.

For a customer-specific screen, keep the customer filter in every request. A continuation token must never replace the authorization boundary that restricts which orders the caller may see.

 

Understand what keyset paging does not provide

Keyset paging naturally supports moving forward from a known position. Jumping directly to page 500 is less convenient because the application does not necessarily know the key at that boundary.

OFFSET can still be appropriate for small bounded results or interfaces that require arbitrary page numbers. The decision should follow navigation needs and measured workload cost.

Keyset pagination also does not freeze a changing dataset. New rows may appear after the current position, and updates to ordering values can move existing rows across boundaries. If a consistent export is required, design an appropriate snapshot or data-version boundary separately.

 

Keep the token stable and scoped

Include enough information to reconstruct the original ordering and filters. Reject a token used with a different sort mode or incompatible filter set. Protect it from tampering where its contents affect query behaviour or disclose information.

Do not build SQL by concatenating token values. Parse and validate the token, then bind typed values through the database client.

 

Test equal timestamps and concurrent changes

Create more than one page of rows sharing the same timestamp. Confirm that the tie-breaker prevents gaps between those rows. Then test deletion, insertion and changes to the ordering column between page requests, and document the resulting user experience.

A good paging design defines what “next” means. Once that definition is explicit, the query, index and API token can all support the same rule.

 

Technical references

 

Continue learning

To build the database and query foundations behind this topic, explore my course SQL Server Fundamentals – SQL Database for Beginners on Udemy. Review the curriculum and preview to see whether it fits your next learning goal.

 

Read Also

 

Stay connected

Subscribe to the GnoelixiAI Hub newsletter on LinkedIn for practical insights on AI, data and technology.

Watch more on YouTube: SQLNetHub TV and my personal channel.

 

Rate this article: (No Ratings Yet)

Originally published on SQLNetHub.
© SQLNetHub