- Introduced by
- Rudolf Bayer and Edward M. McCreight
- Year
- 1972
- Type
- Database optimization technique
- Family
- Query Processing & Performance
What does it do?
Indexing strategy is the practice of deciding which database columns to index, which index structures to use, and how to maintain those indexes as data and query patterns change.
The B-tree, described by Bayer and McCreight in 1972, is a widely used index structure for this purpose in relational databases. The strategy itself is a set of design decisions based on observed queries rather than a single algorithm.
The intuition behind the method
An index is an auxiliary data structure that lets the database find matching rows without examining every row in a table. Without a suitable index, the database performs a full table scan, reading each row to test the filter condition. With a B-tree or hash index, the database can instead locate matching rows through a tree traversal or a hash lookup, at the cost of extra storage and extra work on every write. The central question is therefore which columns, and which combinations of columns, are worth indexing for the queries that actually run.
History
Bayer and McCreight's 1972 paper, Organization and maintenance of large ordered indices, introduced the B-tree, a tree structure that keeps its height logarithmic in the number of keys under insertions and deletions. Later index types, including hash indexes for equality lookups and bitmap indexes for low-cardinality columns, were developed for other access patterns. Modern database systems offer several index types and automated tuning or adaptive techniques, but the design decisions described here remain the core of the practice.
When to use it
Use indexing strategy when specific, identifiable queries are slow because they scan large tables, and when their filter or join conditions are selective enough that an index can avoid most of the rows. It fits workloads where the frequently run queries are known or can be collected from the system. It is not the right first step when the slowdown comes from poor query logic, lock contention, or hardware limits, or when the execution plans already use appropriate indexes. Be cautious on tables with very heavy write traffic, where index maintenance may cost more than the reads gain. The approach assumes that the extra write cost is acceptable and that table statistics are current, since the optimizer relies on them to decide whether to use an index.
Limitations and considerations
- Every index must be updated when rows are inserted, updated or deleted, and it occupies storage, so each additional index adds cost to write operations.
- The benefit depends on selectivity: an index on a column where most rows share the same value gives little reduction in the rows read.
- A composite index is useful mainly for queries that filter on its leading column or on a leading run of its columns; queries that filter only on a later column generally cannot use it efficiently.
- The optimizer chooses between an index and a scan using table statistics, so outdated statistics can lead to plans that ignore an index that would help.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- For selective filters and joins, an index lets the database avoid a full table scan and read only the rows that match.
- Index changes are made at the database level, so a query can benefit from a new index without changes to application code, provided the optimizer chooses the index.
- Indexes can be added, changed or dropped as query patterns change, which makes the strategy iterative.
Applications
- Illustrative: order-history lookups that filter by customer and by date range, where a composite index with the customer identifier as its leading column matches the filter.
- Illustrative: lookups by a unique account or record number, where an index on that column replaces a full scan with a direct search.
- Illustrative: queries that retrieve the most recent items written by a given author, where a composite index on author and timestamp supports both the filter and the ordering.
- Illustrative: reporting queries on a date column that select a bounded time range, where a B-tree index on that column supports the range condition.
How it works
How it works
Collect the queries the system runs, noting the columns used in WHERE, JOIN, ORDER BY and GROUP BY clauses and how often each query runs. Frequently executed queries matter more than rare ones.
Examine the execution plans of the slow queries to see which steps are expensive, in particular whether the plan performs a full table scan on a large table.
For each candidate column, estimate its selectivity, meaning how many rows a typical filter on that column keeps. Note which columns tend to appear together in the same filter or join condition.
Select index candidates: single-column indexes for highly selective filters, and composite indexes whose leading column matches the most common filter. For each candidate, choose an index type suited to the access pattern, such as a B-tree for equality and range conditions, a hash index for equality conditions only, or a bitmap index for low-cardinality columns.
Create the candidate indexes, then for each target query compare the execution plan and the run time before and after. Measure insert, update and delete timings on a write load that resembles production, since the added write cost is the price paid for each index. Keep an index only when its read benefit outweighs that maintenance cost. Drop any index that the plans of the target queries never use, and check the plans again after an index rebuild or reorganisation. Any selectivity threshold used to shortlist columns is a rule of thumb and should be checked against the actual data and the resulting plans.
Common pitfalls
- Creating indexes without checking the actual query patterns or the selectivity of the indexed columns.
- Over-indexing a table, which slows writes and adds storage use without a matching gain in read performance.
- Choosing the column order of a composite index without considering which column the frequent queries filter on first.
- Ignoring index fragmentation, which can reduce the efficiency of index access over time; a rebuild or reorganisation may be needed, and the plan should be checked again afterwards.
Frequently asked questions
Should I index every column used in a WHERE clause?
No. Prioritize columns with high selectivity (that eliminate many rows) and columns used frequently. Low-selectivity columns (e.g., gender with 2 values) provide minimal benefit. Use composite indexes for multiple frequently-used columns.
What is a composite index and when should I use it?
A composite index includes multiple columns in a specific order. Use when queries frequently filter or sort by multiple columns together. Example: INDEX(customer_id, order_date) helps queries that filter by customer AND order date.
How do I know if an index is being used?
Use EXPLAIN, EXPLAIN PLAN, or the equivalent command in your database system to show the query execution plan. If the plan shows an index and an index scan or index seek operation, the index is being used. If it shows a table scan on the relevant table, the index is not being used.
What is index fragmentation and why does it matter?
Fragmentation occurs when index pages are not stored contiguously on disk, which can require more I/O operations to read a range of keys. Many database systems provide a maintenance operation that rebuilds or reorganizes an index to restore contiguity; the exact command depends on the product. Its effect is most noticeable for range queries, and the query plan should be checked afterwards.
Related methods
Sources
Bayer, R., & McCreight, E. (1972). Organization and maintenance of large ordered indices. Acta Informatica, 1(3), 173-189.
Open DOI referenceSeltzer, M., & Bostic, K. (1994). An implementation of a log-structured file system for UNIX. Winter USENIX Conference, 307-326.
Open publication referenceGarcia-Molina, H., Ullman, J. D., & Widom, J. (2009). Database Systems: The Complete Book (2nd ed.). Pearson Education.
Open publication reference