What Is a Database Index? Speed Up Lookups While Managing the Cost
A database index is a separately maintained search structure that helps a database find rows matching a condition. Like the index at the back of a book, it can point toward candidate locations instead of requiring a scan of all the data. But an index consumes storage and must be updated when rows change. Choose indexes for real query and write patterns, then inspect query plans to see whether they help.
How does an index help a lookup?
Suppose an orders table contains millions of rows and an application needs one order by its identifier. Without an index, the database may need to inspect many rows. With an index on order_id, it can search the structure for the value and then read the corresponding table row. The index is a search aid, not necessarily a second complete copy of the answer; the database chooses the access method for each query.
CREATE INDEX orders_customer_id_idx
ON orders (customer_id);
SELECT order_id, order_date, status
FROM orders
WHERE customer_id = 42;In PostgreSQL, CREATE INDEX creates a B-tree by default. B-trees support equality tests and range conditions for sortable values. Creating an index does not guarantee that every query will use it. If a query needs most rows, reading the table sequentially may cost less. The planner compares estimated costs and selects a plan. This SQL is illustrative and was not run.
Which queries and columns should you examine first?
Start with queries that run often and matter to response time. Conditions in WHERE, join keys, and predicates used to select rows for UPDATE or DELETE can be candidates. Yet “this column is queried” is not enough reason to index it: a small table or a query returning most of a table may be served more efficiently by a sequential scan.
Consider selectivity: roughly, how much of the table a condition filters out. If many rows share a value, that value alone may not narrow the search much. A frequently searched value that matches only a few rows may make an index more useful. The outcome still depends on data distribution, query shape, table size, and hardware, so verify the plan rather than treating selectivity as a guarantee.
What does an index cost?
Indexes use storage in addition to table data. When rows are inserted, updated, or deleted, the database also has to maintain relevant indexes, which can add write work. Many indexes can therefore burden writes and operational tasks such as backup and recovery even when they help some reads. Review usage and query plans periodically; an unused index may impose cost without helping workloads.
Index types support different operators and data. PostgreSQL offers B-tree, Hash, GiST, SP-GiST, GIN, and BRIN. B-tree is a broad choice for ordinary comparisons and ranges; GIN can suit searches for a component in a value containing multiple components, such as an array. Select the index type and operator class to match the operation. This article does not attempt a full comparison of index types.
A practical way to create and assess an index
Identify a real slow or important query and its `WHERE`, `JOIN`, and ordering conditions.
Choose the column or expression to index and consider the expected search condition and selectivity.
After adding an index, use PostgreSQL `EXPLAIN` to see whether the planner chose a sequential, index, or bitmap scan. The plan includes estimated rows and costs; a cost estimate is not elapsed time.
Compare plans and performance with representative data and conditions. Include write performance and index size in the decision. No query or benchmark was run for this article.
On PostgreSQL, creating a regular index on a large table can block writes by default, even though reads may continue. Concurrent index creation has different restrictions. Check documentation for your version and prepare an operational window and recovery plan.
Review index use and query statistics regularly and decide whether indexes that no longer serve queries should be removed.
Sources and scope
[PostgreSQL 18: Indexes](https://www.postgresql.org/docs/18/indexes.html)
[PostgreSQL 18: Introduction to Indexes and maintenance cost](https://www.postgresql.org/docs/18/indexes-intro.html)
[PostgreSQL 18: Index Types](https://www.postgresql.org/docs/18/indexes-types.html)
[PostgreSQL 18: Using EXPLAIN](https://www.postgresql.org/docs/18/using-explain.html)
The PostgreSQL 18 documentation was checked on 2026-11-22. The default B-tree, planner behavior, index types, and index-build notes are PostgreSQL-specific. Other database products and versions may differ. The SQL is illustrative; it was not executed and no benchmark is claimed.