Skip to content
LESSON 24 / 50Advancedindexing

Covered Queries & Zero-Document Heap Scans

A Covered Query is the fastest possible read in MongoDB: every queried and projected field exists within the index B-Tree, completely bypassing document heap reads.

Interactive Engine Simulator

Index Traversal & IXSCAN vs COLLSCAN Execution Plan

Topic 24: Covered Queries & Zero-Document Heap Scans

Execution Stage
IXSCAN + FETCH
B-Tree leaf seek
Documents Examined
2
O(log N) lookup
Keys Examined
2
Index keys inspected
Query Latency
0.3 ms
WiredTiger execution time
Explain Plan Output Summary:
{
  "queryPlanner": {
    "winningPlan": {
      "stage": "FETCH",
      "inputStage": {
        "stage": "IXSCAN",
        "indexName": "role_1_country_1",
        "isMultiKey": false,
        "direction": "forward"
      }
    }
  },
  "executionStats": {
    "nReturned": 2,
    "executionTimeMillis": 1,
    "totalKeysExamined": 2,
    "totalDocsExamined": 2
  }
}

1. Formal Definition & Architecture Intent

A Covered Query is a query where: 1. All fields in the query filter are part of an index, and 2. All fields returned in the projection are part of that same index (with _id explicitly suppressed if not in the index). Because the index satisfies the entire query, WiredTiger reads only the B-Tree keys and never reads document payloads from disk or cache.

2. Explain Like I'm 5 (Mental Model)

Imagine asking a librarian: "What year was Pride and Prejudice published?" Instead of walking to the back room and pulling the heavy book off the shelf, the librarian reads the year directly from the index card in their hand.

3. Real-World Architecture Example

Ultra-high-throughput user authentication checking password hash and status.

Sample BSON Documents:
Collection: db.users
{
  "username": "alice",
  "passwordHash": "$2a$12$...",
  "status": "active"
}

Index on { username: 1, passwordHash: 1, status: 1 } satisfies the query with zero document heap lookups.

4. MQL Implementation Steps

Step 1: Create covering compound index
db.users.createIndex({ username: 1, status: 1, email: 1 });
Index contains all three fields.
Step 2: Execute covered query suppressing _id
db.users.find(
  { username: "alice", status: "active" },
  { _id: 0, email: 1, username: 1 } // _id: 0 is mandatory!
).explain("executionStats");
explain() shows totalDocsExamined: 0 and stage: PROJECTION_COVERED.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
totalDocsExamined: 0The definitive indicator of a covered query in executionStats.
_id Suppression MandatoryUnless _id is part of the index, the projection must include _id: 0.
No Multikey IndexesAn index containing an array field can never cover a query.

6. Deep Dive: Why Covered Queries Yield a 10x Performance Boost

In a standard index scan (IXSCAN), finding an entry in the B-Tree produces a RecordId pointer. The engine must then perform a second lookup into the collection data store (FETCH stage) to load the BSON document. A covered query eliminates the FETCH stage entirely, cutting memory access and disk I/O in half.

// Winning plan in a covered query:
// "stage": "PROJECTION_COVERED",
// "totalDocsExamined": 0,
// "totalKeysExamined": 1,
// "nReturned": 1
Production Warning: Never project fields not present in the index if aiming for covered query execution.

7. Architectural Trade-offs

Advantages
  • •Eliminates the FETCH stage, resulting in maximum query throughput and lowest possible latency
  • •totalDocsExamined is exactly 0: zero document cache pages loaded
  • •Dramatically reduces working set memory pressure on high-frequency read APIs
Disadvantages & Gotchas
  • •Requires wide compound indexes that consume more RAM to store all projected fields
  • •Does not work on multikey (array) indexes or embedded subdocument queries

8. Technical Interview Questions

Q1: What two conditions are required for a query to be considered "Covered" in MongoDB?

1. All fields present in the query filter and sort must be part of an index. 2. All fields returned in the projection must be present in that same index, and the _id field must be explicitly suppressed ({ _id: 0 }) unless _id is an indexed component.

9. Staff-Level Ready Answer

"A covered query is fulfilled entirely from the index B-Tree without touching the collection document store. It requires all filtered and projected fields to be in the index with _id: 0 suppressed, resulting in totalDocsExamined: 0 in explain plan execution stats."

10. Quick Knowledge Check

Which explain() metric definitively proves that a query was executed as a Covered Query?