Stop guessing, ask the engine
Prefix any query with EXPLAIN QUERY PLAN and SQLite tells you what it actually intends to do. The output is a tree of operations; the words to look for:
- SCAN — full table scan. Cheap on tiny tables, expensive on large ones.
- SEARCH — index lookup (good).
- USING INDEX X — which index was picked.
- USING COVERING INDEX X — every column the query needs is in the index, so SQLite never touches the actual table (excellent — covered next lesson).
- USE TEMP B-TREE FOR ORDER BY — SQLite couldn't use an index for sorting; it built a temporary one. Often a sign you're missing a sorted index.
Tip: EXPLAIN QUERY PLAN should be muscle memory. Before optimizing, explain. After optimizing, explain again to confirm the plan changed. A 'fix' that doesn't change the plan didn't fix anything.