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日志消息)包括:

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.诊断日志中记录辅助设备的。
  • 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条目。

For more information, see Database Profiler.有关更多信息,请参阅数据库探查器。

Use db.currentOp() to Evaluate mongod Operations使用db.currentOp()计算mongod操作

The db.currentOp() method reports on current operations running on a mongod instance.db.currentOp()方法报告在mongod实例上运行的当前操作。

Use explain to Evaluate Query Performance使用explain来评估查询性能

The 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模式下运行这些方法,以控制返回的信息量。

Example示例

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开始,解释输出包括:

For more information, see Explain Results, cursor.explain(), db.collection.explain(), and Analyze Query Performance.有关更多信息,请参阅解释结果cursor.explain()db.collection.explain()分析查询性能

←  Query OptimizationOptimize Query Performance →