Sub topic 5
Sequential, Index, Index-Only and Bitmap Scans
This chapter assumes you can write SELECT queries with WHERE and understand the basic purpose of a database index. The examples use PostgreSQL and an orders table with columns such as customer_id and created_at.
A scan is how PostgreSQL finds rows in a table. The planner chooses a scan based on the estimated number of matching rows, the available indexes, and the estimated cost of reading data.
Sequential scans
A sequential scan reads the table from beginning to end. PostgreSQL checks each row against the query condition.
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;A simplified plan might look like this:
Seq Scan on orders (cost=0.00..1850.00 rows=120 width=64)
Filter: (customer_id = 42)Seq Scan does not automatically mean the query is slow. It can be the best choice when:
- The table is small.
- The query returns a large part of the table.
- The table has no useful index.
- Reading the whole table sequentially is cheaper than many scattered reads.
For example, if half the rows match, using an index may require PostgreSQL to find many index entries and then fetch many table pages. Reading the table once can be cheaper.
Index scans
An index scan uses an index to find matching row locations, then reads the table rows. For example:
CREATE INDEX orders_customer_id_idx
ON orders (customer_id);
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;The plan may contain:
Index Scan using orders_customer_id_idx on orders
Index Cond: (customer_id = 42)An index scan is usually useful when the condition matches a small part of the table. The index narrows the search, but PostgreSQL still visits the table to retrieve columns that are not stored in the index.
The planner may still select a sequential scan when an index exists. That choice can be correct. An index is an option, not a command.
Index-only scans
An index-only scan can answer the query using only index pages. It avoids fetching the table row when every selected column is available in the index.
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at);
EXPLAIN
SELECT customer_id, created_at
FROM orders
WHERE customer_id = 42;A possible plan is:
Index Only Scan using orders_customer_created_idx on orders
Index Cond: (customer_id = 42)PostgreSQL still checks whether each row is visible to the current transaction. It can often answer that check from its visibility map, which records table pages whose rows are known to be visible. If visibility information is missing, the plan may show heap fetches and behave partly like a regular index scan.
Bitmap scans
A bitmap scan is useful when an index finds more rows than a typical index scan should fetch one by one, but reading the entire table would still be wasteful. PostgreSQL first builds a bitmap of matching table pages, then reads those pages in a more organized order.
A plan often contains both nodes:
Bitmap Heap Scan on orders
Recheck Cond: (customer_id = 42)
-> Bitmap Index Scan on orders_customer_id_idx
Index Cond: (customer_id = 42)The bitmap index scan finds matching locations. The bitmap heap scan reads the required table pages. PostgreSQL may also combine multiple indexes with BitmapAnd or BitmapOr.
Choosing among scan types
| Scan type | Reads | Often suitable when |
|---|---|---|
| Sequential scan | The table from start to finish | Many rows match or the table is small |
| Index scan | Index entries and individual table rows | Few rows match |
| Index-only scan | Index pages, with visibility checks | All needed columns are indexed |
| Bitmap scan | Matching index entries and grouped table pages | A moderate number of rows match |
When a scan seems surprising, compare the estimated row count with the actual row count from EXPLAIN ANALYZE. A bad estimate, stale statistics, or a condition that matches many rows may explain the decision.
Exercise
An index exists on orders(customer_id), but PostgreSQL chooses a sequential scan for a query that returns 80 percent of the table. Is the plan necessarily wrong?
Show answer
No. Reading most table pages sequentially can cost less than using the index and fetching many scattered table rows.