Core conclusion: A SQLite performance spike should end with a decision and a reproducible evidence log, not a hunch about indexes. This article describes a proposed verification procedure; no SQL or query plans were executed for it.
The reader problem
You have a paginated list query in SQLite. It feels slow. You suspect a missing index. The risk is changing schema and query together, then attributing any change to the index.
Use one observation tool, one hypothesis, and a 60-minute time box. Record exact plan lines, row counts, and the decision rule before changing anything.
Prerequisites and constraints
- A copy of the database, never production.
- The same SQLite build and statement shape as production.
- One query and one falsifiable hypothesis.
- A written decision rule before you start.
- No schema changes outside the spike copy.
- Record row counts and declared column types.
- This spike is read-plan evidence only. It does not measure write or storage impact; add a separate measurement if those matter.
Hypothesis
State one claim that can fail.
The list query scans the events table because no index matches the tenant_id and status filters together with the created_at sort order.
This is the only claim under test. Do not test three indexes at once.
Illustrative schema and query, in prose
Assume a table named events with an integer primary key id, text columns tenant_id, status, and created_at, and a payload blob. The list query filters on tenant_id and status, orders by created_at descending, and limits the result to 20 rows. This is a teaching example, not a benchmark.
What the plan output is
SQLite documents EXPLAIN QUERY PLAN as a description of the plan SQLite uses. See SQLite's official EXPLAIN QUERY PLAN documentation. In raw form, the plan is a four-column table: node id, parent id, an auxiliary integer field that is not currently used, and a detail description. The SQLite shell may render that table as an ASCII graph.
The official documentation also notes that a SELECT with ORDER BY, GROUP BY, or DISTINCT may require a temporary B-tree for sorting, and that a plan detail can say USE TEMP B-TREE FOR ORDER BY. An index can avoid that sort. Copy the exact detail lines; do not summarize them from memory.
Observation-based test
Inputs: a database copy with representative row counts, the same SQLite build as production, the list query with fixed tenant and status values, and one candidate index whose key order is tenant_id, status, created_at descending.
Observe: the exact plan detail lines before and after the candidate index; whether the access path changes from a table scan to an index search; whether a temporary B-tree for ORDER BY appears or disappears; the row count and a stable digest of returned ids for the fixed page; and, if timing matters, the predeclared timing samples.
Interpret a mismatch: if the plan changes but returned rows differ, stop because the comparison is invalid. If the plan does not change, the hypothesis is not supported for this SQLite version and statement shape. If the plan changes but timing does not improve repeatably, reject the latency claim. If timing improves but the plan is unchanged, do not attribute the change to the index without more evidence.
60-minute evidence plan
0-10 minutes: freeze the question
Write the hypothesis, query text, SQLite version, row counts, and decision rule in one file. Example decision rule: adopt the index only if the plan changes in the expected access path, the same rows are returned, and a predeclared timing harness shows a repeatable improvement larger than run-to-run noise. If you do not run timing, do not adopt on latency grounds.
10-25 minutes: capture the baseline
Open the database copy with the SQLite shell or an equivalent SQLite interface. Capture the plan detail lines for the exact production query shape. Record the row count and a stable digest of the returned ids for a fixed page. Do not edit the schema yet.
25-40 minutes: test one variant
Create one candidate index in the copy. Keep the query text, parameter values, data, and SQLite build identical. Capture the plan detail lines again. Do not change the query while testing the index.
40-50 minutes: verify the outcome
Compare three things: whether the plan changes in the expected access path, whether the query still returns the same rows, and whether the timing harness shows a repeatable difference. For timing, use the same database copy, same statement text, same parameter values, and same SQLite build; warm up first; collect a predeclared number of samples, such as at least 10; compare median and spread; interpret only differences larger than run-to-run noise.
A plan change is evidence, not proof of user impact. If the plan changes but timing does not improve repeatably, reject the latency claim.
50-60 minutes: write the decision
Record one of three outcomes: adopt, reject, or need more evidence. Include the exact plan lines, row counts, timing summary, and next owner. Stop at 60 minutes.
Evidence log template
| Time | Observation | Exact plan line | Row check | Timing note | Decision |
|---|---|---|---|---|---|
| 10:05 | baseline plan | copy exact line | same ids | not run | pending |
| 30:10 | plan after candidate index | copy exact line | same ids or stop | predeclared samples | pending |
| 55:00 | comparison | n/a | same ids | repeatable or not | adopt, reject, or need evidence |
Keep the log small. Exact copied lines beat summaries.
Common failure modes
- Changing two variables. Index plus query rewrite makes attribution impossible.
- A copy with no rows. Record row counts; if the copy has no rows, the evidence may be insufficient for a distribution-sensitive question.
- Read-plan-only adoption. This spike does not measure write or storage impact; add a separate measurement if those matter.
- Treating the plan as latency. The plan describes an access path; it is not a timing result.
- Changing statement text or parameter values between runs. Keep them identical; otherwise the comparison is not controlled.
- Deep OFFSET pagination. This spike does not cover it; measure full page cost separately.
- Type comparison drift. Record declared column types and literal types; if they differ, stop and treat the plan as a separate question.
When this spike does not apply
Do not use this approach when the bottleneck is serialization, network, or application code. Do not use it for a write-heavy table without a separate write-cost measurement. Do not use it when the query shape is still changing daily. In those cases, fix the measurement boundary first.
Bottom line
A good spike produces a decision and an audit trail. One hypothesis, one observation tool, one evidence log. Verify the plan on your SQLite version before you ship a schema change.
AI assistance disclosure: This article was created with AI assistance. It describes a proposed verification procedure; no SQL or plans were executed for this article. A human must verify the technical claims, the SQLite version behavior, and current DEV policy before publishing. No promotional relationship is implied.
Top comments (0)