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:有关其他相关信息,请参阅:
The following profiling levels are available:以下分析级别可用:
Level | |
---|---|
0 | |
1 | slowms .slowms 值花费更长时间的操作集合数据。
|
2 |
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 MongoDB在为数据库启用配置文件后,在数据库中创建system.profile
collection in a database after you enable profiling for that database.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 从MongoDB 5.0开始(也可从4.4.2、4.2.12和4.0.22开始),使用level
, slowms
, sampleRate
, or filter
using the profile
command or db.setProfilingLevel()
wrapper method are recorded in the log file
.profile
命令或db.setProfilingLevel()
包装方法对数据库探查器level
、slowms
、sampleRate
或filter
所做的更改记录在日志文件中。
The slowms and sampleRate profiling settings are global. slowms和sampleRate文件设置是全局的。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
设置会影响整个过程。
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:要更改慢速操作阈值,请通过以下方式之一指定所需阈值:
slowms
using the profile
command or db.setProfilingLevel()
shell helper method.profile
命令或db.setProfilingLevel()
shell助手方法设置slowms
的值。--slowms
from the command line at startup.--slowms
的值。slowOpThresholdMs
in a configuration file.slowOpThresholdMs
的值。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
将分析比阈值慢的操作。
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 从MongoDB 4.0开始,您可以使用db.setProfilingLevel()
to configure slowms
and sampleRate
for mongos
. db.setProfilingLevel()
为mongos
配置slowms
和sampleRate
。For the 对于mongos
, the slowms
and sampleRate
configuration settings only affect the diagnostic log and not the profiler since profiling is not available on mongos
. mongos
,slowms
和sampleRate
配置设置仅影响诊断日志,而不影响探查器,因为在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
日志消息)包括:
queryHash
planCacheKey
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消息:
diagnostic log
.diagnostic log
中记录辅助设备。REPL
component with the text applied op: <oplog entry> took <num>ms
.REPL
组件下,文本应用为applied op: <oplog entry> took <num>ms
。May be affected by 可能受slowOpSampleRate
, depending on your MongoDB version:slowOpSampleRate
影响,具体取决于您的MongoDB版本:
slowOpSampleRate
. slowOpSampleRate
的影响。slowOpSampleRate
.slowOpSampleRate
的影响。The profiler does not capture slow oplog entries.探查器不捕获慢速oplog条目。
To profile only a randomly sampled subset of all slow operations , specify the desired sample rate in one of the following ways:要仅分析所有慢速操作的随机采样子集,请通过以下方式之一指定所需采样率:
[2]
sampleRate
using the profile
command or db.setProfilingLevel()
shell helper method.profile
命令或db.setProfilingLevel()
shell助手方法设置sampleRate
的值。--slowOpSampleRate
for mongod
or --slowOpSampleRate
for mongos
from the command line at startup.mongod
设置--slowOpSampleRate
或为mongos
设置--slowOpSampleRate
的值。slowOpSampleRate
in a configuration file.slowOpSampleRate
的值。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 从MongoDB 4.0开始,您可以使用db.setProfilingLevel()
to configure slowms
and sampleRate
for mongos
. db.setProfilingLevel()
为mongos
配置slowms
和sampleRate
。For the 对于mongos
, the slowms
and sampleRate
configuration settings only affect the diagnostic log and not the profiler since profiling is not available on mongos
. mongos
,slowms
和sampleRate
配置设置仅影响诊断日志,而不影响探查器,因为在mongos
上无法进行评测。[1]
For example, the following sets a 例如,以下设置mongos
instance's sampling rate for logging slow operations:mongos
实例的采样率,用于记录慢速操作:
db.setProfilingLevel(0, { sampleRate: 0.42 })
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 二级仅记录慢速oplog条目;增加logLevel
does not log all oplog entries.logLevel
不会记录所有oplog条目。
[1] | (1, 2) |
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:您可以通过以下方式之一设置分析筛选器:
filter
using the profile
command or db.setProfilingLevel()
shell helper method.profile
命令或db.setProfilingLevel()
shell助手方法设置filter
的值。filter
in a configuration file.filter
的值。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
上无法进行评测。
When a profiling 设置分析filter
is set, the slowms and sampleRate options do not affect the diagnostic log or the profiler.filter
时,slowms
和sampleRate
选项不会影响诊断日志或分析程序。
For example, the following 例如,以下db.setProfilingLevel()
method sets for a mongod
instance:db.setProfilingLevel()
方法为mongod
实例设置:
2
,2
,{ 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 } } } )
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
字段指示应分析的慢速操作的百分比。
To disable profiling, use the following helper in 要禁用分析,请在mongosh
:mongosh
中使用以下助手:
db.setProfilingLevel(0)
mongod
Instancemongod
实例启用分析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
时,slowms
和slowOpSampleRate
也会影响将哪些操作记录到诊断日志中。The slowms
and slowOpSampleRate
are also available to configure diagnostic logging for mongos
. slowms
和slowOpSampleRat
e也可用于配置mongos
的诊断日志记录。[2]
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 但是,从MongoDB 4.0开始,您可以在--slowms
and slowOpSampleRate
on mongos
to configure the diagnostic log for slow operations.mongos
上设置--slowms
和slowOpSampleRate
,为慢速操作配置诊断日志。
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 从MongoDB 4.4开始,不再可能在事务中对system.profile
collection from within a transaction.system.profile
集合执行任何操作,包括读取。
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 } )
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:mongosh
的show profile
,如下所示:
show profile
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.在生产部署中配置和启用探查器之前,请仔细考虑任何性能和安全影响。
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
集合的大小,请执行以下步骤。
system.profile
Collection on the Primarysystem.profile
集合的大小To change the size of the 要更改primary上system.profile
collection on the primary, you must:system.profile
集合的大小,必须:
system.profile
collection.system.profile
集合。system.profile
collection.system.profile
集合。For example, to create a new 例如,要创建一个4000000字节(4 MB)的新system.profile
collection that is 4000000
bytes (4 MB), use the following sequence of operations in mongosh
:system.profile
集合,请在mongosh
中使用以下操作序列:
db.setProfilingLevel(0) db.system.profile.drop() db.createCollection( "system.profile", { capped: true, size:4000000 } ) db.setProfilingLevel(1)
system.profile
Collection on a Secondarysystem.profile
集合的大小To change the size of the 要更改secondary上system.profile
collection on a secondary, you must stop the secondary, run it as a standalone, and then perform the steps above. system.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)
REPL component with the text applied op: <oplog entry> took <num>ms .slowOpSampleRate , depending on your MongoDB version:slowOpSampleRate 影响,具体取决于您的MongoDB版本:
|