Skip to main content

Databases

What is a Database Index?

Definition

A database index is an extra data structure that lets the database locate the rows matching a condition without scanning the entire table.

Also known as: Index, DB index

Database Index in more detail

Without an index, finding rows means reading every one — a sequential scan, fine for a thousand rows and ruinous for fifty million. An index, usually a B-tree, keeps the indexed values in sorted order with pointers to the rows, so a lookup follows a few levels of tree instead. That is the difference between milliseconds and minutes, and it is why the same query can be instant on a developer's laptop and time out in production.

Indexes are not free. Every insert, update and delete must maintain each index on the table, so writes slow down and disk usage grows; an over-indexed table is a real problem, not a theoretical one. The planner also chooses whether to use one, and for a query returning a large fraction of the table it will correctly prefer a scan, because random row lookups cost more than reading sequentially.

A few rules explain most surprises. In a composite index the column order matters: an index on (a, b) serves conditions on a, or on a and b, but not on b alone — the left-most prefix rule. Wrapping an indexed column in a function, as in WHERE lower(email) = ..., prevents its use unless you create a matching expression index. A leading wildcard in LIKE cannot use a plain B-tree. And a covering index that includes every column a query needs lets the database answer from the index alone, never touching the table.

Database Index example

-- Before: 50M rows, no index
EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 12;
  Seq Scan on orders  (actual time=0.02..4210 ms, rows=7)

CREATE INDEX idx_orders_user ON orders (user_id);

  Index Scan using idx_orders_user  (actual time=0.03..0.09 ms)

-- Composite: order matters (left-most prefix rule)
CREATE INDEX idx_orders_user_created ON orders (user_id, created_at);
  WHERE user_id = 12                          uses it
  WHERE user_id = 12 AND created_at > now()   uses it
  WHERE created_at > now()                    does NOT use it

-- Function on the column defeats a plain index
  WHERE lower(email) = 'a@b.com'      -> no index used
CREATE INDEX idx_users_email_lower ON users (lower(email));
  -> now it is used

Database Index — common questions

Why not index every column?
Because every index must be updated on each write and occupies disk and memory. Too many indexes slow down inserts and updates measurably, and unused ones cost that price for nothing.
Why is my index not being used?
Common causes: the column is wrapped in a function, the query would return a large share of the table so a scan is cheaper, the leading column of a composite index is missing from the condition, a LIKE pattern starts with a wildcard, or table statistics are stale.
What is a covering index?
One that contains every column a query needs, so the database answers from the index without reading the table rows at all. In PostgreSQL the INCLUDE clause adds non-key columns for exactly this purpose.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary