API Engineering

REST API Pagination: Offset vs Cursor vs Keyset Pagination with SQL Examples

Learn how to paginate large datasets in REST APIs. Compare OFFSET/LIMIT with high-performance cursor-based and keyset pagination in SQL.

By Cometflow API Engineering Team • Updated 2026-10-06

Returning unbounded database tables in a single API response exhausts server RAM and freezes client browsers. Choosing between Offset Pagination and Keyset (Cursor) Pagination determines whether your API stays fast as tables grow past millions of rows.

1. SQL Performance Comparison: OFFSET vs. Keyset

-- ❌ SLOW ON DEEP PAGES: Database must scan and discard 100,000 rows (O(N))
SELECT id, title, created_at
FROM articles
ORDER BY created_at DESC, id DESC
LIMIT 20 OFFSET 100000;

-- ✅ FAST AT ANY DEPTH: Uses composite B-Tree index seek (O(log N))
SELECT id, title, created_at
FROM articles
WHERE (created_at, id) < ('2026-10-01T12:00:00Z', 48192)
ORDER BY created_at DESC, id DESC
LIMIT 20;

Frequently Asked Questions (FAQ)

When is OFFSET pagination still appropriate?

Offset pagination is appropriate for small-to-medium back-office tables (under 25,000 rows) where users explicitly need numbered page buttons (Page 1, 2, 3... 15) rather than infinite scroll or Next/Previous navigation.