The Real Fullstack

The Million-Row Problem

The page was instant at launch. A year and a million orders later it takes 150 ms per call. Why?

エンジニア9 分パフォーマンスラボコード課題
postgresqlsqlredisspring-data-jpa

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

ミッション:Read the (simulated) execution plan and choose indexes so that even an uncached request meets a 20 ms budget, without slowing down writes with indexes nobody uses.背景 · 学習目標 ▾

Fernway Outfitters, a fictional online shop, shows each customer their 20 latest shipped orders. The query has not changed since launch, but the orders table has grown to a million rows and the p95 latency of the endpoint has followed it. A teammate suggests putting Redis in front of it.

ミッション: Read the (simulated) execution plan and choose indexes so that even an uncached request meets a 20 ms budget, without slowing down writes with indexes nobody uses.

学習目標

  • Read a query plan: sequential scan, index scan, sort, rows examined.
  • Order composite index columns: equality columns first, then the sort column.
  • Distinguish a cache (mitigation for repeated reads) from fixing the access path.
シミュレーションシミュレーション値
Account pageOrders APIRedisPostgreSQL
シミュレーションを実行
SELECT id, total, status, created_at  FROM orders WHERE customer_id = $1 AND status = 'SHIPPED' ORDER BY created_at DESC LIMIT 20;
CREATE INDEX idx_status   ON orders (status);CREATE INDEX idx_created  ON orders (created_at);CREATE INDEX idx_customer ON orders (customer_id);-- equality columns first, then the ORDER BY column:CREATE INDEX idx_customer_status_created ON orders (customer_id, status, created_at);

簡略化

A tiny cost model with uniform data replaces PostgreSQL's real statistics. Real planners consider correlation, caching of pages, parallel workers and many more paths. 教育用モデルです。数値は挙動を示すためのもので、ベンチマークではありません。

コンセプト

関連する課題

参考資料