Index strategies
Good indexing means matching indexes to your real queries, keeping few of them, and checking they are used.
In this page:
Index strategies
Start from your slowest, most frequent queries, design compound indexes with the ESR rule, and remove unused indexes found with $indexStats.
Covered queries answer entirely from the index. Every extra index adds write cost and memory pressure, so balance both.
Monitor with the profiler.
Note:
$indexStats shows which indexes are actually being used.
Example: Index strategies
test> db.orders.aggregate([{ $indexStats: {} }, { $project: { name: 1, "accesses.ops": 1 } }])
[
{ name: '_id_', accesses: { ops: Long('1204') } },
{ name: 'status_1_createdAt_-1', accesses: { ops: Long('9821') } },
{ name: 'legacy_flag_1', accesses: { ops: Long('0') } }
]
test> db.orders.dropIndex("legacy_flag_1")
{ nIndexesWas: 3, ok: 1 }
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Indexing without checking real queries
- Keeping unused indexes
- Ignoring memory limits for indexes
Chapter Summary
- Index for real query patterns
- Use ESR for compound indexes
- Drop unused indexes
- Covered queries read only the index
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: