Indexes & Query Performance

The Index Mental Model

An index is a maintained, redundant access path from searchable keys to rows. It can replace broad inspection with targeted navigation, but every extra structure consumes space and write work, and the planner may rationally choose another path.
  • Logical promise and physical structure differ.
  • An index maps keys to row locators or stored rows.
  • Access has navigation and fetch phases.
  • Every write maintains applicable indexes.
  • An index is useful only through supported operators.
  • Benefit belongs to a workload, not a table.
Commerce lookup and maintenance paths
Cost ledger for `(customer_id, status, placed_at)`
OperationPotential gainPotential cost
Recent paid-order lookupNavigate to one ordered customer/status intervalTree pages plus possible heap fetch
Customer rangeRead adjacent matching entriesMany locators can mean scattered fetches
Insert orderNew searches become possible immediatelyExtra search, WAL/logging, page modification
Change indexed statusNew searches become possibleOld entry removal and new entry insertion
DeleteFuture lookup avoidedLogical deletion, vacuum/purge or merge work varies
One canonical commerce access path
CREATE INDEX orders_customer_status_placed_idx
  ON orders (customer_id, status, placed_at DESC);

SELECT order_id, status, placed_at
FROM orders
WHERE customer_id = 7 AND status = 'PAID'
ORDER BY placed_at DESC;

The optimizer compares alternatives rather than obeying the mere existence of this index. If customer 7 owns most rows, pages are already cached, or the query returns most columns and rows, scanning may cost less than navigating and fetching many scattered tuples.