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. 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:本文档概述了数据库探查器的一些关键管理选项。有关其他相关信息,请参阅:
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 如果您尝试这样做,MongoDB 6.3及更高版本会返回一个IllegalOperation
error if you attempt to do so. IllegalOperation
错误。Earlier MongoDB versions crash.早期的MongoDB版本崩溃。
Profiling Levels分析级别
The following profiling levels are available:以下配置文件级别可用:
0 | |
1 | slowms or that match a filter.slowms 值或与筛选器匹配的操作集合数据。 |
2 |
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 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, and 4.2.12), changes made to the database profiler 从MongoDB 5.0开始(也可从4.4.2和4.2.12开始使用),使用level
, slowms
, sampleRate
, or filter
using the profile
command or db.setProfilingLevel()
wrapper method are recorded in the log file
.profile
命令或dbsetProfileLevel()
包装方法对数据库探查器level
、slowms
、sampleRate
或filter
所做的更改将记录在日志文件中。
Global and Per-Database Profiling Settings全局和每个数据库的分析设置
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 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毫秒。要更改慢速操作阈值,请通过以下方式之一指定所需的阈值:
Set the value of使用slowms
using theprofile
command ordb.setProfilingLevel()
shell helper method.profile
命令或db.setProfilingLevel()
shell助手方法设置slowms
的值。Set the value of在启动时从命令行设置--slowms
from the command line at startup.--slowms
的值。Set the value of在配置文件中设置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
to help identify slow queries with the same query shape.以帮助识别具有相同查询形状的慢速查询。planCacheKey
to provide more insight into the query plan cache for slow queries.以便为慢速查询提供对查询计划缓存的更多了解。
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 textapplied 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在MongoDB 4.2中,这些慢速操作日志条目不受slowOpSampleRate
. MongoDB logs all slow oplog entries regardless of the sample rate.slowOpSampleRate
的影响。MongoDB记录所有慢速操作日志条目,而不管采样率如何。In MongoDB 4.4 and later, these slow oplog entries are affected by the在MongoDB 4.4及更高版本中,这些慢速操作日志条目受到slowOpSampleRate
.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]
Set the value of使用sampleRate
using theprofile
command ordb.setProfilingLevel()
shell helper method.profile
命令或db.setProfilingLevel()
shell助手方法设置sampleRate
的值。Set the value of在启动时,从命令行为--slowOpSampleRate
formongod
or--slowOpSampleRate
formongos
from the command line at startup.mongod
设置--slowOpSampleRate
的值,或为mongos
设置--slowOpSampleRate
的值。Set the value of在配置文件中设置slowOpSampleRate
in a configuration file.slowOpSampleRate
的值。
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 从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 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) |
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:您可以通过以下方式之一设置分析筛选器:
Set the value of使用filter
using theprofile
command ordb.setProfilingLevel()
shell helper method.profile
命令或db.setProfilingLevel()
shell助手方法设置filter
的值。Set the value of在配置文件中设置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
时,slowm
s和sampleRate
选项不会影响诊断日志或探查器。
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 logquery
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
实例启用分析
mongod
InstanceFor 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
时,slowms
和slowOpSampleRate
还会影响将哪些操作记录到诊断日志中。The slowms
and slowOpSampleRate
are also available to configure diagnostic logging for mongos
. slowms
和slowOpSampleRate
也可用于配置mongos
的诊断日志记录。[2]
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 但是,从MongoDB 4.0开始,您可以在--slowms
and slowOpSampleRate
on mongos
to configure the diagnostic log for slow operations.mongos
上设置--slowms
和slowOpSampleRate
来配置慢速操作的诊断日志。
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 从MongoDB 4.4开始,不再可能在事务中对system.profile
collection from within a transaction.system.profile
集合执行任何操作,包括读取。
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毫秒的操作,请运行类似于以下内容的查询:5
milliseconds, run a query similar to the following:
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
集合和MongoDBlogfile
。Carefully consider any performance and security implications before configuring and enabling the profiler on a production deployment.在生产部署上配置和启用探查器之前,请仔细考虑任何性能和安全影响。
The system.profile
Collectionsystem.profile
集合
system.profile
CollectionThe system.profile
collection is a capped collection with a default size of 1 megabyte. system.profile
e集合是一个封顶集合,默认大小为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
集合的大小
system.profile
Collection on the PrimaryTo change the size of the 要更改primary上system.profile
collection on the primary, you must:system.profile
集合的大小,必须:
Disable profiling.禁用分析。Drop the删除system.profile
collection.system.profile
集合。Create a new创建新的system.profile
collection.system.profile
集合。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
集合的大小
system.profile
Collection on a SecondaryTo 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
集合的大小,必须停止辅助系统,将其作为独立系统运行,然后执行上述步骤。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)
|