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.
Index Traversal & IXSCAN vs COLLSCAN Execution Plan
Topic 24: Covered Queries & Zero-Document Heap Scans
{
"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.
{
"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
db.users.createIndex({ username: 1, status: 1, email: 1 });db.users.find(
{ username: "alice", status: "active" },
{ _id: 0, email: 1, username: 1 } // _id: 0 is mandatory!
).explain("executionStats");5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| totalDocsExamined: 0 | The definitive indicator of a covered query in executionStats. |
| _id Suppression Mandatory | Unless _id is part of the index, the projection must include _id: 0. |
| No Multikey Indexes | An 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
7. Architectural Trade-offs
- •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
- •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
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?