SQL Indexing and Performance/Use keyset pagination

Paginate by a stable cursor instead of a large OFFSET.

Section: Pagination, Deduplication, and Search Patterns

Use keyset pagination

sql
sql
SELECT *
FROM posts
WHERE (published_at, post_id) < (TIMESTAMP "2025-03-01 10:00:00", 12345)
ORDER BY published_at DESC, post_id DESC
FETCH FIRST 20 ROWS ONLY;
Explanation

Keyset pagination scales better than OFFSET for deep pages.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Pagination, Deduplication, and Search Patterns
Deduplicate rows with ROW_NUMBER
Keep one row per business key and remove duplicates logically.
OpenIn sheetsqlsame section
Search across multiple columns
Apply a simple wildcard search over several text fields.
OpenIn sheetsqlsame section
Explain a query
Inspect the execution plan for a SELECT statement.
OpenIn sheetsql1 tag match
Index a common lookup column
Speed up frequent equality or range predicates.
OpenIn sheetsql1 tag match
Explain with runtime stats
Run a query and collect timing or actual row information where supported.
OpenIn sheetsql1 tag match
Refresh optimizer statistics
Update table statistics for better planning.
OpenIn sheetsql1 tag match