Sequential scan or index scan: the choice is about fractions, not rules
A sequential scan reads every page of a table and applies the filter to each row. An index scan descends a B-tree to the matching entries and then visits the heap pages those entries point to. The first does a known amount of sequential I/O; the second does a smaller amount of random I/O plus bookkeeping. Which is faster depends on how many rows match and how expensive a random read is on the storage.
The planner compares the two costs using seq_page_cost and random_page_cost. With the default ratio of 4, an index is attractive only when it eliminates most of the pages. On NVMe the real ratio is closer to 1, and the same index is attractive for a much larger fraction. This is why the same query can be fast on one server and slow on another with the same data.
| Plan | Reads | Best when |
|---|---|---|
| Seq Scan | All pages, sequential | Small table or a large fraction matches |
| Index Scan | Index pages plus heap pages, random | Few rows match, or order is needed |
| Index Only Scan | Index pages only | All needed columns are in the index, visibility map is current |
| Bitmap Heap Scan | Index bitmap plus heap in physical order | Moderate fraction, random I/O is costly |
| Bitmap Index Scan | Index pages only, builds the bitmap | Child of a bitmap heap scan |
Selectivity is the first question
Ask what fraction of the table the predicate matches. One percent of a million rows is 10,000 rows, which an index can fetch efficiently. Fifty percent is 500,000 rows, and visiting half the table through an index is usually slower than reading it in order. The estimate can be wrong, which is why the estimated and actual rows matter: a plan that assumed one percent and got fifty percent is a statistics problem.
Correlation matters too. If the indexed column is physically ordered, the heap visits are sequential and the index scan is cheaper than the random-cost model predicts. PostgreSQL records this as the column's correlation statistic and adjusts the cost. A perfectly correlated index scan can be almost as cheap as a sequential scan, which is why an append-only table's time-ordered index performs so well.
When a sequential scan is the right answer
Small tables are read entirely because the index descent and heap visits cost more than a few pages. A table with a hundred rows rarely benefits from an index on a filter, though it may still need one for a uniqueness constraint. Very wide rows change the arithmetic: fewer rows fit per page, so the same row count costs more pages, which pushes the planner toward an index sooner.
Analytical queries that aggregate most of a table legitimately scan it. A report that counts all orders by month reads every order; no index avoids that work, and a sequential scan is the honest plan. The optimisation there is a materialised summary or a pre-aggregated table, not an index on the filter column.
Index-only scans and the visibility map
An index-only scan answers the query from the index without visiting the heap, but only if every column needed is in the index and the visibility map says the pages are all-visible. The map is maintained by VACUUM. A table that has not been vacuumed recently shows many heap fetches, and the scan degrades toward a normal index scan. The planner's estimate can be optimistic because it assumes a clean map.
Covering indexes make index-only scans possible. In PostgreSQL 11 and later, INCLUDE adds non-key columns to the leaf entries without making them part of the search key. A query that filters on one column and returns two others can be served entirely from an index on the filter with those two included, at the cost of index size and write amplification.
What to do with a slow sequential scan
- Check the filter and the rows removed; a scan that discards most rows is a candidate for an index.
- Check the estimate against the actual rows; if the estimate is far off, fix statistics first.
- Check whether an existing index could serve the predicate but is not used because of a function, cast or type mismatch.
- Consider a composite index with equality columns first and the range or sort column last.
- If most of the table matches, accept the scan and optimise the aggregation or the data model instead.
The analyzer flags a sequential scan that reads many rows and keeps few, and proposes an index on the filter columns. That suggestion is a hypothesis. The same index that speeds this query slows every insert and update, and if the predicate is not selective in production it will not be used. Measure with the real distribution before creating it.
An index is a bet that the predicate is selective. Check the bet against actual rows before paying the write cost.
Sources
- PostgreSQL documentation — Introduction to indexes
- PostgreSQL documentation — Index-only scans
- PostgreSQL documentation — random_page_cost
Questions
Is a Seq Scan always bad?
No. For a small table, or a predicate that matches a large fraction of the rows, a sequential scan reads pages in order and avoids the random I/O of an index. A planner that chooses it may be right; the question is whether the fraction is really large and whether the table is really small.
At what selectivity does an index win?
It depends on storage and row width, but a common crossover is around 5 to 15 percent of the table. On fast NVMe storage with random_page_cost near 1, indexes win at higher fractions. On spinning disks they lose earlier. Measure on your hardware rather than applying a fixed percentage.
What is a bitmap heap scan?
The planner reads the index to build a bitmap of matching heap pages, then visits those pages in physical order. It reduces random I/O when many rows match but not enough for a full scan. It also loses the index order, so it cannot satisfy an ORDER BY without a sort.
Load the indexed sample and the seq-scan sample and compare the two shapes in the tree.
Open the analyzerWritten against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.
Back to the guides