Sub topic 10
Covering indexes and index-only scans
This chapter assumes you know how a B+tree index maps keys to row locations, the difference between clustered and secondary indexes, and how composite indexes are ordered by column. It builds on those ideas.
Why a secondary index lookup costs extra
A secondary index stores the indexed columns plus a pointer to the row. In PostgreSQL the pointer is a tuple ID in the heap (the table's main storage). In InnoDB it is the primary key value. After the index finds a match, the database makes a second trip to the table to fetch the other columns the query asked for.
For a query that matches many rows, those trips are mostly random page reads. They often cost more than the index search itself.
What makes an index covering
An index is covering for a query when every column the query needs is stored in the index. The database can then answer from the index leaf entries and skip the table. When the plan does this, it is called an index-only scan.
"Needs" includes columns in SELECT, WHERE, ORDER BY, and JOIN clauses. One missing column means a table visit.
Building one with INCLUDE
You can cover a query by adding columns to the key, but that makes entries larger and changes sort order. PostgreSQL 11 and later offer INCLUDE, which stores extra columns in the leaf entries without using them for ordering.
CREATE INDEX orders_customer_created_total_idx
ON orders (customer_id, created_at)
INCLUDE (total);
EXPLAIN
SELECT created_at, total
FROM orders
WHERE customer_id = 42;The query filters on customer_id, which is the leading key column. It returns created_at (a key column) and total (an included column). Every column it touches is in the index, so the planner can choose an index-only scan.
Use INCLUDE for columns you only read, never search or sort by. Keep key columns for filtering and ordering.
The visibility catch in PostgreSQL
PostgreSQL uses MVCC (multi-version concurrency control), so several versions of a row may exist at once. Index entries do not record which transactions can see a version. That information lives in the heap.
To skip the heap, PostgreSQL consults the visibility map, which marks pages where all rows are visible to every transaction. If a page is not marked, the scan must still visit the heap for those rows.
The plan shows this in the Heap Fetches count:
Index Only Scan using orders_customer_created_total_idx on orders
Index Cond: (customer_id = 42)
Heap Fetches: 0A high Heap Fetches number means the visibility map is out of date. Running VACUUM updates it. Tables with heavy updates lose the benefit more often.
InnoDB handles this differently. A clustered table's secondary indexes already contain the primary key, so a query needing only indexed columns and the primary key is covered automatically. MySQL reports Using index in the Extra column of EXPLAIN.
Trade-offs
- Wider entries mean fewer entries per page, so lower fanout and a larger index.
- Every write to an included column must also update the index.
- Covering indexes suit hot, read-heavy queries. Do not add them for every query.
Exercise
Exercise
A table has an index on (customerid, createdat) INCLUDE (total). Which of these queries can use an index-only scan: (a) SELECT total FROM orders WHERE customerid = 7; (b) SELECT status FROM orders WHERE customerid = 7; (c) SELECT total FROM orders WHERE total > 100? Explain each.
Show answer
Only (a) can. It filters on the leading key column and reads only total, which is included in the index. Query (b) needs status, which is not in the index, so it must visit the table. Query (c) reads only indexed columns, but it filters on an included column, which is not ordered and cannot be used to seek, and it skips the leading key column. The planner could in principle scan the whole index, but it will usually not choose that over other plans.