EXPLAIN and Query Plans

Ask the database what it intends to do before blaming it for being slow. Add an index and watch the plan change.

EXPLAIN and Query Plans

Watch the plan change

query.sql SQLite
Result

Worth knowing

EXPLAIN reports the plan the optimiser chose. It is the database's own account, not a guess.
SCAN means every row is read. SEARCH ... USING INDEX means it jumped straight to the matching ones.
The plan depends on statistics as well as indexes. On a tiny table a scan can genuinely be the better choice.
An index on a column is invisible to a query that wraps that column in a function.

Overview

SQL describes the result you want; the optimizer chooses how to retrieve it. A plan may scan a table, search an index, join tables in a particular order, or sort rows. EXPLAIN asks the database to show that chosen strategy.

In this guide
  1. What a plan tells you
  2. A useful investigation
  3. Explain is engine-specific
  4. Compare plans with a controlled test
  5. Further reading

What a plan tells you

In SQLite, EXPLAIN QUERY PLAN includes operations such as SCAN and SEARCH, and can show which index is used. A SCAN is not automatically bad: it can be best for a small table or a query that needs most rows. PostgreSQL's EXPLAIN shows estimated costs and row counts; EXPLAIN ANALYZE runs the query and adds actual measurements.

A useful investigation

Start with the exact slow query and representative data. Check whether the plan reads far more rows than expected, repeats an expensive operation, sorts a large result, or fails to use an index that should help. Compare estimated and actual row counts where the engine exposes both. A large difference can mislead later planner choices.

Then change one thing—an index, predicate, or query shape—and compare plans and timing again. Keep in mind that timings vary with cache state and workload. Do not add an index just because one plan says SCAN; table size, selectivity, and write cost matter.

For example, an index on orders(customer_id) may help a selective lookup but not a query that returns nearly every order. See indexes and composite indexes.

Explain is engine-specific

Plan output is not a stable cross-database format. SQLite's query-plan output is a high-level description, not an actual row-by-row execution profile. PostgreSQL's EXPLAIN ANALYZE executes the statement, so use care with statements that modify data. Consult the target engine and version.

Compare plans with a controlled test

Use the same database state and query parameters when comparing plans. Record the selected index, actual row counts and timing where available, then vary one factor. A plan for a tiny development table may differ from production because statistics and selectivity differ. For PostgreSQL, EXPLAIN ANALYZE executes the statement, so wrap a data-changing experiment in a transaction and roll it back if appropriate. For SQLite, use EXPLAIN QUERY PLAN to inspect strategy, then measure execution separately; it does not report actual per-step timing.

Further reading

Check yourself

0 of 3

Answer without scrolling back up.

  1. In a plan, what does SCAN tell you?

  2. Why does `WHERE customer_id + 0 = 42` stop using an index on customer_id?

  3. When is a full scan the better plan?

Cheat sheet

EXPLAIN and Query Plans

Run EXPLAIN on a slow query, read the plan node by node, and spot the sequential scan or nested loop that accounts for the missing time.

DATABASE · vizlearn.in/database/explain_and_query_plans.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.

Teaching a course with this? Individual teachers can use it freely; for a whole department or programme, see institutional licences.