Database Profiler

The database profiler collects detailed information aboutDatabase Commands executed against a running mongodinstance. This includes CRUD operations as well as configuration andadministration commands.The profiler writes all the data it collects to thesystem.profile collection, acapped collection in the admindatabase. See Database Profiler Output for an overview of thesystem.profile documents created bythe profiler.

The profiler is off by default. You can enable the profiler on aper-database or per-instance basis at one of several profilinglevels.

This document outlines a number of key administration options for thedatabase profiler. For additional related information, consider thefollowing resources:

Profiling Levels

The following profiling levels are available:

LevelDescription
0The profiler is off and does not collect any data.This is the default profiler level.
1The profiler collects data for operations that take longerthan the value of slowms.
2The profiler collects data for all operations.

Enable and Configure Database Profiling

You can enable database profiling for mongod instances .

This section uses the mongo shell helperdb.setProfilingLevel() helper to enable profiling. Forinstructions using the driver, see your driverdocumentation.

When you enable profiling for a mongod instance, you setthe profiling level to a valuegreater than 0. The profiler records data in the system.profile collection. MongoDB creates thesystem.profile collection in adatabase after you enable profiling for that database.

To enable profiling and set the profiling level, pass the profilinglevel to the db.setProfilingLevel() helper. For example, toenable profiling for all database operations, consider the followingoperation in the mongo shell:

  1. db.setProfilingLevel(2)

The shell returns a document showing the previous level of profiling.The "ok" : 1 key-value pair indicates the operation succeeded:

  1. { "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }

To verify the new setting, see theCheck Profiling Level section.

Specify the Threshold for Slow Operations

By default, the slow operation threshold is 100 milliseconds. To changethe slow operation threshold, specify the desired threshold value inone of the following ways:

For example, the following code sets the profiling level for thecurrent mongod instance to 1 and sets the slowoperation threshold for the mongod instance to 20milliseconds:

  1. db.setProfilingLevel(1, { slowms: 20 })

Profiling level of 1 will profile operations slower than thethreshold.

Important

The slow operation threshold applies to all databases in amongod instance. It is used by both the database profilerand the diagnostic log and should be set to the highest useful value toavoid performance degradation.

Starting in MongoDB 4.0, you can use db.setProfilingLevel()to configure slowms and sampleRate for mongos.For the mongos, the slowms and sampleRateconfiguration settings only affect the diagnostic log and not theprofiler since profiling is not available on mongos.[1]

For example, the following sets a mongos instance’s slowoperation threshold for logging slow operations:

  1. db.setProfilingLevel(0, { slowms: 20 })

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.

Profile a Random Sample of Slow Operations

New in version 3.6.

To profile only a randomly sampled subset of all slow operations ,specify the desired sample rate in one of the following ways:[2]

By default, sampleRate is set to 1.0, meaning all slow_operations are profiled. When sampleRate is set between 0 and 1,databases with profiling level 1 will only profile a randomly sampledpercentage of _slow operations according to sampleRate.

For example, the following method sets the profiling level for themongod to 1 and sets the profiler to sample 42% ofall slow operations:

  1. db.setProfilingLevel(1, { sampleRate: 0.42 })

The modified sample rate value also applies to the system log.

Starting in MongoDB 4.0, you can use db.setProfilingLevel()to configure slowms and sampleRate for mongos.For the mongos, the slowms andsampleRate configuration settings only affect the diagnostic logand not the profiler since profiling is not available onmongos. [1]

For example, the following sets a mongos instance’ssampling rate for logging slow operations:

  1. db.setProfilingLevel(0, { sampleRate: 0.42 })

Important

When logLevel is set to 0, MongoDB records _slow_operations to the diagnostic log at a rate determined byslowOpSampleRate. Starting in MongoDB4.2, the secondaries of replica sets log all oplog entry messagesthat take longer than the slow operation threshold to apply regardless of the sample rate.

At higher logLevel settings, all operations appear inthe diagnostic log regardless of their latency with the followingexception: the logging of slow oplog entry messages by thesecondaries. The secondaries log only the slow oplogentries; increasing the logLevel does not log alloplog entries.

  • :binary:~bin.mongod or :binary:~bin.</code><code>mongos
[1](1, 2) See Database Profiling and Sharding.

Check Profiling Level

To view the profiling level, issuethe following from the mongo shell:

  1. db.getProfilingStatus()

The shell returns a document similar to the following:

  1. { "was" : 0, "slowms" : 100, "sampleRate" : 1.0, "ok" : 1 }

The was field indicates the current profiling level.

The slowms field indicates operation time threshold, inmilliseconds, beyond which operations are considered slow.

The sampleRate field indicates the percentage of slow operationsthat should be profiled.

To return only the profiling level, use thedb.getProfilingLevel() helper in the mongo shell as in the following:

  1. db.getProfilingLevel()

Disable Profiling

To disable profiling, use the following helper in the mongoshell:

  1. db.setProfilingLevel(0)

Enable Profiling for an Entire mongod Instance

For development purposes in testing environments, you can enabledatabase profiling for an entire mongod instance. Theprofiling level applies to all databases provided by themongod instance.

To enable profiling for a mongod instance, pass the followingoptions to mongod at startup.

  1. mongod --profile 1 --slowms 15 --slowOpSampleRate 0.5

Alternatively, you can specify operationProfiling in the configurationfile.

This sets the profiling level to 1, defines slow operations as thosethat last longer than 15 milliseconds, and specifies that only 50%of slow operations should be profiled. [2]

The slowms and slowOpSampleRate also affect which operationsare recorded to the diagnostic log when logLevel isset to 0. The slowms and slowOpSampleRate are alsoavailable to configure diagnostic logging for mongos. [2]

See also

mode,slowOpThresholdMs, andslowOpSampleRate.

Database Profiling and Sharding

You cannot enable profiling on a mongos instance. To enableprofiling in a sharded cluster, you must enable profiling for eachmongod instance in the cluster.

However, starting in MongoDB 4.0, you can set the —slowms and slowOpSampleRate on mongos to configure the diagnosticlog for slow operations.

View Profiler Data

The database profiler logs information about database operations in thesystem.profile collection.

To view profiling information, query the system.profile collection. To view example queries, seeExample Profiler Data Queries. For an explanation of theoutput data, see Database Profiler Output.

Tip

You can use $comment to add data to the query predicate tomake it easier to analyze data from the profiler.

Example Profiler Data Queries

This section displays example queries to the system.profilecollection. For an explanation of the query output, seeDatabase Profiler Output.

To return the most recent 10 log entries in the system.profilecollection, run a query similar to the following:

  1. db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty()

To return all operations except command operations ($cmd), run a querysimilar to the following:

  1. db.system.profile.find( { op: { $ne : 'command' } } ).pretty()

To return operations for a particular collection, run a query similar tothe following. This example returns operations in the mydb database’stest collection:

  1. db.system.profile.find( { ns : 'mydb.test' } ).pretty()

To return operations slower than 5 milliseconds, run a querysimilar to the following:

  1. db.system.profile.find( { millis : { $gt : 5 } } ).pretty()

To return information from a certain time range, run a query similar tothe following:

  1. db.system.profile.find({
  2. ts : {
  3. $gt: new ISODate("2012-12-09T03:00:00Z"),
  4. $lt: new ISODate("2012-12-09T03:40:00Z")
  5. }
  6. }).pretty()

The following example looks at the time range, suppresses the userfield from the output to make it easier to read, and sorts the resultsby how long each operation took to run:

  1. db.system.profile.find({
  2. ts : {
  3. $gt: new ISODate("2011-07-12T03:00:00Z"),
  4. $lt: new ISODate("2011-07-12T03:40:00Z")
  5. }
  6. }, { user: 0 }).sort( { millis: -1 } )

Show the Five Most Recent Events

On a database that has profiling enabled, the show profile helperin the mongo shell displays the 5 most recent operationsthat took at least 1 millisecond to execute. Issue show profilefrom the mongo shell, as follows:

  1. show profile

Profiler Overhead

When enabled, profiling has a minor effect on performance. Thesystem.profile collection is acapped collection with a default size of 1 megabyte. Acollection of this size can typically store several thousand profiledocuments, but some applications may use more or less profiling data peroperation.

Change Size of system.profile Collection on the Primary

To change the size of the system.profile collection, you must:

  • Disable profiling.
  • Drop the system.profilecollection.
  • Create a new system.profilecollection.
  • Re-enable profiling.For example, to create a new system.profile collections that’s 4000000 bytes, usethe following sequence of operations in the mongo shell:
  1. db.setProfilingLevel(0)
  2.  
  3. db.system.profile.drop()
  4.  
  5. db.createCollection( "system.profile", { capped: true, size:4000000 } )
  6.  
  7. db.setProfilingLevel(1)

Change Size of system.profile Collection on a Secondary

To change the size of the system.profile collection on a secondary, you muststop the secondary, run it as a standalone, and then perform the stepsabove. When done, restart the standalone as a member of the replica set.For more information, see Perform Maintenance on Replica Set Members.

[2](1, 2, 3) 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.