Database Profiler数据库分析器

On this page本页内容

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. 默认情况下,探查器处于off状态。You can enable the profiler on a per-database or per-instance basis at one of several profiling levels.您可以在多个分析级别之一的每个数据库或每个实例上启用分析程序。

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:有关其他相关信息,请参阅:

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 longer than the value of slowms.探查器为比slowms值花费更长时间的操作集合数据。
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. mongod实例启用分析时,将分析级别设置为大于0的值。The profiler records data in the system.profile collection. 探查器在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, 4.2.12, and 4.0.22), 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和4.0.22开始),使用profile命令或db.setProfilingLevel()包装方法对数据库探查器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 databaselevel. 当通过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. 默认情况下,慢速操作阈值为100毫秒。To change the slow operation threshold, specify the desired threshold value in one of the following ways:要更改慢速操作阈值,请通过以下方式之一指定所需阈值:

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 (also available starting in 4.0.6), secondary members of a replica set now log oplog entries that take longer than the slow operation threshold to apply. 从版本4.2开始(也可从4.0.6开始使用),副本集的辅助成员现在记录的oplog条目比应用慢速操作阈值所需的时间更长。These slow oplog messages:这些慢速oplog消息:

  • Are logged for the secondaries in the diagnostic log.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 and earlier, these slow oplog entries are not affected by the slowOpSampleRate. 在MongoDB 4.2及更早版本中,这些慢速oplog条目不受slowOpSampleRate的影响。MongoDB logs all slow oplog entries regardless of the sample rate.MongoDB记录所有慢速oplog条目,无论采样率如何。
    • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢速oplog条目受slowOpSampleRate的影响。

The profiler does not capture slow oplog entries.探查器不捕获慢速oplog条目。

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 slowoperations 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 slowoperations 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设置下,所有操作都会出现在诊断日志中,无论其延迟如何,但以下例外情况除外:辅助设备记录慢速oplog条目消息The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.二级仅记录慢速oplog条目;增加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秒的查询操作。
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.或者,您可以在配置文件中指定operationProfiling

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提示

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 Queries示例分析器数据查询

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

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帮助程序显示了5个最新的操作,执行这些操作至少需要1毫秒。Issue show profile from mongosh, as follows:发布mongoshshow 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集合和MongoDB日志文件。Carefully 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.profile集合是一个封顶集合,默认大小为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. 要更改secondarysystem.profile集合的大小,必须停止secondary,将其作为独立运行,然后执行上述步骤。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 now log oplog entries that take longer than the slow operation threshold to apply. 从版本4.2开始(也可从4.0.6开始使用),副本集的辅助成员现在记录的oplog条目比应用慢操作阈值所需的时间更长。These slow oplog messages:
  • Are logged for the secondaries in the diagnostic log.这些慢速oplog消息:记录在诊断日志中的辅助设备。
  • Are logged under the REPL component with the text 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 and earlier, these slow oplog entries are not affected by the slowOpSampleRate. 在MongoDB 4.2及更早版本中,这些慢速oplog条目不受slowOpSampleRate的影响。MongoDB logs all slow oplog entries regardless of the sample rate.MongoDB记录所有慢速oplog条目,无论采样率如何。
    • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢速oplog条目受slowOpSampleRate的影响。
The profiler does not capture slow oplog entries.探查器不捕获慢速oplog条目。
←  Tuning Your Connection Pool SettingsDatabase Profiler Output →