Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Yandex Managed Service for MongoDB
  • Getting started
    • All tutorials
    • Sharding collections
    • Migrating data to Managed Service for MongoDB
    • Migrating Managed Service for MongoDB cluster from 4.4 to 6.0
    • Migrating collections from a third-party MongoDB cluster
    • MongoDB performance analysis and tuning
    • Delivering data from Yandex Managed Service for Apache Kafka® using Yandex Data Transfer
  • Access management
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes

In this article:

  • Getting started
  • Diagnosing resource shortages
  • Troubleshooting resource shortage issues
  • Diagnosing inefficient query execution
  • Troubleshooting issues with inefficient queries
  • Diagnosing locks
  • Troubleshooting locking issues
  • Diagnosing insufficient disk space
  • Troubleshooting disk space issues
  1. Tutorials
  2. MongoDB performance analysis and tuning

MongoDB performance analysis and tuning

Written by
Yandex Cloud
Updated at October 28, 2024
  • Getting started
  • Diagnosing resource shortages
  • Troubleshooting resource shortage issues
  • Diagnosing inefficient query execution
  • Troubleshooting issues with inefficient queries
  • Diagnosing locks
  • Troubleshooting locking issues
  • Diagnosing insufficient disk space
  • Troubleshooting disk space issues

In this tutorial, you will learn how to:

  • Use performance diagnostic tools and monitoring tools.
  • Troubleshoot identified issues.

Managed Service for MongoDB cluster performance drops most often due to one of the following:

  • High CPU and disk I/O utilization.
  • Inefficient query execution in MongoDB.
  • Locks.
  • Insufficient disk space.

Here are some tips for diagnosing and fixing these issues.

Getting startedGetting started

  1. Install the mongostat and mongotop utilities on an external host with network access to your MongoDB host (see Pre-configuring a connection to a MongoDB cluster) to receive MongoDB performance data.
  2. Determine which databases need to be checked for issues.
  3. Create a MongoDB user with the mdbMonitor role for these databases. You need to do this in order to use mongostat and mongotop.

Diagnosing resource shortagesDiagnosing resource shortages

If some of the CPU and disk I/O resources "hit a plateau", that is, the curve of the graph that was previously steadily ascending has leveled off, it's probably due to a resource shortage, which led to reduced performance. This usually happens when the resource usage reaches its limit.

In most cases, high CPU utilization and high Disk IO are due to suboptimal indexes or a large load on the hosts.

Start diagnostics by identifying the load pattern and problematic collections. Use the built-in MongoDB monitoring tools. Next, analyze the performance of specific queries using logs or profiler data.

Pay attention to queries:

  • Not using indexes (planSummary: COLLSCAN). Such queries may affect both I/O consumption (more reads from the disk) and CPU consumption (data is compressed by default and decompression is required for it). If the required index is present, but the database does not use it, you can force its usage with hint.
  • With large docsExamined values (number of scanned documents). This may mean that the currently running indexes are inefficient or additional ones are required.

As soon as performance drops, you can diagnose the problem in real time using a list of currently running queries:

Queries from all users
Queries from the current user

To run these queries, users needs the mdbMonitor role.

  • Long queries, such as those taking more than one second to execute:

    db.currentOp({"active": true, "secs_running": {"$gt": 1}})
    
  • Queries to create indexes:

    db.currentOp({ $or: [{ op: "command", "query.createIndexes": { $exists: true } }, { op: "none", ns: /\.system\.indexes\b/ }] })
    
  • Long queries, such as those taking more than one second to execute:

    db.currentOp({"$ownOps": true, "active": true, "secs_running": {"$gt": 1}})
    
  • Queries to create indexes:

    db.currentOp({ "$ownOps": true, $or: [{ op: "command", "query.createIndexes": { $exists: true } }, { op: "none", ns: /\.system\.indexes\b/ }] })
    

See also the examples in the MongoDB documentation.

Troubleshooting resource shortage issuesTroubleshooting resource shortage issues

Try optimizing the identified queries. If the load is still high or there is nothing to optimize, the only option is to upgrade the host class.

Diagnosing inefficient query executionDiagnosing inefficient query execution

To identify problematic queries in MongoDB:

  • Review the logs. Pay special attention to:

    • For read queries, the responseLength field (written as reslen in the logs).
    • For write queries, the number of affected documents.
      In the cluster logs, they are displayed in the nModified, keysInserted, and keysDeleted fields. On the cluster monitoring page, analyze the Documents affected on primary, Documents affected on secondaries, and Documents affected per host graphs.
  • Review the profiler data. Output long-running queries (adjustable with the slowOpThreshold DBMS setting).

Troubleshooting issues with inefficient queriesTroubleshooting issues with inefficient queries

Each individual query can be analyzed in terms of the query plan. Learn more about this in the MongoDB documentation:

  • Query analysis guide.
  • Reference for the db.collection.explain function.

Analyze the graphs on the cluster monitoring page:

  • Index size on primary, top 5 indexes.
  • Scan and order per host.
  • Scanned / returned.

To more quickly narrow down the search scope, use indexes.

Warning

Each new index slows down writes. Too many indexes may negatively affect write performance.

To optimize read requests, use projection. In many cases, you need to return only a few fields rather than the entire document.

If you can neither optimize the queries you found nor go without them, upgrade the host class.

Diagnosing locksDiagnosing locks

Poor query performance can be caused by locks.

MongoDB does not provide detailed information on locks. There are only indirect ways to find out what is locking a specific query:

  • Pay attention to large or growing db.serverStatus().metrics.operation.writeConflicts values: they may indicate high write contention on some documents.

  • Examine large or growing values using the Write conflicts per hosts graph on the cluster monitoring page.

  • As soon as performance drops, carefully review the list of currently running queries:

    Queries from all users
    Queries from the current user

    To run these queries, the user needs the mdbMonitor role.

    • Find queries that hold exclusive locks, such as:

      db.currentOp({'$or': [{'locks.Global': 'W'}, {'locks.Database': 'W'}, {'locks.Collection': 'W'} ]}).inprog
      
    • Find queries waiting for locks (the timeAcquiringMicros field shows the waiting time):

      db.currentOp({'waitingForLock': true}).inprog
      db.currentOp({'waitingForLock': true, 'secs_running' : { '$gt' : 1 }}).inprog
      
    • Find queries that hold exclusive locks, such as:

      db.currentOp({"$ownOps": true, '$or': [{'locks.Global': 'W'}, {'locks.Database': 'W'}, {'locks.Collection': 'W'} ]}).inprog
      
    • Find queries waiting for locks (the timeAcquiringMicros field shows the waiting time):

      db.currentOp({"$ownOps": true, 'waitingForLock': true}).inprog
      db.currentOp({"$ownOps": true, 'waitingForLock': true, 'secs_running' : { '$gt' : 1 }}).inprog
      
  • Pay attention to the following in the logs and profiler:

    • Queries that had waited a long time to get locks will have large timeAcquiringMicros values.
    • Queries that had competed for the same documents will have large writeConflicts values.

Learn more about which locks are used by standard client and administrative queries in the official MongoDB documentation.

Troubleshooting locking issuesTroubleshooting locking issues

Detected locks indicate unoptimized queries. Try optimizing problematic queries.

Diagnosing insufficient disk spaceDiagnosing insufficient disk space

If a cluster shows poor performance combined with a small amount of free disk space, this is probably because one or more hosts in the cluster switched to "read-only".

The amount of used disk space is displayed on the Disk space usage per host, top 5 hosts graphs on the cluster monitoring page.

To monitor storage usage on cluster hosts, configure an alert.

Troubleshooting disk space issuesTroubleshooting disk space issues

For recommendations on troubleshooting these issues, see Maintaining a cluster in operable condition.

Was the article helpful?

Previous
Migrating collections from a third-party MongoDB cluster
Next
Delivering data from Yandex Managed Service for Apache Kafka® using Yandex Data Transfer
© 2025 Direct Cursus Technology L.L.C.