Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

Reading a Query Plan: EXPLAIN and Full Table Scans

Understand the idea

How to read an EXPLAIN query plan and spot silent full table scans before production outages.

The idea

When a query runs slowly, guessing which index to add is bad engineering. Database engines have a built-in optimizer that plans out the most efficient way to run your SQL. To see what steps the database plans to take, you look at its using the EXPLAIN QUERY PLAN command.

Reading a query plan is one of the most useful skills in database work. In SQLite, the plan shows two contrasting operations:

  1. SCAN [table]: The database will perform a . It reads every page of the table from disk and inspects every row. On a small test table with 10 rows on your laptop, this takes 0.05ms. On a live table with 1,000,000 rows in production, this causes huge delays, high CPU usage, and slow page loads.
  2. SEARCH [table] USING INDEX [index_name]: The database uses an existing to jump straight to the matching rows in a fraction of a millisecond.

Checking the query plan lets you confirm that your indexes are actually working before pushing code live, and helps you spot slow table scans early.

What changes

Key Query Plan Terms in SQLite
Plan TermWhat the Engine DoesPerformance Impact
SCAN tableReads every page and row in table from diskSlow linear scan: dangerous on large tables
SEARCH table USING INDEXTraverses B-tree index directly to matching keysFast logarithmic lookup: sub-millisecond
USING COVERING INDEXAll needed columns live in the index; table unreadFastest retrieval: zero table row lookups
USE TEMP B-TREE FOR ORDER BYBuilds temporary in-memory B-tree to sort unindexed rowsHigh CPU and RAM overhead on large queries

Sequential scan versus index

SELECT * FROM records WHERE key = 42;

SCAN · 0 / 8 row checks

row 1 · key 10waiting
row 2 · key 25waiting
row 3 · key 15waiting
row 4 · key 60waiting
row 5 · key 35waiting
row 6 · key 42waiting
row 7 · key 5waiting
row 8 · key 70waiting

INDEX · 0 / 3 steps

Root page
20 | 40 | 60
Leaf page · key 42 → row 6
↓ Direct row fetch
row 6 · key 42
Waiting for lookup

Illustrative work count: one scan row per step; one index page or row fetch per step.

Find every row where key = 42. Compare a full scan with a small B-tree index.

01 / 09Sequential scan versus index

The command

Before and after: EXPLAIN QUERY PLAN on products

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

Run the example to see SQLite first show 'SCAN products' (table scan), and then switch to 'SEARCH products USING INDEX' once the index is created.

Guided challenge

Analyze query plan detail

Update the function to return true if the plan uses an index search and false if it performs a scan.

Analyze query plan detail

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

false
true

Hints 0/1

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does 'SCAN products' indicate in an EXPLAIN QUERY PLAN output in SQLite?
    02What term in a SQLite query plan confirms that a B-tree index is actively used to filter rows?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson