Why Unindexed Queries Cripple Scaling Applications
As your database collections grow past millions of documents, COLLSCAN (collection scans) exhaust CPU and memory bandwidth.
The Anatomy of Compound Indexes (ESR Rule)
When designing compound indexes, always abide by the ESR Rule:
- Equality: Exact matches first
- Sort: Ordering criteria next
- Range: Range filters ($gt, $lt, $in) last
// Optimal compound index following ESR
schema.index({ status: 1, createdAt: -1, price: 1 });
Analyzing Query Plans
Always verify index utilization with .explain("executionStats"):
- Target
totalDocsExamined / nReturned = 1 - Look for
IXSCANstages and confirm zero temporary in-memory sorts!
