STAGE 04 / LESSON 03
Indexes and query plans
An index can help a database find matching rows without inspecting every row.
THE IDEA
Understand it.
Indexes have a cost: they use storage and must be maintained when rows change. A low-selectivity column or a small table may still be faster to scan; the database chooses a plan based on estimates.
Measure with a representative query and data. EXPLAIN shows the chosen plan; EXPLAIN ANALYZE actually runs the statement, so use care with writes and production workloads.
SEE IT IN CONTEXT
Inspect a task query
CREATE INDEX tasks_status_idx ON tasks (status);
EXPLAIN
SELECT id, title FROM tasks WHERE status = 'open';Creating an index does not guarantee the planner will use it. Check the plan and measure before and after.
LEARN FROM REAL WEBSITES
See it in the real world.
These simplified structures come from public sources. Company systems change, and the sources may describe only one project or an earlier design.
GitLab
Documentation checked November 2026Read a query plan
GitLab publishes a guide to interpreting PostgreSQL EXPLAIN plans for its database work.
- SQL query
- EXPLAIN plan
- Scans and estimated work
What to learn: A plan shows the database's chosen approach; use it to investigate scans and indexes rather than guessing.
Read the source: GitLab query plansGitHub
GitHub engineering, 2015Measure a database workload
GitHub describes using MySQL Performance Schema to examine queries and table and index activity.
- MySQL activity
- Performance Schema
- Query and index analysis
What to learn: Production workload evidence helps decide which queries or indexes need attention. The techniques shown are MySQL-specific.
Read the source: GitHub MySQL workload analysisTRY IT YOURSELF
Practice.
Why might an index on status be less useful if almost every task is open?
Show one possible answer
The filter returns most rows, so reading through the index and then fetching rows may cost more than a table scan.
GO DEEPER