r/Database • u/ClickOk5811 • 19d ago
Composite index column order: four queries, four query plans (tested on SQLite, check your own engine)
A composite index on (customer_id, status) is not the same as one on (status, customer_id). Instead of explaining it abstractly, here’s a small test. I used SQLite 3.53.1 because it’s easy to reproduce, with a table of 20,000 rows, about 500 distinct customers and 3 statuses:
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INT,
status TEXT,
created_at TEXT
);
CREATE INDEX idx_cust_status ON orders (customer_id, status);
Then EXPLAIN QUERY PLAN for four queries:
WHERE customer_id = 7
-> SEARCH orders USING INDEX idx_cust_status (customer_id=?)
WHERE customer_id = 7 AND status = 'paid'
-> SEARCH orders USING INDEX idx_cust_status (customer_id=? AND status=?)
WHERE status = 'paid'
-> SCAN orders
WHERE customer_id > 7 AND status = 'paid'
-> SEARCH orders USING INDEX idx_cust_status (customer_id>?)
What it shows:
- The index helps when the query constrains the leading column(s). Filtering on
customer_idalone uses it. - Filtering on only the second column (
status) falls back to a full scan, because the index is sorted bycustomer_idfirst. - With a range on the first column (
customer_id > 7), the index narrows by that range, butstatuscan’t narrow further in the index; the engine still has to check it row by row among the matches.
I also ran ANALYZE and re-checked the status-only query. It was still a scan in this case. I think that’s because the leading column has many distinct values here, so skipping through the index wouldn’t help, but I’m not sure of the exact reason, and some engines have features (skip scan, loose index scan) that may change the picture for other data shapes.
Rule of thumb people often give: put equality-filtered columns first and the range column after them, then think about which queries you actually run. It’s a heuristic, not a law. Selectivity, sort order (ORDER BY) and covering needs all matter too.
Limits of this test:
- One engine, one version, one tiny dataset. Plans depend on statistics, versions and settings, so run
EXPLAINon your own database with realistic data. - Index choice also affects write cost and storage, which I didn’t test.
How do you decide on column order for composite indexes: by query patterns, by selectivity, or by something else? And have you been surprised by a plan that ignored an index you expected it to use?
2
u/Zestyclose-Turn-3576 19d ago
> WHERE status = 'paid'
Since most orders are probably paid, I would hope it would not be using an index
> WHERE customer_id > 7 AND status = 'paid'
Strange to have a `>` predicate on a key column like that.
1
u/Aggressive_Ad_5454 19d ago
Read this. https://use-the-index-luke.com/ and this https://sqlite.org/optoverview.html
Your analysis is correct for BTREE indexing.
1
u/Muted_Jellyfish_6784 19d ago
Nice clear test. The case people miss is range predicates: with (customer_id, status), a query on customer_id equality plus a created_at range cannot use the index for the range, so (customer_id, created_at) or a three-column index is worth testing for that pattern. A covering index example would round it out, where the plan skips the table lookup entirely
The useful habit is driving index choices from the actual query log rather than intuition. Pull the top queries by frequency and total time, group them by their WHERE and ORDER BY columns, and design for the few patterns that cover most of the load
1
u/Discordbothost 19d ago
what did you SELECT in the status-only test? selecting just customer_id/status lets this existing index cover the query; SELECT * needs the other columns too. the projection can change the plan even when the WHERE stays identical.