Labs ICT
Pro Login

Performance Tuning

Optimizing your MongoDB deployment.

Performance Monitoring

Monitoring is the first step to performance tuning. You can't fix what you can't measure. MongoDB provides several tools to track how your database is performing.

The serverStatus command gives you a snapshot of everything happening in your database. It shows connections, operations, memory usage, and much more.

MongoDB Atlas and Cloud Manager provide dashboards with visualizations of these metrics. For self-managed deployments, tools like Grafana with MongoDB exporters work great.

db.serverStatus()

db.serverStatus().opcounters

db.serverStatus().mem

db.currentOp()

db.serverStatus({ opLatencies: 1 }).opLatencies

Query Optimization

The explain() method shows you how MongoDB executes a query. It reveals which indexes are used, how many documents are scanned, and where bottlenecks exist.

Look for COLLSCAN (collection scan) in the output. That means MongoDB is scanning every document in the collection. Add an index to fix that.

The executionStats mode gives you the most detail. Pay attention to totalDocsExamined versus nReturned. If examined is much higher than returned, your query needs optimization.

db.orders.find({ status: "pending" }).explain("executionStats")

db.users.find({ email: "alice@example.com" }).explain()

db.products.find({ price: { $gt: 100 }, category: "electronics" }).explain("allPlans")

Index Optimization

Indexes are the most important factor in query performance. The right index can turn a slow query into a fast one. The wrong index, or no index at all, can bring your database to its knees.

MongoDB automatically creates an index on _id, but you need to create indexes for your common query patterns. Use the explain() method to identify which fields need indexes.

Compound indexes cover multiple fields. The order of fields matters: put the most selective field first. A compound index on {status: 1, date: -1} works great for queries that filter by status and sort by date.

db.orders.createIndex({ status: 1, createdAt: -1 })

db.products.createIndex({ name: "text", description: "text" })

db.users.createIndex({ email: 1 }, { unique: true })

db.orders.getIndexes()

db.orders.aggregate([{ $indexStats: {} }])

Server Configuration

MongoDB's configuration file controls many performance-related settings. Tweaking these can significantly improve performance for your specific workload.

The wiredTigerCacheSizeGB setting controls how much memory MongoDB uses for caching. The default is fine for most systems, but you can increase it on dedicated servers with lots of RAM.

Connection pool size, thread counts, and journal settings also affect performance. Don't change these without understanding their impact. Start with defaults and adjust based on monitoring data.

db.adminCommand({ setParameter: 1, wiredTigerEngineRuntimeConfig: "cache_size=2GB" })

db.adminCommand({ setParameter: 1, maxIncomingConnections: 5000 })

db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false })

db.adminCommand({ setParameter: 1, transactionLifetimeLimitSeconds: 300 })