1.When a database index is the right tool
A database index is the right tool whenever the question is structured: exact values, ranges, ordering, and combinations of those, over data you also need to write transactionally. "Orders for this customer, placed in the last 30 days, status pending, newest first" is a structured question, and a composite index answers it with a single range scan that stays fast as the table grows. The database is also the only place that can answer such a question in the same transaction as a write, which matters whenever the answer drives a decision that has to be consistent with what is stored — checking remaining stock, computing a balance, validating a constraint. Beyond the ordinary B-tree, most relational databases carry index types that cover more than people expect: an inverted index type for array containment and JSON keys, a trigram index for substring and fuzzy matching, a partial index that indexes only the rows a hot query actually touches, and built-in full-text search with stemming and ranking. That built-in full-text search is the important middle ground — for many products it is genuinely sufficient, and adopting a separate search engine before exhausting it buys an extra system to run, sync, monitor and reindex in exchange for capabilities the database already had. The rule is that structured filtering and transactional reads stay in the database, and the next concept covers what genuinely justifies leaving it.
Key features
- A B-tree index answers exact-match, range and ordering questions, and a composite index answers combinations of them in one scan
- The database is the only place that can answer a query in the same transaction as a write, which is required whenever the answer drives a consistent decision
- Inverted index types cover array containment and JSON key lookups — structured questions people often assume need a search engine
- Trigram indexes support substring and fuzzy matching, including leading wildcards that a plain B-tree cannot serve
- Built-in full-text search with stemming and ranking is sufficient for many products, and exhausting it first avoids an extra system to run, sync and reindex
- Structured filtering and transactional reads stay in the database even in a system that also runs a search engine
Index types and the question shape each one answers
- Exact match — B-tree
- Range and order — B-tree, composite
- Array / JSON containment — inverted index
- Substring, fuzzy — trigram
- Stemmed text with ranking — built-in full-text search
Command
-- equality columns first, then the sort column
CREATE INDEX orders_customer_status_created
ON orders (customer_id, status, created_at DESC);
-- answered by one contiguous range scan
SELECT * FROM orders
WHERE customer_id = $1
AND status = 'pending'
ORDER BY created_at DESC
LIMIT 20;
-- and readable inside the same transaction as a
-- write, which no external index can offerA database index is the right tool for structured questions — exact values, ranges, ordering and their combinations — and it is the only tool that can answer inside the same transaction as a write. Composite indexes put equality columns before the range or sort column. Beyond B-trees, inverted, trigram, partial and built-in full-text indexes cover far more than people assume, and exhausting the built-in full-text search first avoids adopting a whole second system for capabilities the database already had.