The Real Fullstack

The Slow Pagination

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

エンジニア7 分パフォーマンスラボコード課題
sqlpostgresqlrestangular

この課題はまだ日本語に翻訳されていません。英語版を表示しています。

ミッション:Serve page 5,000 in ≤ 5 ms while new rows are being inserted, without duplicates.背景 · 学習目標 ▾

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.

ミッション: Serve page 5,000 in ≤ 5 ms while new rows are being inserted, without duplicates.

学習目標

  • 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.
シミュレーション
シミュレーション値

シミュレーションを実行すると出力が表示されます。

シミュレーションを実行
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

簡略化

Cost proportional to index entries read. Real costs also include fetching heap rows for visible columns. 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

先に取り組むと役立つ課題

関連する課題

参考資料