Check A SQLite Query Plan Before Adding An Index
A task list gets slower as its local database grows. Adding an index on updated_at seems obvious because the screen sorts by that column. But the query also filters by project and completion state, and its actual plan may be doing more work than the sort suggests.
Start with the exact query the screen runs, including its limit and tie-breaker.
Reproduce the query shape
This example shows the newest 50 open tasks for one project:
SELECT id, title
FROM tasks
WHERE project_id = ? AND completed = 0
ORDER BY updated_at DESC, id DESC
LIMIT 50;
The ID provides a deterministic order when timestamps match. Without a tie-breaker, an index change can expose a different ordering among equal timestamps even though both results satisfy the SQL you wrote.
Use bound parameters for values in the app. Run experiments on a synthetic fixture or a disposable copy, not by casually changing an active user’s database.
Our fixture creates 20,000 tasks across 100 projects, with a mixture of open and completed rows and repeated timestamps. That is enough to demonstrate a plan difference without pretending to reproduce every production distribution.
Inspect before changing the schema
Prefix the query with EXPLAIN QUERY PLAN. SQLite’s plan documentation explains the high-level output, including scans, constrained searches, and temporary sorting structures.
On the fixture, before adding an index, the plan included a task-table scan and a temporary B-tree for ordering. Those observations tell us what to investigate; they do not by themselves measure wall-clock latency.
A full scan is not always a bug. Reading most of a small table can be a reasonable choice. The useful question is whether the plan fits the actual number of matching rows and the work the screen needs.
Test a compound index against the whole query
For this query, try:
CREATE INDEX tasks_project_open_recent
ON tasks(project_id, completed, updated_at DESC, id DESC);
ANALYZE;
The leading columns match equality filters. The remaining columns match the requested order inside that filtered range. SQLite’s query-planning guide explains how multicolumn indexes support combined searching and sorting.
After adding this index and analyzing the synthetic data, the fixture’s plan used tasks_project_open_recent for the project and completion constraints and no longer reported the temporary order-by structure.
The query also returns title, which is not in this index. Do not call it a covering index for this SELECT. Adding title would change the storage and write tradeoff, and a long mutable title may be a poor addition merely to avoid a table lookup for 50 rows.
Verify results before celebrating the plan
The fixture runs the SELECT before and after the schema change and compares the complete ordered result. Both runs return the same 50 IDs and titles.
That check is deliberately separate from the diagnostic plan output. SQLite warns that EXPLAIN QUERY PLAN text can change between releases. It is useful for interactive investigation, but your production behavior should not depend on parsing its wording.
A regression test can assert the application’s required result and measure a representative workload. If you record plan output for review, record the SQLite version with it and treat formatting changes as diagnostic differences rather than automatic data failures.
Count the writes you just made more expensive
An index has to be maintained when indexed values change. This task index includes completion and update time, so common edits affect it. Measure bulk import, sync application, and frequent task updates as well as reads.
Also inspect existing indexes. A new compound index may overlap one already used by another screen. Remove redundant indexes only after checking the other query shapes and migration requirements; do not drop them from intuition alone.
When the app uses SwiftData or Core Data, work through the framework’s supported modeling and migration mechanisms. This example concerns an app-owned SQLite schema. It is not permission to alter a framework-managed store behind its back.
September 2026 review: the Python SQLite fixture ran on SQLite 3.54.0, preserved the exact ordered results, and showed the scan/sort-to-index-search change described above. It does not establish a latency win on the SQLite version bundled with your target iOS release; repeat the experiment there with representative data.