liveBook · Manning

Sequential scan

As explained in MongoDB 8.0 in Action, Third Edition

A collection scan is query execution that examines documents throughout a collection to find matches without relying on an applicable index.

A sequential scan, also called a collection scan, examines documents throughout a MongoDB collection to find matches when no applicable index supports the query. It can burden response time and database resources as collections grow. section 7.1

Use these guidelines when evaluating a collection scan:

  • Expect MongoDB to scan the collection when any $or clause lacks suitable index support; every clause must be indexed for indexed evaluation. section 7.6.4
  • Do not assume an index is always faster: indexes require index and document lookups, while a collection scan uses one lookup for broad result sets. section 7.7
  • Treat the roughly 30% selectivity guideline as variable; data size and query conditions can move the useful range from about 2% to 60%. section 7.7

A growing log query

A monitoring system queries logs for one server during the past week. An index on timestamp may be fast when the result set is small, but the same query can become slow as accumulated logs make the result set large. section 7.7

db.movies.find({
  $or: [
    { year: 1914 },
    { "imdb.rating": { $gt: 7 } }
  ]
}).explain("executionStats")

Inspect executionStats for an $or query to verify whether MongoDB performs a collection scan or uses supported indexes. section 7.6.4

db.movies.createIndex({ "imdb.rating": 1 })

Create an index for the imdb.rating clause so every $or condition has index support and the collection scan can be avoided. section 7.6.4

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains scan behavior, index trade-offs, $or requirements, and selectivity, but does not detail scan internals.
Examples
Uses server-log queries and an unindexed movie-rating $or clause to demonstrate collection-scan behavior.
What you can do after
Diagnose collection scans with explain and improve eligible $or queries by adding supporting indexes.
How it connects
Connects collection scans with query planners, indexes, $or queries, and execution-plan analysis.

Also explained in

Compare how 3 books explain sequential scan →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.