The Real Fullstack

The Slow Pagination

Same query, same index, only the page number changed. Why is page 5,000 a hundred times slower?

Ingénieur7 minLaboratoire de performanceDéfi de code
sqlpostgresqlrestangular

Ce défi n'est pas encore traduit en Français. Vous lisez la version anglaise.

Mission:Serve page 5,000 in ≤ 5 ms while new rows are being inserted, without duplicates.Contexte · Objectifs d'apprentissage ▾

Larkspur's activity feed has an infinite scroll backed by LIMIT/OFFSET. Power users and crawlers reach deep pages; the database CPU climbs with them. Users also report seeing the same event twice after new events arrive.

Mission: Serve page 5,000 in ≤ 5 ms while new rows are being inserted, without duplicates.

Objectifs d'apprentissage

  • Explain why OFFSET cost grows with the page number.
  • Implement keyset (seek) pagination with a stable sort key.
  • Understand the UX trade-off: no random page jumps.
Simulation
Simulé

Lancez la simulation pour voir la sortie.

Lancer la simulation
SELECT id, title, created_at FROM events ORDER BY created_at DESC, id DESC LIMIT 20OFFSET 99980;          -- reads and discards 99,980 index entries first

Simplification

Cost proportional to index entries read. Real costs also include fetching heap rows for visible columns. Modèle pédagogique — les chiffres illustrent un comportement, ce ne sont pas des benchmarks.

Concepts

Utile avant celui-ci

Défis liés

Références