Docs HomeMongoDB Manual

Evaluate Performance of Current Operations评估当前操作的性能

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, 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 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, these slow oplog entries are not affected by the slowOpSampleRate. MongoDB logs all slow oplog entries regardless of the sample rate.在MongoDB 4.2中,这些慢速操作日志条目不受slowOpSampleRate的影响。MongoDB记录所有慢速操作日志条目,而不管采样率如何。
    • In MongoDB 4.4 and later, these slow oplog entries are affected by the slowOpSampleRate.在MongoDB 4.4及更高版本中,这些慢速操作日志条目受到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操作

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()分析查询性能