Evaluate Performance of Current Operations

The following sections describe techniques for evaluating operationalperformance.

Use the Database Profiler to Evaluate Operations Against the Database

MongoDB provides a database profiler that shows performancecharacteristics of each operation against the database. Use the profilerto locate any queries or write operations that are running slow. You canuse this information, for example, to determine what indexes to create.

Starting in MongoDB 4.2, the profiler entries and the diagnosticlog messages (i.e. mongod/mongos logmessages) for read/write operations include:

  • queryHash to help identify slow queries with the samequery shape.
  • planCacheKey to provide more insight into the query plancache for slow queries.

Starting in version 4.2 (also available starting in 4.0.6), secondary members of a replica set nowlog oplog entries that take longer than the slowoperation threshold to apply. These slow oplog messages are loggedfor the secondaries in the diagnostic log under the REPL component with the text appliedop: <oplog entry> took <num>ms. These slow oplog entries dependonly on the slow operation threshold. They do not depend on the loglevels (either at the system or component level), or the profilinglevel, or the slow operation sample rate. The profiler does notcapture slow oplog entries.

For more information, see Database Profiler.

Use db.currentOp() to Evaluate mongod Operations

The db.currentOp() method reports on current operationsrunning on a mongod instance.

Use explain to Evaluate Query Performance

The cursor.explain() and db.collection.explain()methods return information on a query execution, such as the indexMongoDB selected to fulfill the query and execution statistics. You canrun the methods in queryPlannermode, executionStats mode, orallPlansExecution mode tocontrol the amount of information returned.

Example

To use cursor.explain() on a queryfor documents matching the expression { a: 1 }, in thecollection named records, use an operation that resembles thefollowing in the mongo shell:

  1. db.records.find( { a: 1 } ).explain("executionStats")

Starting in MongoDB 4.2, the explain output includes:

For more information, see Explain Results,cursor.explain(), db.collection.explain(), andAnalyze Query Performance.