runtime
48 / 88

lesson 48 Advanced product analysis · advanced

Query plans & indexes

Inspect how SQLite reads data and uses indexes.

the idea

An index can help SQLite find or order rows efficiently. EXPLAIN QUERY PLAN shows SCAN versus SEARCH and index use. Indexes cost storage and slow writes; the optimizer may still scan a tiny table.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

customers4 columns · 8 rows
idinteger · keynametextcitytextjoined_attext
1AliceChicago2024-01-10
2BenAustin2024-02-12
3CaraSeattle2024-03-05
4DevChicago2024-04-20
5ElenaNULL2024-05-01

showing 5 of 8 rows

orders6 columns · 12 rows
idinteger · keycustomer_idintegerproduct_idintegerquantityintegerstatustextordered_attext
101112paid2025-01-01
102231paid2025-01-02
103121paid2025-01-05
104352pending2025-01-08
105445paid2025-02-01

showing 5 of 12 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
EXPLAIN QUERY PLAN
SELECT name
FROM customers
WHERE city='Chicago';

Watch for: A full scan is not automatically bad; table size and selectivity matter.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Run the filtered lookup. This is the query whose plan you will inspect. Note the city predicate—indexes may later speed this path.

    step-1.sql
    SELECT name
    FROM customers
    WHERE city = 'Chicago';
  2. Read the query plan. EXPLAIN QUERY PLAN names the access strategy (scan, search, and so on). Indexes change which path the engine picks.

    step-2.sql
    EXPLAIN QUERY PLAN
    SELECT name
    FROM customers
    WHERE city = 'Chicago';

challenge 1

Show name for Chicago customers. Run the reference with Explain mode to inspect the plan.

Return nameany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.