Course contents 08 / 19

08 / 19 · Storage and Execution

Designing an index

Choose a composite key for a shop's date report and distinguish ordered keys from included payload.

The shop report reads a date range for one store, in date and order-number order. An index on date alone places orders for all stores together. Putting the store first can restrict the search to one contiguous interval of keys.

Put the equality before the range#

Use six toy orders from two stores. Each row already has its order total.

CREATE TEMP TABLE report_orders (
    order_id integer,
    shop_id integer,
    paid_on date,
    total_cents integer
);

INSERT INTO report_orders VALUES
    (101, 1, DATE '2026-01-01', 1300),
    (102, 1, DATE '2026-01-02', 350),
    (103, 1, DATE '2026-01-02', 1550),
    (104, 1, DATE '2026-01-03', 1200),
    (201, 2, DATE '2026-01-02', 2400),
    (202, 2, DATE '2026-01-03', 800);

CREATE INDEX report_orders_shop_day_id
ON report_orders (shop_id, paid_on, order_id) INCLUDE (total_cents);

A multicolumn B-tree orders tuples lexicographically. It compares shop_id first, then paid_on when shops match, then order_id when both previous fields match. Within shop 1, the keys for 2 January are adjacent. Shop 2 has an order on the same day, but it is outside that interval. (PostgreSQL 18, §11.3; SQLite, §1.6)

Figure

One interval inside shop 1

One interval inside shop 1The six keys are ordered by shop, date and order ID. The second and third keys belong to the selected interval.ShopDateOrder101-01101101-02102101-02103101-03104201-02201201-03202
The date filter selects two adjacent keys within shop 1. Order 201 is on the same date in shop 2.
SELECT order_id, paid_on, total_cents
FROM report_orders
WHERE shop_id = 1
  AND paid_on >= DATE '2026-01-02'
  AND paid_on < DATE '2026-01-03'
ORDER BY paid_on, order_id;

The rows are 102 and 103 on 2 January, with amounts 350 and 1550 cents. The index can start at the lower date bound inside the shop-1 block and stop at the upper bound. The index keys in that interval already have the requested order. With a fixed shop and no date restriction, the index can still produce that order across the whole shop block. SQL keeps ORDER BY because another access path might return the same rows in a different order.

Without a shop condition, the same date can occur in many shop blocks. PostgreSQL 18 can sometimes use skip scan, which searches later keys repeatedly under different values of an earlier key. It can be attractive when there are few distinct earlier values. If reports across all stores are common, a date-leading index could instead make those searches contiguous. (PostgreSQL 18, §11.3)

Distinguish search keys from payload#

The query also needs total_cents. INCLUDE adds it as payload, without making it part of the search order. An index covers a query when it contains the values that query needs. Both SQLite and SQL Server describe covering indexes, with different schema syntax. In PostgreSQL, the ability to read output columns from the index is only one part of an index-only scan. The engine still checks row visibility. If a heap page is marked all-visible, the scan can use that fact. Otherwise it may need the heap even though the index contains every requested value. (PostgreSQL 18, §11.9; SQLite, §1.7; Microsoft, Included Columns)

An included value makes each entry larger. Include a column when avoiding heap access is useful for the workload. An included value cannot provide a search bound or the ordering that a key column provides.

Check how much the predicate selects#

Selectivity is the fraction of rows that pass a condition. The two predicates above keep 2 of 6 toy orders. On a much larger table, the same date range might keep a small fraction or most rows, depending on the data. A narrow range can make a few heap visits attractive. A broad range can make the index and heap visits cost more than one sequential scan. This is why an index definition alone does not determine the executed plan.

Next#

One query may be served by an index or by a table scan and sort. From SQL to execution follows the operators that produce the result and shows where to see the chosen path.

References#

  1. PostgreSQL Global Development Group. PostgreSQL 18 documentation. Multicolumn Indexes, §11.3 covers key order and skip scan. Index-Only Scans and Covering Indexes, §11.9 covers included columns and the visibility map.
  2. SQLite Project. Query Planning, §§1.6–1.7. Describes multicolumn key ordering and covering indexes.
  3. Microsoft. Create Indexes with Included Columns. Explains the SQL Server distinction between key and included columns and the cost of larger entries.