Sub topic 4
Point Lookups, Range Scans, and Ordering
Prerequisites: you should know basic SELECT, WHERE, and ORDER BY syntax, and understand that a B-tree keeps indexed values in sorted order.
An index lookup can find rows without checking every row in a table. A point lookup searches for one exact key value, such as customer_id = 42. A range scan starts at one key and reads adjacent keys until it reaches the end of the requested range.
Point lookups
Suppose orders_customer_id_idx is a B-tree index on customer_id. The database follows the tree from its root to the leaf page containing 42. The leaf stores the key and a reference to the matching table row. If several orders belong to customer 42, the database reads the neighboring entries for that same key.
CREATE INDEX orders_customer_id_idx
ON orders (customer_id);
SELECT *
FROM orders
WHERE customer_id = 42;An exact comparison is usually efficient when the value matches relatively few rows. The index finds the first matching entry, then the database fetches the requested columns from the table. Those table fetches are sometimes called heap lookups in PostgreSQL terminology.
The index does not necessarily contain the complete row. It usually contains the indexed key and a row reference. Therefore, an index can reduce the number of rows examined while still requiring additional table reads.
Range scans
A range scan uses the index's sorted order. The database seeks to the first qualifying key and then walks forward through nearby leaf entries.
SELECT *
FROM orders
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01';This half-open range includes January 1 and excludes February 1. It avoids problems caused by guessing the final time of the last day.
| Predicate | Lookup behavior |
|---|---|
| `customer_id = 42` | Find one key and its matching entries |
| `created_at >= start AND created_at < end` | Seek to `start`, then scan sorted entries |
A range scan helps most when the range is small compared with the table. If nearly every row matches, reading the index and then fetching almost every table row may cost more than a sequential scan.
Using an index for ordering
Because a B-tree is sorted, the database may use it to satisfy ORDER BY without a separate sort step.
SELECT id, customer_id, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at
LIMIT 20;An index whose key order matches the filter and ordering can return the first 20 rows directly. A B-tree can usually be scanned in either direction, so an ascending index can also support a descending order.
However, an index scan is not automatically faster. If the query returns most rows, a sequential scan followed by a sort may cost less. The planner compares estimated costs before choosing a plan. Use EXPLAIN to inspect that choice.
EXPLAIN
SELECT *
FROM orders
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01'
ORDER BY created_at;Look for Index Scan or Index Only Scan when an index supplies the access path. Seq Scan means the planner chose to read the table sequentially.
Exercise
Why can an index help a January date query even though it does not identify one exact row?
Show answer
The B-tree is sorted. The database seeks to the first January value and reads adjacent entries until the range ends.