← Lessons

quiz vs the machine

Gold1420

Databases

Index Only Scans and Covering Indexes

When every needed column lives in the index, the database can answer a query without touching the table at all.

5 min read · core · beat Gold to climb

Skipping the Table

A normal index scan finds matching entries in the index, then visits the heap to read the rest of each row's columns. An index only scan answers the query entirely from the index because every column it needs is already stored there. This avoids the random heap fetches that often dominate cost.

Covering Indexes

A covering index is one that contains all columns a query reads. You build one by adding the extra columns either as key columns or as included payload columns that are stored but not used for ordering. The goal is that the index alone satisfies both the filter and the projection.

The Visibility Catch

The index does not record whether a row is still visible to your transaction. The database keeps a visibility map marking pages where all rows are visible. Index only scans only skip the heap for those clean pages, so a table with many recent changes still triggers heap visits until vacuum updates the map.

Key idea

A covering index lets the database answer from the index alone, but visibility checks mean only clean pages skip the heap, so keep vacuum current.

Check yourself

Answer to earn rating on the learn ladder.

1. What makes an index only scan possible?

2. Why might an index only scan still visit the heap?