Database Manual / CRUD Operations / CRUD Concepts / Query Optimization

Analyze Query Performance分析查询性能

MongoDB provides several ways to examine the performance of your workload, allowing you to understand query performance and identify long-running queries. Understanding query performance helps you build effective indexes and ensure your application runs critical queries efficiently.MongoDB提供了几种检查工作负载性能的方法,使您能够了解查询性能并识别长时间运行的查询。了解查询性能有助于构建有效的索引,并确保应用程序高效运行关键查询。

Identify Slow Queries识别慢速查询

Use the following methods to identify slow queries that occur on your deployment.使用以下方法识别部署中出现的慢速查询。

Performance Overview性能概述

The following methods provide overviews of your deployment's performance. Use these methods to determine if there are performance issues that need to be addressed:以下方法概述了部署的性能。使用这些方法确定是否存在需要解决的性能问题:

Method方法Availability可用性Description描述
Use the Atlas Performance Advisor使用Atlas性能顾问M10+ Atlas clusters集群The Atlas Performance Advisor monitors slow queries and suggests new indexes to improve performance. Atlas性能顾问监控慢速查询,并建议新的索引以提高性能。For more information, see Monitor and Improve Slow Queries with the Performance Advisor.有关更多信息,请参阅使用Performance Advisor监视和改进慢速查询
Check ongoing operations in Atlas检查Atlas中正在进行的操作M10+ Atlas clusters集群You can use the Atlas Real-Time Performance Panel (RTPP) to see current network traffic, database operations, and hardware statistics.您可以使用Atlas实时性能面板(RTPP)查看当前的网络流量、数据库操作和硬件统计数据。
Check ongoing operations locally检查本地正在进行的操作Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署

The $currentOp aggregation stage returns information on active operations and cursors. $currentOp聚合阶段返回有关活动操作和游标的信息。Use $currentOp to identify long-running or stuck operations that may be negatively impacting performance.使用$currentOp来识别可能对性能产生负面影响的长时间运行或卡住的操作。

You can also use the top command to get additional operation count and latency statistics.您还可以使用top命令获取其他操作计数和延迟统计信息。

Check server metrics检查服务器指标Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署

For Atlas clusters, you can view cluster metrics to identify performance issues.对于Atlas集群,您可以查看集群指标以识别性能问题。

For self-hosted deployments, the serverStatus command provides metrics that can indicate poor performance and anomalies for query execution.对于自托管部署,serverStatus命令提供了可以指示查询执行性能不佳和异常的指标。

View common query shapes查看常见查询形状Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署The $queryStats aggregation stage returns information about common query shapes. $queryStats provides a holistic view of the kinds of queries being run on your deployment.$queryStats聚合阶段返回有关常见查询形状的信息。$queryStats提供了在部署上运行的查询类型的整体视图。
View index statistics查看索引统计信息Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署The $indexStats aggregation stage returns information about your collection's indexes and how often individual indexes are used. Use $indexStats to identify unused indexes that can be removed to improve write performance.$indexStats聚合阶段返回有关集合索引的信息以及单个索引的使用频率。使用$indexStats来识别可以删除以提高写入性能的未使用索引。
View collection statistics查看集合统计信息Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署

The $collStats aggregation stage returns statistical information on specific collections and their usage. $collStats provides information such as:$collStats聚合阶段返回有关特定集合及其使用情况的统计信息。$collStats提供以下信息:

  • Storage usage for the collection and its indexes集合及其索引的存储使用情况
  • The average size of documents文档的平均大小

Analyze a Slow Query分析慢速查询

Use these methods to analyze a slow query and determine the cause of poor performance:使用以下方法分析慢速查询并确定性能不佳的原因:

Method方法Availability可用性Description描述
Use the Atlas Query Profiler使用Atlas查询分析器M10+ Atlas clusters集群The Atlas Query Profiler shows long-running operations and performance statistics. Atlas查询分析器显示长时间运行的操作和性能统计数据。For more information, see Monitor Query Performance with the Query Profiler.有关更多信息,请参阅使用Query Profiler监视查询性能
Enable the Database Profiler启用数据库探查器Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署

When enabled, the database profiler stores information about slow queries in the system.profile collection.启用后,数据库分析器将有关慢速查询的信息存储在system.profile集合中。

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

View slow queries in the diagnostic log查看诊断日志中的慢速查询Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署

MongoDB logs queries that exceed the slow operation threshold (default 100 milliseconds) in the diagnostic logs.MongoDB在诊断日志中记录超过慢速操作阈值(默认100毫秒)的查询。

Check the diagnostic logs to identify problematic queries and see which queries would benefit from indexes.检查诊断日志以识别有问题的查询,并查看哪些查询将从索引中受益。

View explain results查看解释结果Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署

Query explain results show information on the query plan and execution statistics. You can use explain results to determine the following information about a query:查询解释结果显示查询计划和执行统计信息。您可以使用解释结果来确定有关查询的以下信息:

  • The amount of time a query took to execute执行查询所花费的时间
  • Whether the query used an index查询是否使用了索引
  • The number of documents and index keys scanned to fulfill a query为完成查询而扫描的文档和索引键的数量

To view explain results, use the following methods:要查看解释结果,请使用以下方法:

To learn about explain results output, see Explain Results and Interpret Explain Plan Results.要了解解释结果输出,请参阅解释结果解释解释计划结果

Perform Advanced Query Analysis执行高级查询分析

The following methods are suited for deeper investigation of problematic queries, and can provide fine-grained performance insights:以下方法适用于对有问题的查询进行更深入的调查,并可以提供细粒度的性能见解:

Method方法Availability可用性Description描述
View plan cache statistics查看计划缓存统计信息Atlas clusters and self-hosted deploymentsAtlas集群和自托管部署

The $planCacheStats aggregation stage returns information about a collection's plan cache.$planCacheStats聚合阶段返回有关集合计划缓存的信息。

The plan cache contains query plans that the query planner uses to efficiently complete queries. Generally, the plan cache should contain entries for your most commonly-run queries.计划缓存包含查询计划器用于高效完成查询的查询计划。通常,计划缓存应包含最常运行的查询的条目。