Evaluate Performance of Current Operations评估当前操作的性能
On this page本页内容
The following sections describe techniques for evaluating operational performance.以下各节介绍了评估操作性能的技术。
Use the Database Profiler to Evaluate Operations Against the Database使用数据库探查器评估针对数据库的操作
MongoDB provides a database profiler that shows performance characteristics of each operation against the database. MongoDB提供了一个数据库探查器,用于显示针对数据库的每个操作的性能特征。Use the profiler to locate any queries or write operations that are running slow. You can use this information, for example, to determine what indexes to create.使用探查器查找运行缓慢的任何查询或写入操作。例如,您可以使用这些信息来确定要创建的索引。
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
.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.探查器未捕获慢速操作日志项。
For more information, see Database Profiler.有关详细信息,请参阅数据库档案器。
Use db.currentOp()
to Evaluate mongod
Operations使用db.currentOp()
评估mongod
操作
db.currentOp()
to Evaluate mongod
OperationsThe db.currentOp()
method reports on current operations running on a mongod
instance.db.currentOp()
方法报告mongod
实例上运行的当前操作。
Use explain
to Evaluate Query Performance使用explain
评估查询性能
explain
to Evaluate Query PerformanceThe cursor.explain()
and db.collection.explain()
methods return information on a query execution, such as the index MongoDB selected to fulfill the query and execution statistics. cursor.explain()
和db.collection.explain()
方法返回查询执行的信息,例如为实现查询和执行统计信息而选择的索引MongoDB。You can run the methods in queryPlanner mode, executionStats mode, or allPlansExecution mode to control the amount of information returned.您可以在queryPlanner
模式、executionStats
模式或allPlansExecution
模式下运行这些方法来控制返回的信息量。
To use 要在查询与表达式cursor.explain()
on a query for documents matching the expression { a: 1 }
, in the collection named records
, use an operation that resembles the following in mongosh
:{ a: 1 }
匹配的文档时使用cursor.explain()
,请在名为records
的集合中使用类似于mongosh
中的以下操作:
db.records.find( { a: 1 } ).explain("executionStats")
Starting in MongoDB 4.2, the explain output includes:从MongoDB 4.2开始,解释输出包括:
queryHash
to help identify slow queries with the same query shape.以帮助识别具有相同查询形状的慢速查询。planCacheKey
to provide more insight into the query plan cache for slow queries.以便为慢速查询提供对查询计划缓存的更多了解。
For more information, see Explain Results, 有关详细信息,请参阅解释结果、cursor.explain()
, db.collection.explain()
, and Analyze Query Performance.cursor.explain()
、db.collection.explain()
和分析查询性能。