Covering Index

Difficulty: Advanced

Question

What is a covering index, and how does it let the database answer a query without reading the table?

Answer

A covering index is an index that contains every column a query needs, both for filtering and for the output, so the database can answer the query from the index alone. This avoids the extra random reads to the table (the heap in PostgreSQL, the clustered index in MySQL) that a normal index lookup requires. In PostgreSQL this appears as an Index Only Scan, and in MySQL EXPLAIN shows "Using index".

Why the table lookup is expensive

A normal B-tree index stores the indexed key plus a pointer to the row. For a query like:

SELECT order_id, amount_paise FROM orders WHERE user_id = 42 ORDER BY created_at DESC LIMIT 20

an index on user_id finds the matching entries, but then must jump to the table for each one to fetch amount_paise and created_at. Those jumps are random I/O spread across the table, and on a cold cache they dominate the query time. If the index already holds every needed column, those jumps disappear.

Two ways to build one

  • Composite key columns: CREATE INDEX ON orders (user_id, created_at, amount_paise). All three columns are part of the sorted key.
  • Included (non-key) columns: CREATE INDEX ON orders (user_id, created_at) INCLUDE (amount_paise). Supported in PostgreSQL 11+ and SQL Server. Included columns live only in the leaf pages, are not sorted or searchable, and keep the upper levels of the tree smaller.

Use key columns for what you filter and sort by; use INCLUDE for columns you only return. INCLUDE is also the way to add payload columns to a UNIQUE index without changing what must be unique.

Engine-specific details

  • PostgreSQL: an index-only scan still needs to know whether each row is visible to your transaction. It checks the visibility map; for pages not marked all-visible, it visits the heap anyway. EXPLAIN ANALYZE shows this as Heap Fetches. Regular VACUUM (autovacuum) keeps this number low.
  • MySQL InnoDB: every secondary index implicitly contains the primary key, so an index on (user_id, created_at) already covers queries that also return the primary key column.
  • SQL Server: the same INCLUDE syntax; the plan shows an Index Seek with no Key Lookup.

Trade-offs

  • Wider indexes use more disk and memory, and fewer entries fit per page.
  • Every insert or update to an included column must update the index too, which slows writes and, in PostgreSQL, can prevent HOT updates.
  • Covering every query creates index sprawl. Build covering indexes for your hottest, most latency-sensitive queries only.

Common mistakes

  • Selecting extra columns (or SELECT *) so the index no longer covers the query.
  • Putting a column needed for ORDER BY in INCLUDE, where it cannot provide sort order.
  • Expecting an index-only scan on a freshly bulk-loaded PostgreSQL table before VACUUM has set the visibility map.
Regular index lookup vs covering index. The covering index removes the per-row trip back to the table.
Regular indexCovering index
Index contentsSearch key plus row pointerSearch key plus all needed columns
Table accessOne random read per matching rowNone, or only for non-visible pages
Plan label (PostgreSQL)Index ScanIndex Only Scan
CostSmaller indexLarger index, slower writes

Code examples

Covering index with INCLUDE in PostgreSQL

-- Hot query: latest 20 orders for a user with their amount
CREATE INDEX orders_user_recent_idx
  ON orders (user_id, created_at DESC)
  INCLUDE (amount_paise, status);

VACUUM (ANALYZE) orders;  -- sets the visibility map

EXPLAIN (ANALYZE, BUFFERS)
SELECT created_at, amount_paise, status
FROM orders
WHERE user_id = 42
ORDER BY created_at DESC
LIMIT 20;

Heap Fetches: 0 confirms the query was answered entirely from the index without touching the table.

Key points

  • A covering index holds every column the query filters on, sorts by and returns.
  • Avoids random table lookups; shows as Index Only Scan (PostgreSQL) or Using index (MySQL).
  • INCLUDE adds non-key payload columns (PostgreSQL 11+, SQL Server).
  • PostgreSQL index-only scans depend on the visibility map; watch Heap Fetches.
  • InnoDB secondary indexes always contain the primary key.

Common follow-up questions

  • What is the difference between key columns and INCLUDE columns in an index?
  • Why might PostgreSQL still visit the heap during an index-only scan?
  • How does column order in a composite index affect which queries it serves?
  • What does a Key Lookup in a SQL Server plan indicate?

Interview tips

  • Explain the extra table lookup that a normal index needs; the benefit only makes sense once that cost is clear.
  • Mention the visibility map and Heap Fetches if the interviewer is PostgreSQL-focused; it separates practitioners from theory.
  • Balance it with write cost and index size so you do not sound like you would index everything.

Concepts covered

Covering Index, Index-Only Scan, INCLUDE Clause, Visibility Map, Composite Index

More Database Design questions