New: Try Voli The Bear, Fast package manager (and not only) for Windows
All topics / Why Is My Query Slow? (Indexes & EXPLAIN)

Why Is My Query Slow? (Indexes & EXPLAIN)

A query that's instant on your laptop crawls in production because the database is reading every row to find matches; an index lets it jump straight to them, and EXPLAIN lets you see which one is happening.

Download EPUB
  1. The Full-Table Scan A database with no help reads every single row to find your matches - a full-table scan. That's invisible on 100 rows and brutal on 10 million, which is why a query is fast on your laptop and dying in prod.
  2. Indexes An index is a separate, sorted structure (a B-tree) that lets the database jump straight to matching rows instead of scanning every one - like the index at the back of a book. It speeds up reads but costs you on writes and disk, so index the columns you filter, join, and sort on, not everything.
  3. Reading EXPLAIN EXPLAIN shows the database's plan for a query and EXPLAIN ANALYZE actually runs it; learn to read seq scan vs. index scan, compare estimated vs. actual rows to spot a bad plan, and follow the measure-add-index-recheck loop to fix a slow query for real.