SQL for BAs: why a query is slow
A slow query costs time and can hurt a busy system. Learn the few causes you can spot and how to ask for help with a plan.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: finding a word in a book. With no index you read every page. With an index you open the back of the book and jump. A very short book needs no index. If the search took too long, you ask how the reader looked, not only what they found.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). A task report feels slow. Nina, the , asks Sam, the , why. Sam asks the database how it plans to look.
What it is: (Structured Query Language) is a language for asking a database questions. A table is a grid of rows and columns. A row is one task. A column is one fact. means the command that shows the steps the database expects to take, and the plan is that list of steps. A full table scan reads every row. An index is an extra structure that can jump to rows by one column, like the index of a book. EXPLAIN lives in the database program. Its neighbour is the query result itself. The result says what came back. The plan says how the database looked.
Why it matters: A slow report can read millions of rows and make the live app slow for Lily and everyone else. Sam can spot a and a missing index, then hand the plan to Leo, the developer. Guessing an index without the plan often adds cost and no speed.
The book has one reader. A busy database has many, so a heavy plan hurts other people too.
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the work.
THE REAL JOB Finding a word in a book, with or without an index.
- Plan
- Scan
- Index
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| The book | The table |
| The index | A database index |
| Reading every page | A full scan |
| Asking how the reader looked | EXPLAIN |
WHERE THIS PICTURE STOPS
- A book is read by a person, but a database has a planner.
- A book index is fixed, but data changes.
- A page is read once, but queries repeat.
HOOK
Plan, scan, index.
TELL A FRIEND
A book: an index saves reading every page.
OFTEN MIXED UP WITH Databases for analysts: indexes, views, locks and transactions. That topic explains indexes and views. This one uses them to understand a slow query.
PRACTISE AND KEEP
Sources for this topic — 5 sources
- Market practice
Using EXPLAIN (PostgreSQL documentation) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §14.1 estimated cost/rows, ANALYZE actual work/loops and execution; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §14.1 estimated cost/rows, ANALYZE actual work/loops and execution. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
PostgreSQL18: indexes on expressions ↗ — PostgreSQL Global Development Group · PostgreSQL 18: indexes on expressions; §11.7 expression index example lower(col), affected maintenance and non-HOT update scope. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Market practice
MySQL8.4: EXPLAIN ↗ — Oracle / MySQL · MySQL 8.4: EXPLAIN; §15.8.2 EXPLAIN ANALYZE executes and shows actual timing/iterator rows. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Market practice
SQLite: query planning ↗ — SQLite project · SQLite: query planning; Index lookup and covering-index source figures5/14; query planner concepts, no promised runtime multiplier. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Simplified educational illustration
Payments Signal editorial teaching models — Payments Signal
What this simplifies: Daily List people, data and exercises are invented for teaching. Query results belong to the declared fixture and database version. Authorization, business identity, time bounds and unknown-value policy must be agreed in context; no universal mandatory query template is implied.
Used wherever diagrams, scenarios, figures, or example values are didactic constructions rather than sourced facts; every such use carries a simplifications disclosure. All people, companies, banks, and list entries in examples are fictional.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.