Skip to content
Back to all posts
Technologydatabase indexing strategies

Database Indexes Explained: B-trees, Composite Keys, and Trade-offs

Understand how database indexes locate rows, why B-trees support range queries, how composite key order matters, and when an index can make writes slower.

3 min readUpdated May 3, 2026AI-assisted

TechSilo

AI-assisted and human-curated

A database index is an additional data structure that helps the database find rows without scanning every row in a table. It can make a query much faster, but it also consumes storage and must be maintained when data changes.

The important question is not "Should every column have an index?" It is "Which access patterns justify the cost of maintaining this structure?"

What an index stores

A typical B-tree index stores ordered key values and references to the corresponding table rows or pages. Because the keys are ordered, the database can navigate the tree to locate a value or a range without reading every row.

That makes B-tree indexes useful for equality filters, range conditions, and ordered retrieval. Other index types serve different workloads: hash indexes are designed for equality lookups, while database-specific structures such as PostgreSQL GIN or GiST support other kinds of search.

The exact implementation and available index types depend on the database engine.

Choose indexes from real query patterns

Suppose an orders table is commonly queried by customer and then sorted by creation time:

sql
SELECT id, created_at, total
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;

A composite index on (customer_id, created_at) may help this access pattern because the database can first locate a customer's entries and then read them in date order. The best definition depends on the engine and query plan; for PostgreSQL, a descending key can be specified explicitly:

sql
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at DESC);

A composite index is not just a bag of columns. Key order matters. An index beginning with customer_id is usually more useful for queries filtering by customer than one beginning with created_at. Some engines can use a leftmost prefix of a composite index, but the details differ.

Selectivity is useful, but not the whole decision

Selectivity describes how narrowly a condition filters the table. An index on a highly selective column can be attractive, but a low-cardinality column is not automatically useless. The planner may still use an index depending on table size, data distribution, correlation, combined conditions, and the cost of fetching table rows.

For example, an index on status may not help if almost every row has the same status. A composite or partial index may fit a specific query better, but measure it rather than guessing.

Indexes have a write and storage cost

Every insert, delete, and relevant update may require index maintenance. Extra indexes increase storage, can slow write-heavy workloads, and create more work for vacuuming or maintenance operations in some engines.

Avoid creating indexes for every column mentioned in a query. Start with frequent, expensive queries, then evaluate the proposed index against the query plan and representative data.

A practical way to test an index

1. Capture the slow query and the parameters used in production.

2. Inspect the query plan before changing the schema.

3. Add one candidate index in a safe environment.

4. Compare execution time, rows read, buffers, and write overhead.

5. Check whether other important queries become slower or more expensive.

6. Keep the index only if the measured benefit justifies its ongoing cost.

For PostgreSQL, EXPLAIN (ANALYZE, BUFFERS) can show actual execution details for a read query. Remember that EXPLAIN ANALYZE executes the statement; do not casually run it on data-changing statements in production.

Indexes are workload-specific. A good index is one that supports a measured access pattern without creating more maintenance cost than the query improvement is worth.

Want to put this into practice?

Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.

Related blog posts