Skip to content
IT glossary
DatabasesUpdated December 3, 2026

Database index

Индекс в базе данных

An extra data structure helps a database find matching rows without reading the whole table. It can speed up queries, but it takes up space and adds work when data changes.

Also known as: индекс, B-tree

What an index saves you from

Suppose an orders table holds purchases from millions of customers. Without a useful index, the database may scan the table and check each row to find one customer's orders. An index gives it a way to go straight to a much smaller set of candidates. Think of a book index: looking up a topic tells you where to read instead of making you search every page.

Why B-tree indexes are common

A B-tree is one type of index, not another name for all indexes. It keeps keys in order. The database follows the tree to the first relevant key, then scans nearby entries until it has enough results or reaches the end of the matching range. This works for exact lookups and ranges, and the key order can sometimes satisfy ORDER BY without an extra sort. In PostgreSQL, index entries point to table rows. If the index contains all the columns the query needs, an index-only scan is possible, though PostgreSQL may still need to check the table to confirm that some rows are visible to the query.

Example: the latest orders for one customer

CREATE INDEX orders_customer_created_idx
    ON orders (customer_id, created_at DESC);

SELECT id, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 10;

The index groups entries by customer, then puts the newest orders first within each group. The database can jump to customer 42 and stop after finding ten matching rows. A query for all orders placed since yesterday is different: matching timestamps are spread across customer groups. The same index isn't necessarily unusable, but an index starting with created_at is usually a better fit if that query runs often.

When a scan is cheaper

An index can help with recurring filters or with finding matching rows on one side of a join. But on a small table, or when a query needs much of the table, scanning it may cost less than reading an index and fetching rows from the table. Indexes also consume storage and add maintenance work to writes. Start with the queries that matter rather than indexing every column or assuming that two single-column indexes will provide the same ordering as one composite index.

What interviewers look for

Expect to be given a query and asked to choose an index, explain the column order, and describe how you'd verify your choice. In PostgreSQL, EXPLAIN shows the chosen plan and its estimates; EXPLAIN ANALYZE actually runs the statement and reports observed timings and row counts. Compare estimates with actual results on representative data instead of treating an index scan as proof that the query got faster.