Seq Scan vs Index Scan vs Index-Only
Why the planner chooses each and what it tells you about your query.
Three Ways to Read a Table
When the planner needs rows from a table it picks one of three access methods, and interviewers expect you to name all three:
- Seq Scan, read every row in the table from start to finish.
- Index Scan, walk an index to find matching rows, then fetch each from the table.
- Index-Only Scan, answer entirely from the index without touching the table at all.
Knowing why the planner chooses each is the core of this lesson and a guaranteed senior question.
What a Sequential Scan Does
A Seq Scan reads the table's pages one after another and applies any filter to each row. No index is consulted.
This sounds bad but it is often the right choice. Sequential reads are fast for the disk (no random jumps), so when a query returns a large fraction of the table, scanning everything beats jumping through an index millions of times.
The example: scan orders, keep rows where amount > 100. If most orders exceed 100, a seq scan is correct.
EXPLAIN SELECT * FROM orders WHERE amount > 100;
Seq Scan on orders (cost=0.00..18334.00 rows=900000 width=64)
Filter: (amount > 100)All lessons in this course
- Reading an EXPLAIN Plan
- Seq Scan vs Index Scan vs Index-Only
- Join Algorithms: Nested Loop, Hash, Merge
- Spotting and Fixing Slow Queries