Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/data-pagination.md
1---2title: Use Cursor-Based Pagination Instead of OFFSET3impact: MEDIUM-HIGH4impactDescription: Consistent O(1) performance regardless of page depth5tags: pagination, cursor, keyset, offset, performance6---78## Use Cursor-Based Pagination Instead of OFFSET910OFFSET-based pagination scans all skipped rows, getting slower on deeper pages. Cursor pagination is O(1).1112**Incorrect (OFFSET pagination):**1314```sql15-- Page 1: scans 20 rows16select * from products order by id limit 20 offset 0;1718-- Page 100: scans 2000 rows to skip 198019select * from products order by id limit 20 offset 1980;2021-- Page 10000: scans 200,000 rows!22select * from products order by id limit 20 offset 199980;23```2425**Correct (cursor/keyset pagination):**2627```sql28-- Page 1: get first 2029select * from products order by id limit 20;30-- Application stores last_id = 203132-- Page 2: start after last ID33select * from products where id > 20 order by id limit 20;34-- Uses index, always fast regardless of page depth3536-- Page 10000: same speed as page 137select * from products where id > 199980 order by id limit 20;38```3940For multi-column sorting:4142```sql43-- Cursor must include all sort columns44select * from products45where (created_at, id) > ('2024-01-15 10:00:00', 12345)46order by created_at, id47limit 20;48```4950Reference: [Pagination](https://supabase.com/docs/guides/database/pagination)51