Evaluate Performance of Current Operations
On this page
The following sections describe techniques for evaluating operational performance.
Use the Database Profiler to Evaluate Operations Against the Database
MongoDB provides a database profiler that shows performance characteristics of each operation against the database. Use the profiler to locate any queries or write operations that are running slow. You can use this information, for example, to determine what indexes to create.
Starting in MongoDB 4.2, the profiler entries and the diagnostic log messages (i.e. mongod/mongos log messages) for read/write operations include:
queryHash
to help identify slow queries with the same query shape.planCacheKey
to provide more insight into the query plan cache for slow queries.
Starting in version 4.2, secondary members of a replica set now log oplog entries that take longer than the slow operation threshold to apply. These slow oplog messages:
Are logged for the secondaries in the
diagnostic log
.Are logged under the
REPL
component with the textapplied op: <oplog entry> took <num>ms
.Do not depend on the log levels (either at the system or component level)
Do not depend on the profiling level.
May be affected by
slowOpSampleRate
, depending on your MongoDB version:In MongoDB 4.2, these slow oplog entries are not affected by the
slowOpSampleRate
. MongoDB logs all slow oplog entries regardless of the sample rate.In MongoDB 4.4 and later, these slow oplog entries are affected by the
slowOpSampleRate
.
The profiler does not capture slow oplog entries.
For more information, see Database Profiler.
Use db.currentOp()
to Evaluate mongod
Operations
The db.currentOp()
method reports on current operations
running 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 index
MongoDB selected to fulfill the query and execution statistics. You can
run the methods in queryPlanner
mode, executionStats mode, or
allPlansExecution mode to
control the amount of information returned.
Example
To use cursor.explain()
on a query
for documents matching the expression { a: 1 }
, in the
collection named records
, use an operation that resembles the
following in mongosh
:
db.records.find( { a: 1 } ).explain("executionStats")
Starting in MongoDB 4.2, the explain output includes:
queryHash
to help identify slow queries with the same query shape.planCacheKey
to provide more insight into the query plan cache for slow queries.
For more information, see Explain Results,
cursor.explain()
, db.collection.explain()
, and
Analyze Query Performance.