Query performance

EXPLAIN ANALYZE, index selection, N+1 detection in ORMs (Prisma/Drizzle), connection pooling (pgBouncer). "This page takes 4s" is always a query question first.

About 28 min read3-question knowledge check

The mental model

A library with a card catalog answers "find every book by this author" in seconds; a library without one answers it by walking every shelf. A sequential scan is walking the shelves; an index scan is the catalog. But the catalog is not free — every new book must be filed in every catalog (write cost), and for some questions ("find every book with a blue cover") no sensible catalog exists and walking the shelves is genuinely the right plan.

The piece most engineers miss: between your SQL and the data sits a cost-based planner that chooses *how* to execute — which index, which join algorithm, which order. It chooses using statistics (sampled histograms of your data), not by looking at the actual rows. When the statistics are right, it picks well; when they are stale or the data is skewed, it confidently picks a catastrophic plan. "The database is slow" is almost never true — one query got a bad plan, or was asked 51 times instead of once is what is actually happening.

What breaks without this skill: the page that takes 4 seconds. The team adds caching, upgrades the instance, blames the ORM — when EXPLAIN ANALYZE would have shown a sequential scan over 12M rows, or the request log would have shown 51 queries where 1 was needed (the N+1 problem). Query performance is the highest-leverage debugging skill in fullstack work because the fix is usually one index or one query reshape, and the win is usually 10–100×.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision