What is the difference between a sequential scan and an index scan?

IntermediateConceptPostgreSQL
Answer

Short answer

A sequential scan reads every row of a table to find matches. An index scan uses an index to jump straight to the matching rows. Index scans win when you need a small fraction of rows; sequential scans can actually be faster when you need a large share of the table.

Example

EXPLAIN SELECT * FROM orders WHERE id = 42;          -- Index Scan using orders_pkey
EXPLAIN SELECT * FROM orders WHERE total > 0;        -- Seq Scan (almost every row matches)

Other scan types you'll see

Plan node Meaning
Index Only Scan All needed columns are in the index — table not touched
Bitmap Index/Heap Scan Collects matching locations from the index, then reads pages in order (medium selectivity)

Why Postgres sometimes ignores your index

  • The query returns too many rows (seq scan is cheaper).
  • The column is wrapped in a function: WHERE lower(email) = ... needs an expression index ON (lower(email)).
  • Stale statistics → ANALYZE.
  • Type mismatch (comparing text to integer).

What to say in the interview

"A seq scan reads the whole table; an index scan seeks directly to matching rows. The planner chooses based on selectivity and statistics, so an ignored index usually means low selectivity, a function on the column needing an expression index, or stale stats."

All Databases (MySQL, PostgreSQL, MongoDB) questions Roadmap: PostgreSQL basics

More on PostgreSQL