Tuning
MongoDB tuning involves a holistic approach, focusing on data modeling (embedding vs. referencing), indexing (creating efficient indexes for queries), query optimization (using
explain() for slow queries, projection), server configuration (memory, storage, concurrency settings like tickets, connection pooling), and monitoring (profiler, Atlas tools) to ensure optimal performance, scalability, and resource usage for your specific application needs. This video provides an overview of the MongoDB architecture for query performance:
Key Areas for Tuning
- Data Modeling & Indexing:
- Embed vs. Reference: Model data for common access patterns, embedding related data for fewer reads, but referencing when data is shared or grows large (minimizing document size).
- Index Strategically: Create indexes on fields used in
find(),sort(), andaggregate()stages. Use compound indexes for multi-field queries. - Avoid Collection Scans: Use
explain()to ensure queries use index scans, not full collection scans.
This video explains the importance of indexes for performance:
- Query & Aggregation Optimization:
- Use the Profiler: Enable the database profiler to find slow queries exceeding a threshold.
explain()Plan: Analyzeexplain()output to see if indexes are used, how many documents are examined, and identify bottlenecks.- Projection: Use
projectionto return only needed fields, reducing network traffic and memory. - Aggregation Pipelines: Optimize stages, push filters down, and use appropriate operators for complex data processing.
This video demonstrates how to use the profiler and analyze query plans:
- Server & Storage Engine Tuning:
- WiredTiger Tickets: Adjust
wiredTigerTicketValuesfor read/write concurrency if operations queue up (tickets hit 0). - Memory: Configure
storage.wiredTiger.engineConfig.cacheSizeGBfor optimal WiredTiger cache size. - Compression: Enable compression (e.g., Snappy, zlib) to reduce I/O and storage, often improving performance.
- WiredTiger Tickets: Adjust
This video discusses memory settings and their impact on performance:
- Connection Management:
- Connection Pooling: Tune
minPoolSize,maxPoolSize, andsocketTimeoutMSin your drivers to match application load and network conditions.
- Connection Pooling: Tune
This video explores patterns for tuning MongoDB performance and scalability:
- Hardware & OS (Advanced):
- NUMA Settings: Configure BIOS/OS settings (like
iommu=pt) for optimal CPU/memory interaction on NUMA systems.
- NUMA Settings: Configure BIOS/OS settings (like
General Approach
- Monitor: Use MongoDB Atlas metrics or tools like
mongostat,mongotop, and the profiler. - Methodical Changes: Apply changes one at a time and measure the impact.
- Balance: Indexing speeds up reads but slows writes; find the right balance for your workload.









