Skip to content

What is a database index?

Updated Nov 30, 20263 min readBased on official documentation
What is a database index?

In short

A database index is a separate structure that helps the database find rows quickly. Instead of reading every row in a table, the database follows the index to the matching rows. Indexes speed up reads but add some work to writes.

The problem indexes solve

Imagine a table with millions of rows and a query that looks up one row by its id. Without any preparation, the database has to scan the whole table, row by row, to find every match. When only one or two rows match, that is a lot of wasted reading.

An index fixes this. It is an extra data structure that the database keeps alongside the table. It stores the values of one or more columns in a sorted, searchable form, so the database can jump straight to the rows it needs. The documentation describes this as walking a few levels down a search tree instead of reading everything.

A useful comparison is the alphabetical index at the back of a non-fiction book. Readers look up a term there, find the page numbers, and go straight to them. They do not need to read the whole book. A database programmer plays the role of the book's author here: they must guess which lookups the application will make often enough to deserve an index.

How it works

You create an index on one or more columns of a table. Suppose you have a simple table:

CREATE TABLE test1 (id integer, content varchar);

If your application often runs queries that filter by id, you can add an index like this:

CREATE INDEX test1_id_index ON test1 (id);

The name is up to you. Pick one that tells you later what the index is for. If you need to remove an index, use DROP INDEX. You can add and remove indexes on a table at any time.

Once the index exists, the database keeps it up to date on its own whenever the table changes. When you run a query, the query planner decides whether using the index is cheaper than a full table scan. The planner is the part of the database that chooses how to run each query.

The planner relies on statistics about the data. These statistics can get stale as the table changes. Running the ANALYZE command refreshes them, and you may need to do this regularly so the planner makes good choices.

Where indexes help

Indexes are not only for simple lookups. They can also speed up UPDATE and DELETE statements that have search conditions. They can help joins too. If a column appears in a join condition, an index on that column can make joined queries much faster.

In general, an index can help a query when a WHERE or JOIN clause compares an indexed column to a value using an operator the index supports. The value side can be any expression that does not change during the query and does not read from the indexed table. The planner can also rearrange some comparisons, such as putting the value on the left side, so the index can still be used.

The costs

Indexes are not free. Building one on a large table can take a long time. By default, PostgreSQL lets SELECT statements run on the table during the build, but it blocks INSERT, UPDATE and DELETE until the build finishes. In production systems that wait is often unacceptable. The documentation describes a way to allow writes during the build, but it comes with several caveats, so read the details before using it.

Every index also adds work. The database must update it on each write, which slows down data changes. Indexes can also stop the database from using certain optimizations for updates. For these reasons, an index that is rarely or never used in queries should be removed.

Common mistakes

The most common mistake is adding indexes on every column just in case. Each one costs write time and storage. Start from the queries your application actually runs, and add indexes that match them.

Another mistake is forgetting about statistics. If queries suddenly slow down after a large data change, running ANALYZE is a good first step.

Finally, avoid vague index names. A name like idx1 tells you nothing a year later. A name like test1_id_index shows the table and the column.

Alternatives and next steps

Indexes are not the only way to speed up queries. Better query design, fewer returned columns, and caching can all help. Sometimes the right fix is to change the query rather than add an index. Once you know the basics, you can explore how to check whether a query actually uses an index, which the PostgreSQL documentation covers in its chapter on the query planner.

FAQ

Do indexes make every query faster?

No. An index helps when the query can use it and when using it is cheaper than a full table scan. For queries that return many rows, the planner may still choose a scan. Indexes also slow down writes.

Do I need to rebuild an index after changing a table?

Usually not. The database updates the index automatically when the table is modified. You may still need to run ANALYZE so the planner has up-to-date statistics to decide whether to use the index.

Can I add an index without stopping writes?

By default, PostgreSQL blocks writes while an index is being built, but reads continue. The documentation describes a way to allow writes during the build, though it has several caveats you should review first.

How do I know if an unused index should be removed?

If an index is seldom or never used by your queries, it is a candidate for removal. You can remove it with the DROP INDEX command. Check your query behavior first, since removing an index is easy to do but may affect performance.

Sources

This page is based on the official documentation below. Read it for full detail.

Database index — official documentation ↗
blozhikDeveloper news from across the web, explained. Every story links to its original sources.