Indexing is the process of creating a data structure—called an index—that allows a system to locate specific information quickly without scanning every item. In databases, search engines, and file systems, an index works much like the index at the back of a book: instead of reading every page to find a topic, you look up the term in the index and jump directly to the relevant pages.
What Is an Index and Why It Matters
An index is a separate structure that stores a sorted or hashed representation of selected data fields, along with pointers to the full records. Without an index, finding a record requires a full scan—checking each row or document one by one. For small datasets, that is fine. But as data grows, full scans become prohibitively slow. Indexes trade extra storage space and slower write operations for dramatically faster reads.
Consider a library with a million books. If you want all books by a certain author, scanning every shelf is impractical. A card catalog sorted by author lets you go straight to the right drawer. The catalog is the index; the shelves are the table; the call numbers are the pointers.
How Indexes Are Built and Maintained
Most indexes use one of two core structures: B-trees (and their variants, B+ trees) or hash tables. B-trees keep keys in sorted order, which supports range queries (e.g., “find all orders between January and March”) and exact matches. Hash indexes map keys to locations via a hash function, offering very fast exact-match lookups but no efficient range scans.
When you create an index, the database builds it by reading the existing data, extracting the indexed columns, sorting or hashing them, and storing the result. From then on, every insert, update, or delete to the table must also update the index. This is why indexes slow down writes: each change may require rebalancing a tree or updating multiple index entries.
Search engines like Google use inverted indexes. Instead of mapping a document to its words, an inverted index maps each word to the list of documents containing it (and often the positions within those documents). When you search for “indexing,” the engine looks up that term in the inverted index and retrieves the matching documents instantly, then ranks them.
Types of Indexes and Their Uses
Different systems offer different index types. Here are common ones:
- B-tree index: The default in most relational databases. Good for equality and range queries, and for sorting.
- Hash index: Fast for exact matches only; not useful for range queries or sorting.
- Bitmap index: Efficient for columns with few distinct values (e.g., gender, status flags), often used in data warehousing.
- Full-text index: Designed for searching words within text fields, using inverted indexes and sometimes stemming and stop-word removal.
- Composite index: An index on multiple columns. The order of columns matters: a composite index on (last_name, first_name) can speed up queries filtering by last_name alone, but not by first_name alone.
- Covering index: An index that includes all columns needed by a query, so the database can answer the query from the index alone without touching the table.
How Indexing Works in Practice: A Step-by-Step Example
Imagine a table users with columns id, email, and created_at, containing 10 million rows. A query like SELECT * FROM users WHERE email = 'alice@example.com' would normally scan all 10 million rows. To speed it up, you create an index on email.
- The database reads all existing rows and extracts the
emailvalues. - It sorts those values and builds a B-tree structure where each leaf node contains an email and a pointer to the corresponding row.
- When the query runs, the database traverses the B-tree from root to leaf, finds the matching email in a few steps (logarithmic time), follows the pointer, and returns the row.
- Later, if a new user is inserted, the database inserts the new email into the B-tree, possibly splitting nodes to keep the tree balanced.
The result: the query that once took seconds now takes milliseconds, at the cost of some extra disk space and a slight slowdown on inserts.
Choosing the Right Columns to Index
Indexes are most effective on columns that are frequently used in WHERE clauses, JOIN conditions, and ORDER BY clauses. Columns with high selectivity—many unique values—are better candidates than columns with low selectivity, such as a boolean flag, because an index on a low-selectivity column may not narrow the search enough to be worth the overhead. However, low-selectivity columns can still benefit from bitmap indexes in analytical workloads.
Also consider write patterns: tables that receive many inserts or updates may suffer from too many indexes. A common guideline is to index the columns you query most, and avoid indexing columns that are rarely filtered or that change constantly.
Common Problems and Misconceptions
A frequent misconception is that adding an index always makes queries faster. In reality, the query optimizer may choose not to use an index if it estimates that a full scan is cheaper—for example, when a query returns a large fraction of the table. Indexes also become fragmented over time due to inserts and deletes, which can degrade performance; periodic rebuilding or reorganizing can help.
Another issue is over-indexing: each index consumes disk space and slows down writes. For a table with many indexes, a single insert might update half a dozen index structures. Monitoring index usage and removing unused indexes is a standard maintenance task.
Finally, indexes do not help with queries that use functions on the indexed column, such as WHERE YEAR(created_at) = 2024, unless a function-based index is created. Rewriting the query to compare the column directly (e.g., a range on created_at) allows the index to be used.
Frequently Asked Questions
Does indexing slow down writes?
Yes, generally. Every insert, update, or delete must also update the relevant indexes, which adds overhead. The trade-off is usually worthwhile for read-heavy workloads but may be costly for write-heavy ones.
Can I have too many indexes?
Yes. Each index consumes storage and adds write overhead. It is best to index only the columns that are frequently used in queries and to periodically review and remove unused indexes.
How does a search engine index a web page?
A search engine crawls the page, extracts its text and links, and adds the words to an inverted index that maps each word to the pages containing it. It also stores other signals for ranking, but the core lookup relies on the inverted index.
What is the difference between a clustered and a non-clustered index?
A clustered index determines the physical order of rows in the table; a table can have only one. A non-clustered index is a separate structure with pointers to the rows. Both speed up queries, but clustered indexes are often faster for range scans because the data is stored in order.
Do indexes work for text search?
Standard B-tree indexes are not designed for searching words within text. Full-text indexes, which use inverted indexes and additional processing like stemming, are needed for efficient text search.
How do I know if an index is being used?
Most database systems provide tools to examine query execution plans, which show whether an index was used. They also track index usage statistics that can reveal unused indexes.