Docs HomeMongoDB Manual

Database Profiler数据库档案器

The database profiler collects detailed information about Database Commands executed against a running mongod instance. 数据库探查器集合有关针对正在运行的mongod实例执行的数据库命令的详细信息。This includes CRUD operations as well as configuration and administration commands. 这包括CRUD操作以及配置和管理命令。The profiler writes all the data it collects to a system.profile collection, a capped collection in each profiled database. 探查器将其集合的所有数据写入一个system.profile集合,该集合是每个已分析数据库中的一个封顶集合See Database Profiler Output for an overview of the system.profile documents created by the profiler.有关探查器创建的system.profile文档的概述,请参阅数据库探查器输出

The profiler is off by default. You can enable the profiler on a per-database or per-instance basis at one of several profiling levels.探查器在默认情况下处于off状态。您可以在多个分析级别中的一个级别上按数据库或按实例启用探查器。

When enabled, profiling has an effect on database performance and disk use. See Database Profiler Overhead for more information.启用后,分析会对数据库性能和磁盘使用产生影响。有关详细信息,请参阅数据库探查器开销

This document outlines a number of key administration options for the database profiler. For additional related information, see:本文档概述了数据库探查器的一些关键管理选项。有关其他相关信息,请参阅:

Warning

Do not attempt to create a time series collection or view with the name system.profile. 请勿尝试创建名为system.profile的时间序列集合或视图。MongoDB 6.3 and later versions return an IllegalOperation error if you attempt to do so. 如果您尝试这样做,MongoDB 6.3及更高版本会返回一个IllegalOperation错误。Earlier MongoDB versions crash.早期的MongoDB版本崩溃。

Profiling Levels分析级别

The following profiling levels are available:以下配置文件级别可用:

Level数量Description描述
0The profiler is off and does not collect any data. This is the default profiler level.探查器已关闭,不集合任何数据。这是默认的探查器级别。
1The profiler collects data for operations that take longer than the value of slowms or that match a filter.探查器为耗时超过slowms值或与筛选器匹配的操作集合数据。
When a filter is set: 设置筛选器时:
  • The slowms and sampleRate options are not used for profiling.slowmssampleRate选项不用于分析。
  • The profiler only captures operations that match the filter.探查器仅捕获与筛选器匹配的操作。
2The profiler collects data for all operations.探查器集合所有操作的数据。

Enable and Configure Database Profiling启用和配置数据库分析

You can enable database profiling for mongod instances.您可以为mongod实例启用数据库评测。

This section uses mongosh helper db.setProfilingLevel() helper to enable profiling. 本节使用mongosh助手db.setProfilingLevel()助手来启用评测。For instructions using the driver, see your driver documentation.有关使用驱动程序的说明,请参阅驱动程序文档

When you enable profiling for a mongod instance, you set the profiling level to a value greater than 0. The profiler records data in the system.profile collection. 当您为mongod实例启用评测时,您将评测级别设置为大于0的值。探查器在system.profile集合中记录数据。MongoDB creates the system.profile collection in a database after you enable profiling for that database.MongoDB在为数据库启用概要分析后,在该数据库中创建system.profile集合。

To enable profiling and set the profiling level, pass the profiling level to the db.setProfilingLevel() helper. 要启用评测并设置评测级别,请将评测级别传递给db.setProfilingLevel()帮助程序。For example, to enable profiling for all database operations, consider the following operation in mongosh:例如,要为所有数据库操作启用概要分析,请考虑mongosh中的以下操作:

db.setProfilingLevel(2)

The shell returns a document showing the previous level of profiling. shell返回一个显示上一级评测的文档。The "ok" : 1 key-value pair indicates the operation succeeded:"ok" : 1键值对表示操作成功:

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

To verify the new setting, see the Check Profiling Level section.要验证新设置,请参阅检查分析级别部分。

Starting in MongoDB 5.0 (also available starting in 4.4.2, and 4.2.12), changes made to the database profiler level, slowms, sampleRate, or filter using the profile command or db.setProfilingLevel() wrapper method are recorded in the log file.从MongoDB 5.0开始(也可从4.4.2和4.2.12开始使用),使用profile命令或dbsetProfileLevel()包装方法对数据库探查器levelslowmssampleRatefilter所做的更改将记录在日志文件中。

Global and Per-Database Profiling Settings全局和每个数据库的分析设置

The slowms and sampleRate profiling settings are global. slowmssampleRate分析设置是全局设置。When set, these settings affect all databases in your process.设置后,这些设置将影响进程中的所有数据库。

When set via the profile command or db.setProfilingLevel() shell helper method, profiling level and filter settings are set at the database level. 当通过profile命令或db.setProfilingLevel()shell助手方法进行设置时,将在数据库级别设置概要文件级别和筛选器设置。When set as either command line or configuration file options, profiling level and filter settings affect the entire process.当设置为命令行或配置文件选项时,分析级别和filter设置会影响整个过程。

Specify the Threshold for Slow Operations指定慢速操作的阈值

By default, the slow operation threshold is 100 milliseconds. To change the slow operation threshold, specify the desired threshold value in one of the following ways:默认情况下,慢速操作阈值为100毫秒。要更改慢速操作阈值,请通过以下方式之一指定所需的阈值:

For example, the following code sets the profiling level for the current mongod instance to 1 and sets the slow operation threshold for the mongod instance to 20 milliseconds:例如,以下代码将当前mongod实例的评测级别设置为1,并将mongod示例的慢操作阈值设置为20毫秒:

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

Profiling level of 1 will profile operations slower than the threshold.分析级别为1将分析低于阈值的操作。

Important

The slow operation threshold applies to all databases in a mongod instance. 慢速操作阈值适用于mongod实例中的所有数据库。It is used by both the database profiler and the diagnostic log and should be set to the highest useful value to avoid performance degradation.它由数据库探查器和诊断日志使用,并且应该设置为最高有用值以避免性能下降。

Starting in MongoDB 4.0, you can use db.setProfilingLevel() to configure slowms and sampleRate for mongos. 从MongoDB 4.0开始,您可以使用db.setProfilingLevel()mongos配置slowmssampleRateFor the mongos, the slowms and sampleRate configuration settings only affect the diagnostic log and not the profiler since profiling is not available on mongos. 对于mongosslowmssampleRate配置设置只影响诊断日志,而不影响探查器,因为在mongos上无法进行评测。[1]

For example, the following sets a mongos instance's slow operation threshold for logging slow operations:例如,以下设置mongos实例的慢速操作阈值,用于记录慢速操作:

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

Starting in MongoDB 4.2, the profiler entries and the diagnostic log messages (i.e. mongod/mongos log messages) for read/write operations include:从MongoDB 4.2开始,探查器条目和用于读/写操作的诊断日志消息(即mongod/mongos日志消息)包括:

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. 从4.2版开始,副本集的辅助成员现在会记录应用时间超过慢速操作阈值的oplog条目These slow oplog messages:这些慢速操作日志消息:

  • Are logged for the secondaries in the diagnostic log.诊断日志中为辅助设备记录。
  • Are logged under the REPL component with the text applied op: <oplog entry> took <num>ms.REPL组件下记录,并应用文本applied 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:可能会受到slowOpSampleRate的影响,具体取决于您的MongoDB版本:

    • 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.在MongoDB 4.2中,这些慢速操作日志条目不受slowOpSampleRate的影响。MongoDB记录所有慢速操作日志条目,而不管采样率如何。
    • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢速操作日志条目受到slowOpSampleRate的影响。

The profiler does not capture slow oplog entries.探查器未捕获慢速操作日志项。

Profile a Random Sample of Slow Operations评测慢速操作的随机样本

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. 默认情况下,sampleRate设置为1.0,这意味着将分析所有慢速操作。When sampleRate is set between 0 and 1, databases with profiling level 1 will only profile a randomly sampled percentage of slow operations according to sampleRate.sampleRate设置在0和1之间时,分析级别为1的数据库将仅根据sampleRate慢速操作的随机采样百分比进行分析。

For example, the following method sets the profiling level for the mongod to 1 and sets the profiler to sample 42% of all slow operations:例如,以下方法将mongod的评测级别设置为1,并将评测器设置为对所有慢速操作的42%进行采样:

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. 从MongoDB 4.0开始,您可以使用db.setProfilingLevel()mongos配置slowmssampleRateFor the mongos, the slowms and sampleRate configuration settings only affect the diagnostic log and not the profiler since profiling is not available on mongos. 对于mongosslowmssampleRate配置设置只影响诊断日志,而不影响探查器,因为在mongos上无法进行评测。[1]

For example, the following sets a mongos instance's sampling rate for logging slow operations:例如,以下设置mongos实例的采样率,用于记录慢速操作:

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 by slowOpSampleRate.logLevel设置为0时,MongoDB会以slowOpSampleRate确定的速率将慢速操作记录到诊断日志中。

At higher logLevel settings, all operations appear in the diagnostic log regardless of their latency with the following exception: the logging of slow oplog entry messages by the secondaries. 在更高的logLevel设置下,所有操作都会显示在诊断日志中,无论其延迟如何,但以下情况除外:由辅助设备记录慢速操作日志条目消息The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.辅助设备只记录慢速操作日志条目;增加logLevel不会记录所有oplog条目。

[1](1, 2) See Database Profiling and Sharding.请参阅数据库分析和共享

Set a Filter to Determine Profiled Operations设置筛选器以确定分析的操作

New in version 4.4.2. 4.4.2版新增。

You can set a filter to control which operations are profiled and logged. 您可以设置一个筛选器来控制对哪些操作进行分析和记录。You can set the profiling filter in one of the following ways:您可以通过以下方式之一设置分析筛选器:

For mongod instances, the filter affects both the diagnostic log and, if enabled, the profiler.对于mongod实例,filter会影响诊断日志,如果启用,还会影响探查器。

For mongos instances, the filter affects the diagnostic log only and not the profiler since profiling is not available on mongos.对于mongos实例,filter只影响诊断日志,而不影响探查器,因为在mongos上无法进行探查。

Note

When a profiling filter is set, the slowms and sampleRate options do not affect the diagnostic log or the profiler.设置分析filter时,slowmssampleRate选项不会影响诊断日志或探查器。

For example, the following db.setProfilingLevel() method sets for a mongod instance:例如,以下db.setProfilingLevel()方法为mongod实例设置:

  • the profiling level to 2,探查器级别2
  • the filter of { op: "query", millis: { $gt: 2000 } }, which causes the profiler to only log query operations that took longer than 2 seconds.{ op: "query", millis: { $gt: 2000 } }筛选器,这将导致探查器只记录耗时超过2秒的query操作。
db.setProfilingLevel( 2, { filter: { op: "query", millis: { $gt: 2000 } } } )

Check Profiling Level检查分析级别

To view the profiling level, issue the following from mongosh:要查看评测级别,请从mongosh发布以下内容:

db.getProfilingStatus()

The shell returns a document similar to the following:shell返回一个类似于以下内容的文档:

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

The was field indicates the current profiling level.was字段表示当前的分析级别。

The slowms field indicates operation time threshold, in milliseconds, beyond which operations are considered slow.slowms字段指示操作时间阈值(以毫秒为单位),超过该阈值的操作将被视为慢速

The sampleRate field indicates the percentage of slow operations that should be profiled.sampleRate字段指示应分析的慢速操作的百分比。

Disable Profiling禁用分析

To disable profiling, use the following helper in mongosh:要禁用分析,请在mongosh中使用以下帮助程序:

db.setProfilingLevel(0)

Enable Profiling for an Entire mongod Instance为整个mongod实例启用分析

For development purposes in testing environments, you can enable database profiling for an entire mongod instance. 为了在测试环境中进行开发,可以为整个mongod实例启用数据库评测。The profiling level applies to all databases provided by the mongod instance.评测级别适用于mongod实例提供的所有数据库。

To enable profiling for a mongod instance, pass the following options to mongod at startup.要启用mongod实例的评测,请在启动时将以下选项传递给mongod

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

Alternatively, you can specify operationProfiling in the configuration file.或者,您可以在配置文件中指定operationProfileing

This sets the profiling level to 1, defines slow operations as those that last longer than 15 milliseconds, and specifies that only 50% of slow operations should be profiled. 这将分析级别设置为1,将慢速操作定义为持续时间超过15毫秒的操作,并指定只应分析50%的慢速操作。[2]

The slowms and slowOpSampleRate also affect which operations are recorded to the diagnostic log when logLevel is set to 0. logLevel设置为0时,slowmsslowOpSampleRate还会影响将哪些操作记录到诊断日志中。The slowms and slowOpSampleRate are also available to configure diagnostic logging for mongos. slowmsslowOpSampleRate也可用于配置mongos的诊断日志记录。[2]

Tip

See also: 另请参阅:

Database Profiling and Sharding数据库分析和共享

You cannot enable profiling on a mongos instance. 您不能在mongos实例上启用分析。To enable profiling in a sharded cluster, you must enable profiling for each mongod instance in the cluster.要在分片集群中启用评测,必须为集群中的每个mongod实例启用评测。

However, starting in MongoDB 4.0, you can set the --slowms and slowOpSampleRate on mongos to configure the diagnostic log for slow operations.但是,从MongoDB 4.0开始,您可以在mongos上设置--slowmsslowOpSampleRate来配置慢速操作的诊断日志。

View Profiler Data查看档案器数据

The database profiler logs information about database operations in the system.profile collection.数据库探查器将有关数据库操作的信息记录在system.profile集合中。

To view profiling information, query the system.profile collection. 要查看配置文件信息,请查询system.profile集合。To view example queries, see Example Profiler Data Queries. 要查看示例查询,请参阅示例探查器数据查询For an explanation of the output data, see Database Profiler Output.有关输出数据的说明,请参阅数据库档案器输出

Starting in MongoDB 4.4, it is no longer possible to perform any operation, including reads, on the system.profile collection from within a transaction.从MongoDB 4.4开始,不再可能在事务中对system.profile集合执行任何操作,包括读取。

Tip

You can use $comment to add data to the query predicate to make it easier to analyze data from the profiler.您可以使用$comment将数据添加到查询谓词,以便更容易地分析探查器中的数据。

Example Profiler Data QueriesProfiler数据查询示例

This section displays example queries to the system.profile collection. For an explanation of the query output, see Database Profiler Output.本节显示对system.profile集合的示例查询。有关查询输出的说明,请参阅数据库档案器输出

To return the most recent 10 log entries in the system.profile collection, run a query similar to the following:要返回system.profile集合中最近的10个日志条目,请运行类似于以下内容的查询:

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

To return all operations except command operations ($cmd), run a query similar to the following:要返回除命令操作($cmd)之外的所有操作,请运行类似于以下内容的查询:

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

To return operations for a particular collection, run a query similar to the following. 若要返回特定集合的操作,请运行类似于以下内容的查询。This example returns operations in the mydb database's test collection:此示例返回mydb数据库的test集合中的操作:

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

To return operations slower than 5 milliseconds, run a query similar to the following:要返回低于5毫秒的操作,请运行类似于以下内容的查询:

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

To return information from a certain time range, run a query similar to the following:要返回特定时间范围内的信息,请运行类似于以下内容的查询:

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

The following example looks at the time range, suppresses the user field from the output to make it easier to read, and sorts the results by how long each operation took to run:以下示例查看时间范围,从输出中取消user字段以便于阅读,并根据每个操作运行的时间对结果进行排序:

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

Show the Five Most Recent Events显示最近发生的五件事

On a database that has profiling enabled, the show profile helper in mongosh displays the 5 most recent operations that took at least 1 millisecond to execute. 在启用了概要分析的数据库上,mongosh中的show profile helper显示执行至少需要1毫秒的5个最新操作。Issue show profile from mongosh, as follows:mongosh发出show profile,如下所示:

show profile

Profiler Overhead探查器开销

When enabled, profiling has an effect on database performance, especially when configured with a profiling level of 2, or when using a low threshold value with a profiling level of 1. 启用后,分析会对数据库性能产生影响,尤其是在配置为2的分析级别时,或在使用分析级别为1的低阈值时。Profiling also consumes disk space, as it logs to both the system.profile collection and also the MongoDB logfile. 评测还会消耗磁盘空间,因为它同时记录到system.profile集合和MongoDBlogfileCarefully consider any performance and security implications before configuring and enabling the profiler on a production deployment.在生产部署上配置和启用探查器之前,请仔细考虑任何性能和安全影响。

The system.profile Collectionsystem.profile集合

The system.profile collection is a capped collection with a default size of 1 megabyte. system.profilee集合是一个封顶集合,默认大小为1兆字节。A collection of this size can typically store several thousand profile documents, but some applications may use more or less profiling data per operation. 这种大小的集合通常可以存储数千个配置文件文档,但有些应用程序可能会在每次操作中使用或多或少的配置文件数据。If you need to change the size of the system.profile collection, follow the steps below.如果需要更改system.profile集合的大小,请按照以下步骤操作。

Change Size of system.profile Collection on the Primary更改Primary上system.profile集合的大小

To change the size of the system.profile collection on the primary, you must:要更改primarysystem.profile集合的大小,必须:

  1. Disable profiling.禁用分析。
  2. Drop the system.profile collection.删除system.profile集合。
  3. Create a new system.profile collection.创建新的system.profile集合。
  4. Re-enable profiling.重新启用分析。

For example, to create a new system.profile collection that is 4000000 bytes (4 MB), use the following sequence of operations in mongosh:例如,要创建一个4000000字节(4 MB)的新system.profile集合,请在mongosh中使用以下操作序列:

db.setProfilingLevel(0)

db.system.profile.drop()

db.createCollection( "system.profile", { capped: true, size:4000000 } )

db.setProfilingLevel(1)

Change Size of system.profile Collection on a Secondary更改Secondary上system.profile集合的大小

To change the size of the system.profile collection on a secondary, you must stop the secondary, run it as a standalone, and then perform the steps above. 要更改secondary上的system.profile集合的大小,必须停止辅助系统,将其作为独立系统运行,然后执行上述步骤。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, secondary members of a replica set now log oplog entries that take longer than the slow operation threshold to apply. 从4.2版开始,副本集的辅助成员现在会记录应用时间超过慢速操作阈值的oplog条目These slow oplog messages: 这些慢速操作日志消息:
  • Are logged for the secondaries in the diagnostic log.诊断日志中为辅助设备记录。
  • Are logged under the REPL component with the text applied op: <oplog entry> took <num>ms.REPL组件下记录,并应用文本applied 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: 可能会受到slowOpSampleRate的影响,具体取决于您的MongoDB版本:
    • In MongoDB 4.2, these slow oplog entries are not affected by the slowOpSampleRate. 在MongoDB 4.2中,这些慢速操作日志条目不受slowOpSampleRate的影响。MongoDB logs all slow oplog entries regardless of the sample rate.MongoDB记录所有慢速操作日志条目,而不管采样率如何。
    • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢速操作日志条目受到slowOpSampleRate的影响。
The profiler does not capture slow oplog entries.探查器未捕获慢速操作日志项。