Keyset pagination
Core idea
Keyset pagination uses the last returned sort key as a boundary for the next page instead of counting skipped rows. It is useful for sequential browsing.
Minimal example
SELECT id FROM products WHERE id > 2 ORDER BY id LIMIT 2;Expected SQLite rows:
34How it works
For ascending unique id order, use WHERE id > last_id. For multiple sort keys, use a lexicographic condition that matches the ordering: greater customer_id, or equal customer_id and greater id. In applications bind cursor values as parameters. Suitable indexes may improve performance, but no benchmark is claimed. Concurrent updates to sort keys can still cause missed or repeated rows; this is not snapshot isolation.
Practical example
SELECT customer_id, id FROM orders WHERE customer_id > 1 OR (customer_id = 1 AND id > 101) ORDER BY customer_id, id LIMIT 2;Expected SQLite rows:
1 | 1022 | 103Common mistake and practice
Do not use >= for the cursor boundary. After order (1, 102), predict the next row: (2, 103). Keyset pagination is not direct random page access.