Optimize your MongoDB queries to ensure the best possible performance for your application. This can occur both during development and later, as your data usage and demand changes. As collections grow, a periodic review of query performance can help determine when clusters need to scale up or scale out.优化MongoDB查询,以确保应用程序具有最佳性能。这可能发生在开发期间和以后,因为数据使用和需求会发生变化。随着集合的增长,定期检查查询性能可以帮助确定集群何时需要扩展或向外扩展。
Behavior行为
When you send a query to MongoDB, the server interprets the query, builds a query plan to determine how best to retrieve the data, executes the plan, then returns the results to the client.当您向MongoDB发送查询时,服务器会解释查询,构建查询计划以确定如何最好地检索数据,执行计划,然后将结果返回给客户端。
To improve query performance, consider how MongoDB builds and executes the plan for your specific queries and identify problem areas, such as fields that would benefit from an index, inefficient filters, and issues at scale.为了提高查询性能,请考虑MongoDB如何为特定查询构建和执行计划,并确定问题领域,例如从索引中受益的字段、低效的筛选器和大规模问题。
Indexes索引
Indexes store values from individual fields or sets of fields from a collection in a separate data structure. In read operations, they allow MongoDB to search in the index to identify relevant documents instead of the entire collection. In write operations, MongoDB must both write the change to the collection and update the index.索引将单个字段或集合中的字段集的值存储在单独的数据结构中。在读取操作中,它们允许MongoDB在索引中搜索以识别相关文档,而不是整个集合。在写操作中,MongoDB必须将更改写入集合并更新索引。
In many cases the use of indexes can speed up your query. This improvement depends on which fields are indexed and what your queries use for filters. For example, if a query applies no projections or filters, a collection scan can be more efficient than an index scan.在许多情况下,使用索引可以加快查询速度。这种改进取决于对哪些字段进行索引,以及查询用于筛选器的内容。例如,如果查询不应用投影或筛选器,则集合扫描可能比索引扫描更有效。
Tasks任务
Troubleshooting故障排除
Solutions解决方案
Details详情
Atlas Query ProfilerAtlas查询分析器
The Atlas Query Profiler helps diagnose and monitor slow-running queries using log data from your cluster. It aggregates, filters, and visualizes performance statistics in a scatterplot chart through the Query Insights interface.Atlas查询分析器使用集群中的日志数据帮助诊断和监控运行缓慢的查询。它通过Query Insights界面在散点图中聚合、筛选和可视化性能统计数据。
You may find this visualization more convenient in identifying slow queries and performance outliers than the Database Profiler.您可能会发现,与数据库分析器相比,这种可视化在识别慢速查询和性能异常值方面更方便。
With the Query Profiler, you get a macro view of your application load. This can help you identify which queries are slow and provide some insights into how you can improve their performance, such as:使用Query Profiler,您可以获得应用程序负载的宏视图。这可以帮助您确定哪些查询速度较慢,并提供一些关于如何提高其性能的见解,例如:
How long the query takes to run查询运行需要多长时间The number of documents examined by the query查询检查的文档数量Whether the query uses an index查询是否使用索引
For more information, see Monitor Query Performance with the Query Profiler.有关更多信息,请参阅使用查询分析器监视查询性能。
Database Profiler数据库分析器
The database profiler collects detailed information about the queries that run on MongoDB, which it stores in the capped 数据库分析器集合有关在MongoDB上运行的查询的详细信息,并将其存储在封顶的system.profile collection.system.profile集合中。
Important
The database profiler can affect database performance and disk usage. Consider the performance and storage implications before enabling it on a production deployment.数据库分析器会影响数据库性能和磁盘使用情况。在生产部署上启用它之前,请考虑性能和存储影响。
For more information, see Profiler Overhead.有关更多信息,请参阅分析器开销。
With the database profiler, you get a micro view of slow queries in the current application load. This can provide some insights into how you can improve query performance, such as:使用数据库分析器,您可以获得当前应用程序负载中慢速查询的微观视图。这可以提供一些关于如何提高查询性能的见解,例如:
How long the query takes to run查询运行需要多长时间The number of documents examined by the query查询检查的文档数量Whether the query uses an index查询是否使用索引
For more information, see Database Profiler Output.有关更多信息,请参阅数据库分析器输出。
Note
The database profiler is not available on 数据库分析器在mongos. If you need to find slow queries on a sharded cluster, see Monitor Current Operations.mongos上不可用。如果需要在分片集群上查找慢速查询,请参阅监视当前操作。
Monitor Current Operations监控当前操作
The $currentOp aggregation stage returns information on operations currently running on a MongoDB server or cluster. $currentOp can help you identify performance issues, such as:$currentOp聚合阶段返回当前在MongoDB服务器或集群上运行的操作的信息。$currentOp可以帮助您识别性能问题,例如:
How long the query takes to run查询运行需要多长时间Whether the query is waiting on a lock查询是否正在等待锁Whether the query uses an index查询是否使用索引
For more information, see Monitor Slow Queries.有关更多信息,请参阅监视慢速查询。
Explain解释
The explain() method retrieves query plan information and execution statistics on queries and aggregation pipelines. It is available for use with these query methods:explain()方法检索查询计划信息以及查询和聚合管道的执行统计信息。它可用于以下查询方法:
The explain() method can help you to identify potential issues with specific queries, such as:explain()方法可以帮助您识别特定查询的潜在问题,例如:
Inefficient filters筛选器效率低Ineffective index usage索引使用无效Potential performance issues at scale大规模的潜在性能问题
For more information, see Explain Slow Queries.有关更多信息,请参阅解释慢速查询。
Collection Stats集合统计信息
The $collStats aggregation stage provides statistical information on specific collections and their usage. You may find this useful in identifying index and performance issues.$collStats聚合阶段提供有关特定集合及其使用情况的统计信息。您可能会发现这在识别索引和性能问题方面很有用。
This can provide useful information, such as:这可以提供有用的信息,例如:
Storage usage for the collection and its indexes集合及其索引的存储使用情况The average size of documents文档的平均大小
Index Stats索引统计
The $indexStats aggregation stage provides statistical information on index usage by a collection. This can help you identify specific index usage issues, such as:$indexStats聚合阶段提供有关集合索引使用情况的统计信息。这可以帮助您识别特定的索引使用问题,例如:
Whether queries use the index and how often查询是否使用索引以及使用频率Whether an index is redundant索引是否冗余