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. 教学模型——数字用于说明行为,并非基准测试。

概念

相关挑战

参考资料