Video in progress
If you see `Seq Scan` in `EXPLAIN` output, is that always bad?
No. On a small table, or when the query returns a large part of the table, Seq Scan is the cheapest path. The problem is only when a Seq Scan runs even though you need a few rows from a big table: that means there is no index, or it can't be used.
📖 Answer
An Index Scan "jumps" to the table separately for every row it finds (random I/O). A Seq Scan reads the table from start to end in order, which is cheaper per row. On a 200-row countries table an index gives you nothing.
Another case: if WHERE status <> 'ARCHIVED' returns 90% of the rows, the planner skips the index on its own, and that is the right call.
When to worry:
Seq Scan on payments (actual time=0.02..1850.4 rows=1 loops=1)
Filter: (external_id = 'TX-88123')
Rows Removed by Filter: 9214330
It read 9 million rows and kept one. This one needs an index on external_id.
If there is an index and you still get a Seq Scan, the usual causes are: a function applied to the column (lower(email)), a type mismatch, stale statistics, or a parameter that matches too many rows. There is also an in-between type, Bitmap Heap Scan: for a medium number of rows it collects the addresses from the index and then reads the table in order.
Sign up to mark studied questions and save your progress.