Sequential scan

Also known as: full table scan · table scan

On this page 7

A sequential scan, also called a full table scan or table scan, is a database access method that reads every page of a table in physical storage order and evaluates the query conditions against each row. In databases and in the topic of indexing, it is the baseline access path that the query planner chooses when no usable index exists or when an index would be slower than reading the whole table.

A sequential scan is not inherently a performance problem. Because it reads data in large contiguous blocks, it is often the cheapest plan for small tables and for queries that return a large fraction of the rows.

How a sequential scan reads a table

A sequential scan works by iterating over the table’s heap pages from the first to the last, loading each page into the buffer cache and checking every tuple on it against the filter predicate. Rows that satisfy the condition are passed to the next plan node, such as a sort, an aggregate or a join. Unlike an index scan, it does not consult any auxiliary structure, so its cost depends almost entirely on table size, not on the selectivity of the condition.

Storage engines optimize this access pattern aggressively. Operating systems and database engines apply read-ahead prefetching, which issues large sequential reads before the pages are needed. PostgreSQL also uses a small ring buffer for scans of large tables so that one big scan does not evict the entire shared cache, and it can run parallel sequential scans in which several worker processes divide the pages between them.

StageWhat happensMain cost factor
Page fetchNext block read from cache or diskI/O throughput
Visibility checkTuple checked against the transaction snapshotDead tuple count
Predicate evaluationWHERE condition tested on each rowCPU per row
Row outputQualifying rows passed upwardNumber of matches
Stages of a sequential scan

Why the query planner chooses a sequential scan

The query planner chooses a sequential scan when its cost model estimates that reading the whole table is cheaper than any index-based alternative. The decision rests on table statistics, the estimated selectivity of the predicate and configured cost constants. In PostgreSQL, seq_page_cost defaults to 1.0 and random_page_cost to 4.0, which expresses the assumption that random reads are about four times more expensive than sequential ones.

As a rough rule, an index becomes unattractive once a query returns more than roughly 5-20% of the rows, although the real threshold depends on row width, physical correlation between index order and table order, cache contents and storage type. On SSDs the gap between random and sequential reads is smaller, so administrators often lower random_page_cost toward 1.1-2.0, which makes index scans win more often.

SituationWhy a sequential scan is chosenIs it a problem
Small tableA few pages are read faster than an index lookupNo
Low selectivityMost rows qualify anywayNo
Missing indexNo access path matches the predicateOften yes
Non-sargable predicateFunction or cast on the column blocks the indexUsually yes
Stale statisticsPlanner misjudges row countsYes
Typical reasons for a sequential scan plan

Sequential scan compared with index-based access

A sequential scan differs from index-based access in its I/O pattern: it reads every page once and in order, whereas an index scan follows a B-tree index to individual rows and may visit table pages in random order. A bitmap heap scan sits between the two, collecting matching row locations first and then reading the needed pages in physical order. An index-only scan avoids the table entirely when the index covers all requested columns.

Access methodPages readBest whenWeakness
Sequential scanAll table pagesLarge share of rows neededCost grows with table size
Index scanIndex plus matching table pagesFew rows, high selectivityRandom I/O per row
Bitmap heap scanIndex plus sorted table pagesModerate selectivityBitmap build overhead
Index-only scanIndex pages, visibility mapCovering index availableNeeds up-to-date visibility map
Access methods and their typical strengths

Specialized structures change the trade-off for some workloads. A BRIN index can skip whole block ranges in very large, naturally ordered tables, and columnar storage reduces the bytes a scan must read by loading only referenced columns.

Diagnosing and tuning sequential scans

Sequential scans are diagnosed by reading the execution plan, for example with EXPLAIN ANALYZE in PostgreSQL or EXPLAIN in MySQL, where InnoDB reports the access type ALL for a full table scan. The useful question is not whether a scan appears, but whether estimated and actual row counts agree and whether the scan dominates execution time. Monitoring views such as pg_stat_user_tables show how often each table is scanned sequentially.

SymptomLikely causeTypical fix
Slow lookup by one keyNo index on the filter columnCreate a suitable index
Index ignored for lower(email)Function applied to the columnAdd an expression index
Scan after bulk loadOutdated statisticsRun ANALYZE
Scan reads far more pages than rows justifyTable bloat from dead tuplesVACUUM or rebuild the table
Mismatched types in joinImplicit cast prevents index useAlign column data types
Symptoms, causes and fixes

Disabling sequential scans globally, for instance with enable_seqscan = off, is a diagnostic tool only. It forces the planner away from the scan to compare plans, but leaving it off in production usually degrades other queries that genuinely benefit from a full read.

Common misconceptions about full table scans

  • Every sequential scan is a defect: analytical queries and small lookup tables are often best served by one.
  • Adding an index always removes the scan: if the predicate matches most rows, the planner still prefers a full read.
  • LIMIT makes a scan cheap: it stops early only when matching rows appear near the start of the table.
  • A scan reads only live data: in MVCC databases it also reads dead row versions until vacuuming removes them.

The practical guidance is to treat a sequential scan as a signal to verify, not an automatic alarm. When it appears on a large table in a frequent, selective query, it deserves investigation; elsewhere it is usually the correct plan.

Stripped to the essentials

A sequential scan means the database reads a table from start to finish, row by row, to find what a query asks for. It is like looking for a name in an unsorted stack of papers by checking every sheet.

That sounds slow, and for one name in a huge stack it is. But when you need half of the papers anyway, flipping through the whole stack is faster than jumping around with an index. The key point is that a full read is sometimes the smartest choice, and the database decides this by estimating costs.

Where it is applied

An online store runs PostgreSQL with an orders table of about 40 million rows. Customer support reports that searching orders by email takes around 9 seconds. The execution plan shows a sequential scan on orders reading roughly 5 GB, because the query filters on lower(customer_email) and the only index covers the raw column.

The database engineer creates an expression index on lower(customer_email) and refreshes statistics. The same lookup now uses an index scan and finishes in under 5 milliseconds. At the same time, the nightly revenue report, which aggregates all orders of the past year, keeps its sequential scan, because it needs most of the table and runs faster as a full read.