mongodb-indexing-simplified
mongodb-indexing-simplified
Importance:
Without proper indexing, databases can work for small amounts of data but will become
increasingly slow as data grows, eventually causing performance issues that affect the
entire application.
Timing:
Indexes should be created before an application experiences high traffic or stores large
amounts of data, as they form the foundation for efficient data retrieval.
• Example Code:
2. Compound Indexes
• What It Is: An index on multiple fields of a document
• Advantages: Efficient for queries that filter on multiple fields, works for sorting
• Disadvantages: Less efficient for queries that don’t use all indexed fields
• Example Code:
3. Unique Indexes
• What It Is: An index that ensures no two documents have the same value for a field
• Advantages: Enforces data integrity, offers O(1) lookups for unique values
• Example Code:
4. Text Indexes
• What It Is: A specialized index for text search functionality
• Example Code:
5. Geospatial Indexes
• What It Is: Indexes for location-based queries
• Example Code:
Time Complexity
• Without Index (O(n)):
– MongoDB must scan every document
– Query time increases linearly with collection size
– Example: 1 million documents = ~17 minutes for a full scan
• With Index (O(log n)):
– MongoDB uses binary search techniques
– Query time increases logarithmically with collection size
– Example: 1 million documents = ~0.03 seconds
• Perfect Index (O(1)):
– Direct lookup with no search required
– Query time is constant regardless of collection size
– Example: Unique index lookup = instant (milliseconds)
• How to Use:
• Key Metrics:
mongoose.set('debug', true);
db.setProfilingLevel(2);
Resource Usage
• Memory:
– Indexes are stored in memory for faster access
– Each index typically uses 8KB of RAM per document
• Disk Space:
– Indexes typically add 5-10% to database size
– Text indexes can add up to 25% more space
Solutions
1. Analyze Queries: Only create indexes for common queries
2. Use Compound Indexes Wisely: Put most-filtered fields first
3. Audit Existing Indexes: Remove unused or redundant indexes
4. Balance Read/Write: Consider write frequency when indexing
5. Regular Monitoring: Check explain() output regularly
// Compound index
courseSchema.index({ field1: 1, field2: -1 });
// Unique index
courseSchema.index({ field_name: 1 }, { unique: true });
// Text index
courseSchema.index({ field_name: 'text' });
// Geospatial index
courseSchema.index({ location: '2dsphere' });
Final Thought:
Proper indexing is one of the most impactful optimizations you can make to improve
application performance. Taking time to understand and implement effective indexes will
save significant resources and provide a better user experience as your application scales.