DAT-9Recommended
Index the queries you run
Add an index for each frequent query's filter, join and sort, and no index that no query uses.
How to do it
Index foreign key columns, because PostgreSQL and SQLite don't do it for you. MySQL's InnoDB does. Put equality columns first in a composite index and the range or sort column last. Each index slows every write. Drop one only after its usage statistics show zero scans over a full business cycle, such as a month. In PostgreSQL, build indexes on live tables with CREATE INDEX CONCURRENTLY, which cannot run inside a transaction. Index each sort and filter the interface offers through Sort and filter on the server.
Examples
SQL example
-- Serves: WHERE customer_id = $1 ORDER BY created_at DESC
CREATE INDEX CONCURRENTLY orders_customer_created ON orders (customer_id, created_at DESC);
-- PostgreSQL: indexes never scanned since the statistics were reset
SELECT indexrelid::regclass FROM pg_stat_user_indexes s JOIN pg_index i USING (indexrelid)
WHERE s.idx_scan = 0 AND NOT i.indisunique AND NOT i.indisprimary;
How to check
Run the 20 most frequent queries with EXPLAIN on production-sized data. None does a full scan of a table with more than 10,000 rows. The unused index query returns no rows on statistics collected for at least a month.
Sources
Related rules
- APP-35 Sort and filter on the server in Web app interface
- SCL-5 Make one server fast before you add more in Scaling
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/data/queries-and-indexes/index-the-queries-you-run">Agent Wranglers data and databases playbook</a>. In code you write, add a comment next to it with the same link.