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?

Ingeniero9 minLaboratorio de rendimientoReto de código
postgresqlsqlredisspring-data-jpa

Este reto aún no está traducido al Español. Estás leyendo la versión en inglés.

Misión: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.Contexto · Objetivos de aprendizaje ▾

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.

Misión: 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.

Objetivos de aprendizaje

  • 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.
SimulaciónSimulado
Account pageOrders APIRedisPostgreSQL
Ejecutar simulación
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);

Simplificación

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. Modelo educativo: los números ilustran un comportamiento, no son benchmarks.

Conceptos

Retos relacionados

Referencias