Database Manual / Reference / Database Commands / Diagnostics

profile (database command数据库命令)

Definition定义

profile

Changed in version 5.0.在版本5.0中的更改。

For a mongod instance, the command enables, disables, or configures the database profiler. 对于mongod实例,该命令启用、禁用或配置数据库分析器The profiler captures and records data on the performance of write operations, cursors, and database commands on a running mongod instance. If the profiler is disabled, the command configures how slow operations are logged to the diagnostic log.分析器捕获并记录正在运行的mongod实例上写操作、游标和数据库命令的性能数据。如果禁用分析器,该命令将配置如何将慢速操作记录到诊断日志中。

On mongod, if the database profiler level is 2, full logging is enabled on the profiler and the diagnostic log.mongod上,如果数据库分析器级别2,则会在分析器和诊断日志上启用完整日志记录。

At database profiler level 1, the following settings modify both the profiler and the diagnostic log:数据库分析器级别1,以下设置会修改分析器和诊断日志

If the database profiler level is 0, the database profiler is disabled. 如果数据库分析器级别0,则禁用数据库分析器At level 0 the following settings only modify the diagnostic log:在级别0,以下设置仅修改诊断日志:

For a mongos instance, the command only configures how operations get written to the diagnostic log. 对于mongos实例,该命令仅配置如何将操作写入诊断日志。You cannot enable the database profiler on a mongos instance because mongos does not have any collections that the profiler can write to.您无法在mongos实例上启用数据库分析器,因为mongos没有分析器可以写入的任何集合。

Starting in MongoDB 5.0, 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开始,使用profile命令或db.setProfilingLevel()包装器方法对数据库分析器levelslowmssampleRatefilter所做的更改将记录在日志文件中。

On mongos, you can set profile level to:mongos上,您可以将profile级别设置为:

  • 0 to set the slowms, sampleRate, and filter for the diagnostic log;为诊断日志设置slowmssampleRatefilter
  • -1 to read the current settings.读取当前设置。

The profiler is off by default.默认情况下,分析器处于关闭状态。

Warning

Profiling can degrade performance and expose unencrypted query data in the system log. Carefully consider any performance and security implications before configuring and enabling the profiler on a production deployment.分析可能会降低性能,并在系统日志中暴露未加密的查询数据。在生产部署上配置和启用分析器之前,请仔细考虑任何性能和安全影响。

See Profiler Overhead for more information on potential performance degradation.有关潜在性能下降的更多信息,请参阅Profiler开销

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
profile: <level>,
slowms: <threshold>,
sampleRate: <rate>,
filter: <filter expression>
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
profileint

Configures the profiler level. The following profiler levels are available:配置分析器级别。以下分析器级别可用:

0
The profiler is off and does not collect any data. This is the default profiler level.分析器已关闭,不集合任何数据。这是默认的分析器级别。
1

The profiler collects data for operations that exceed the slowms threshold or match a specified 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.分析器仅捕获与筛选器匹配的操作。
2
The profiler collects data for all operations.分析器集合所有操作的数据。

Since profiling is not available on mongos, the profile command cannot be used to set the profiling level to a value other than 0 on a mongos instance.由于mongos上没有分析功能,因此不能使用profile命令将mongos实例上的分析级别设置为0以外的值。

slowmsint

Optional. 可选。Default:默认值: 100

The slow operation time threshold, in milliseconds. Operations that run for longer than this threshold are considered slow.慢速操作时间阈值,单位为毫秒。运行时间超过此阈值的操作被认为很慢。

Slow operations are logged based on workingMillis, which is the amount of time that MongoDB spends working on that operation. This means that factors such as waiting for locks and flow control do not affect whether an operation exceeds the slow operation threshold.慢速操作是基于workingMillis记录的,这是MongoDB在该操作上花费的时间。这意味着等待锁和流量控制等因素不会影响操作是否超过慢速操作阈值。

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设置下,所有操作都会出现在诊断日志中,而不管其延迟如何,但以下情况除外:次级服务器记录慢速oplog条目消息。The secondaries log only the slow oplog entries; increasing the logLevel does not log all oplog entries.次级日志只记录慢速oplog条目;增加logLevel不会记录所有oplog条目。

This argument affects the same setting as the configuration option operationProfiling.slowOpThresholdMs.此参数影响与配置选项operationProfiling.slowOpThresholdMs相同的设置。

sampleRatedouble双精度浮点数

Optional. 可选。Default: 默认值:1.0

The fraction of slow operations that should be profiled or logged. sampleRate accepts values between 0 and 1, inclusive.应该分析或记录的慢速操作的比例。sampleRate接受介于0和1之间的值。

This argument affects the same setting as the configuration option operationProfiling.slowOpSampleRate and does not affect the slow oplog entry log messages on secondaries.此参数影响与配置选项operationProfiling.slowOpSampleRate相同的设置,并且不影响次级服务器上的slow oplog条目日志消息。

filterobject

Optional. 可选。A query that determines which operations are profiled or logged.确定要分析或记录哪些操作的查询。

The filter query takes the following form:筛选器查询采用以下形式:

{ <field1>: <expression1>, ... }

The query can be any legal find() operation where the query <field> matches a field in the profiler output.查询可以是任何合法的find()操作,其中查询<field>分析器输出中的字段匹配。

This argument affects the same setting as the configuration option operationProfiling.filter. When filter is set, the slowms and sampleRate options are not used for profiling and slow-query log lines.此参数影响与配置选项operationProfiling.filter相同的设置。设置filter后,slowmssampleRate选项不用于分析和慢速查询日志行。

The db.getProfilingStatus() and db.setProfilingLevel() shell methods provide wrappers around the profile command.db.getProfilingStatus()db.setProfilingLevel()shell方法profile命令提供包装。

Behavior行为

The profile command obtains a write lock on the affected database while enabling or disabling the profiler. This is typically a short operation. The lock blocks other operations until the profile command has completed.profile命令在启用或禁用分析器时获得受影响数据库的写锁。这通常是一个简短的操作。锁会阻止其他操作,直到profile命令完成。

When connected to a sharded cluster through mongos, you can run the profile command against any database.当通过mongos连接到分片集群时,您可以对任何数据库运行profile命令。

Example示例

Enable Filtering启用筛选

To enable profiling and filter the logged data:要启用分析并筛选记录的数据,请执行以下操作:

db.runCommand(
{
profile: 1,
filter:
{
$or:
[
{ millis: { $gte: 100 } },
{ user: "testuser@admin" }
]
}
}
)

The filter only selects operations that are:筛选器仅选择以下操作:

  • at least 100 milliseconds long, or至少100毫秒长,或
  • submitted by the testuser.testuser提交。

Unset a Filter取消设置筛选器

To clear a profile filter, run profile with the filter: "unset" option.要清除配置文件筛选器,请使用filter: "unset"选项运行profile

db.runCommand(
{
profile: 1,
filter: "unset"
}
)

The operation returns a document with the previous values for the settings.该操作将返回一个包含先前设置值的文档。

To view the current profiling level, see db.getProfilingStatus().要查看当前的分析级别,请参阅db.getProfilingStatus()