Results.

SCEPTRA.TECH

Read a SQLite Query Plan Before Adding Another Index

SQLite workflow: inspect access, inspect sorting, then verify results and timing

A slow SQLite query is a reason to investigate, not a reason to add an index to every column. I would first ask the database how it plans to find and order the rows. EXPLAIN QUERY PLAN answers that question at a useful level, but it does not measure how long the query actually takes.

Start with one real question

Imagine an application that lists the newest open tickets. The example below assumes a tickets table with title, status and created_at columns. Adapt it to a development copy of your database rather than changing a production schema while troubleshooting.

EXPLAIN QUERY PLAN
SELECT title, created_at
FROM tickets
WHERE status = 'open'
ORDER BY created_at DESC
LIMIT 25;

Keep the query, parameter values, existing indexes and SQLite version together in your notes. Those details make a later comparison meaningful. This is an illustrative workflow, not a benchmark or a claim that a particular plan will appear on your database.

Read the access line before the index name

SQLite documents two important labels in the plan's detail field:

  • SCAN: the access goes through all records in the relevant scan. This can include walking an index in order, so SCAN does not automatically mean that no index is involved.
  • SEARCH: only a subset of rows is visited. The detail can name an index and show which conditions narrow the search.

Do not turn those labels into a pass-or-fail score. Reading a small table in full can be reasonable. A SEARCH that matches most of a large table can still involve plenty of work. Read the table name and the conditions alongside the label.

If the detail mentions a covering index, SQLite can obtain the information needed by that query from the index without consulting the original table for those values. That is useful context, not proof that the query meets your application's latency target.

Look separately for sorting work

A line such as USE TEMP B-TREE FOR ORDER BY tells you that SQLite plans a temporary sorting structure. Similar lines can appear for GROUP BY or DISTINCT. This is a clue about an additional operation, not a corruption warning.

For the ticket query, there are two separate needs: find rows with the selected status, then return them in date order. SQLite's query-planning guide explains how a multi-column index can support searching and sorting together. On a disposable development copy, one candidate to evaluate is:

CREATE INDEX tickets_status_created
ON tickets(status, created_at);

That statement changes the database schema. Do not run it merely to make the plan look tidier. First inspect the indexes you already have and follow your project's migration process. Then rerun the same EXPLAIN QUERY PLAN statement and compare the access and sorting lines. Whether SQLite chooses the candidate depends on the actual query and database.

A changed plan still needs a timing check

After inspecting the plan, measure the original SELECT separately on representative data. Check that it returns the same intended rows and order, and compare repeated runs under similar conditions. Record observations instead of promising a fixed speedup from an index name.

SQLite also documents richer query profiling, including rows, loops and cycles. Its shell profiling support requires a build compiled with SQLITE_ENABLE_STMT_SCANSTATUS. Do not assume every installed SQLite shell supports .scanstats on.

Keep the plan as a debugging aid

SQLite explicitly warns that EXPLAIN QUERY PLAN output can change between releases. Read it interactively; do not build application logic that parses its display strings. Plain EXPLAIN is different again: it shows virtual-machine instructions rather than this high-level plan.

My stopping rule would be simple: keep a proposed index only after a reproducible workload comparison supports it, and review the schema change before deployment. The plan explains the strategy. Your measurements tell you whether that strategy helped the work you actually do.

Sources

Comments