07 / 19 · Storage and Execution
Indexes and access paths
Use a B-tree to find matching heap rows, compare it with a hash index and account for index writes.
The shop loads all lines of order 103. The condition order_id = 103 is simple. A sequential scan must still inspect the table to find the two matching lines. An index adds a route from the order number to the stored records.
Separate a search key from the row#
A database index stores searchable keys with information needed to reach matching rows. A PostgreSQL B-tree on order_id can map each order number to the physical addresses of its lines. The order number is a search key here and need not be unique. Two entries can have key 103 and refer to different records. A table can have several indexes, each for a different family of queries. (PostgreSQL 18, Index Types; SQLite, Query Planning)
Run the six-line setup and create the index in the same session.
CREATE INDEX order_lines_by_order ON order_lines (order_id);
SELECT line_no, product, quantity
FROM order_lines
WHERE order_id = 103
ORDER BY line_no;
| line_no | product | quantity |
|---|---|---|
| 1 | coffee | 2 |
| 2 | tea | 1 |
The statement returns the same rows with or without the index. An access path is a way for the engine to obtain them. For a table with six rows it may prefer scanning the heap. The index makes another plan possible. ORDER BY line_no still defines the requested order.
Descend through a B-tree#
A B-tree stores keys in order and keeps its leaves at the same depth. Interior pages hold separators and references to children. At each page, a search uses the separator keys to choose a child. Leaf pages hold entries used to return the matching rows. In the toy diagram, the left leaf contains keys below 103 and the right leaf starts at 103. The equality search reads the root and then the right leaf. It finds both addresses for 103 there. Real entries may span multiple leaf pages, and an index can represent duplicate keys differently. (Bayer and McCreight, 1972; PostgreSQL 18, B-tree Implementation)
Search for order 103
If the two address entries point to different heap pages, an ordinary index lookup can require up to four distinct page visits in this model. There are two index pages and two heap pages. If both records share a heap page, the count is three. These are page references in a toy layout. Buffer hits can avoid fetches from storage. For a range such as order_id >= 102 AND order_id < 104, the tree finds the first eligible key and reads subsequent ordered entries until it reaches the upper bound. A wider range can touch enough heap pages to make a full scan cheaper.
Use the access path that fits the predicate#
A PostgreSQL hash index stores a hash code computed from the indexed value. Equality can use that code to find candidates. Hash codes can collide, so sharing a hash is insufficient to prove equality. Hash order is different from key order. PostgreSQL hash indexes support equality and cannot provide an ordered range scan. B-trees support equality and comparisons over ordered keys. (PostgreSQL 18, Index Types)
An index consumes space and adds maintenance work when rows are inserted or when indexed values change. In a B-tree, adding an entry to a full page can split it and insert a separator into its parent. A split can propagate up the tree. The trade-off depends on the queries that use an index and the write workload that maintains it. (Bayer and McCreight, 1972; PostgreSQL 18, B-tree Implementation)
Next#
The order number is useful for a lookup. A shop report filters by both shop and date and needs a predictable row order. Designing an index shows how several fields form one ordered key and which parts of a query can use it.
References#
- PostgreSQL Global Development Group. PostgreSQL 18 documentation. Index Types, §11.2 compares B-tree and hash predicates. B-tree Implementation, §65.1.4 describes leaf references and page splits.
- Rudolf Bayer and Edward M. McCreight. Organization and Maintenance of Large Ordered Indexes. Acta Informatica, 1, 173–189, 1972. Defines B-tree pages and describes search and splitting.
- SQLite Project. Query Planning, §§1.3–1.4. Describes an ordered index lookup followed by row fetches and the cost of multiple matches.