Index Strategy and Query Planner Validation
Learners will define the full index set for the schema, validate each index with explain(), and prune redundant indexes.
Index Strategy Overview
An index strategy is a deliberate plan for which indexes to create, not an ad-hoc collection of indexes added whenever a query is slow. Every index has a cost: it speeds up reads but slows down writes (each write must update all indexes on the collection) and consumes RAM (indexes must fit in the working set). A good strategy creates the minimum number of indexes that cover all high-frequency access patterns.
Start With the Access Pattern Register
Map every high-frequency access pattern identified during requirements analysis to a proposed index. Document the index fields, sort direction, and purpose. For our e-commerce platform: the product page uses { slug: 1 }; the category listing uses { categoryId: 1, price: 1, _id: 1 } for keyset pagination; the order history uses { userId: 1, createdAt: -1 }. This register prevents redundant indexes and missed coverage.
// Index register for e-commerce capstone
const indexRegister = [
{ collection: 'products', index: { slug: 1 }, unique: true, covers: 'product page' },
{ collection: 'products', index: { categoryId: 1, price: 1, _id: 1 }, unique: false, covers: 'category listing + keyset' },
{ collection: 'products', index: { 'vendor._id': 1 }, unique: false, covers: 'vendor store page' },
{ collection: 'orders', index: { userId: 1, createdAt: -1 }, unique: false, covers: 'user order history' },
{ collection: 'orders', index: { status: 1, createdAt: 1 }, unique: false, covers: 'fulfillment queue' },
{ collection: 'reviews', index: { productId: 1, createdAt: -1 }, unique: false, covers: 'reviews by product' }
]All lessons in this course
- Requirements Analysis and Schema Design
- Index Strategy and Query Planner Validation
- Scaling Plan: Replica Set to Sharded Cluster
- Security Hardening and Production Checklist