Overview
An index is a separate, ordered structure that lets the engine find rows without scanning the whole table. It trades faster reads for slower writes and more storage. The default structure in PostgreSQL, SQL Server and Oracle is the B-tree (strictly a B+tree: all row pointers or data live in the leaves, which are linked for range scans).
Core concepts
- B-tree: a shallow, wide tree. With a fan-out of hundreds, three or four levels address hundreds of millions of rows, so a lookup costs a handful of page reads.
- Composite index
(a, b, c)is sorted bya, thenb, thenc. It serves predicates on a leftmost prefix (a;a, b;a, b, c) and a range on the last used column. Put equality columns first, then the range or sort column. - Covering index: contains every column a query needs, so no table lookup is needed. PostgreSQL and SQL Server use
INCLUDE (cols)for non-key payload columns; PostgreSQL shows an Index Only Scan. - Selectivity: the fraction of rows a predicate returns. Indexes pay off at low fractions; returning a large share of the table is cheaper with a sequential scan.
- Sargable predicates can use an index seek: the indexed column is bare on one side of the comparison.
- Clustered / index-organised storage: SQL Server’s clustered index and Oracle’s index-organised table store the rows in the index order; PostgreSQL heap tables are unordered, and an index always points to the heap.
- Other structures: hash, bitmap (Oracle), GiST/GIN/BRIN (PostgreSQL), columnstore (SQL Server), sparse primary index (ClickHouse; see the ClickHouse section).
Worked example
500,000-row table big(id, customer_id, status, amount, created), PostgreSQL 17, ~72 rows match customer_id = 42.
Without an index:
Gather
-> Parallel Seq Scan on big
Filter: (customer_id = 42)
Rows Removed by Filter: 166643
Buffers: shared hit=3378After CREATE INDEX big_cust ON big(customer_id):
Bitmap Heap Scan on big (actual rows=72)
Heap Blocks: exact=71
Buffers: shared hit=71 read=3
-> Bitmap Index Scan on big_custAbout 3,400 buffers become about 75. With a covering index (customer_id) INCLUDE (amount) and a vacuumed table, SELECT customer_id, sum(amount) ... WHERE customer_id = 42 becomes an Index Only Scan with Heap Fetches: 0.
Sargability, with an index on created:
-- cannot use the index: the column is wrapped in a function
WHERE date_trunc('month', created) = DATE '2025-03-01'
-- can: bare column, half-open range
WHERE created >= DATE '2025-03-01' AND created < DATE '2025-04-01'The first plan was a parallel sequential scan; the second an Index Only Scan on the created index. A predicate on a low-selectivity column (status = 'paid', most rows) stays a sequential scan, which is the correct choice.
Pitfalls and production notes
- Every index slows
INSERT,UPDATEof indexed columns andDELETE, and takes memory and disk. Drop unused ones (check usage statistics:pg_stat_user_indexes, SQL Serversys.dm_db_index_usage_stats, Oracle index monitoring). - Index foreign key columns on the child table (relational-model).
- Implicit type conversion can silently defeat an index, for example comparing a
varcharcolumn to annvarcharparameter in SQL Server, or a number column to a string in Oracle. LIKE '%abc'cannot use a B-tree;LIKE 'abc%'can (with a compatible collation or operator class).- Statistics decide whether the optimizer picks your index (query-planning).
Practice questions
- For
WHERE a = ? AND b > ? ORDER BY b, which column order is best for an index on(a, b)versus(b, a)? - Why can an index on a boolean column be useless, and when is a partial or filtered index useful?
- What does
Heap Fetchestell you in an Index Only Scan?
References
- PostgreSQL manual, Indexes: https://www.postgresql.org/docs/current/indexes.html
- Use The Index, Luke: https://use-the-index-luke.com
Related
query-planning · sql-joins · table-partitioning · sql-window-functions · pg-index-types · sqlserver-indexes · oracle-indexes · ch-sorting-key-sparse-index