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:
- 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.
- 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
| Plan Term | What the Engine Does | Performance Impact |
|---|---|---|
| SCAN table | Reads every page and row in table from disk | Slow linear scan: dangerous on large tables |
| SEARCH table USING INDEX | Traverses B-tree index directly to matching keys | Fast logarithmic lookup: sub-millisecond |
| USING COVERING INDEX | All needed columns live in the index; table unread | Fastest retrieval: zero table row lookups |
| USE TEMP B-TREE FOR ORDER BY | Builds temporary in-memory B-tree to sort unindexed rows | High CPU and RAM overhead on large queries |
Sequential scan versus index
SELECT * FROM records WHERE key = 42;
SCAN · 0 / 8 row checks
INDEX · 0 / 3 steps
20 | 40 | 60
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.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.