Mongodb Explain for Aggregation 框架 [英] Mongodb Explain for Aggregation framework

查看:28
本文介绍了Mongodb Explain for Aggregation 框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MongoDB 中的聚合框架有解释函数吗?我在文档中看不到它.

Is there an explain function for the Aggregation framework in MongoDB? I can't see it in the documentation.

如果没有,是否还有其他方法可以检查查询在聚合框架内的执行情况?

If not is there some other way to check, how a query performs within the aggregation framework?

我知道找到你只是做

db.collection.find().explain()

但是使用聚合框架时出现错误

But with the aggregation framework I get an error

db.collection.aggregate(
    { $project : { "Tags._id" : 1 }},
    { $unwind : "$Tags" },
    { $match: {$or: [{"Tags._id":"tag1"},{"Tags._id":"tag2"}]}},
    { 
        $group: 
        { 
            _id : { id: "$_id"},
            "count": { $sum:1 } 
        }
    },
    { $sort: {"count":-1}}
).explain()

推荐答案

从 MongoDB 3.0 开始,只需更改顺序

Starting with MongoDB version 3.0, simply changing the order from

collection.aggregate(...).explain()

collection.explain().aggregate(...)

会给你想要的结果(文档这里).

will give you the desired results (documentation here).

对于 >= 2.6 的旧版本,您需要使用 explain 聚合管道操作的选项

For older versions >= 2.6, you will need to use the explain option for aggregation pipeline operations

db.collection.aggregate([
    { $project : { "Tags._id" : 1 }},
    { $unwind : "$Tags" },
    { $match: {$or: [{"Tags._id":"tag1"},{"Tags._id":"tag2"}]}},
    { $group: { 
        _id : "$_id",
        count: { $sum:1 } 
    }},
    {$sort: {"count":-1}}
  ],
  {
    explain:true
  }
)

聚合框架的一个重要考虑因素是索引只能用于获取管道的初始数据(例如,$match$sort$geonear 在管道的开头)以及后续的 $lookup$graphLookup 阶段.一旦数据被提取到聚合管道中进行处理(例如通过像 $project$unwind$group 这样的阶段)进一步操作将在内存中(如果设置了 allowDiskUse 选项,则可能使用临时文件).

An important consideration with the Aggregation Framework is that an index can only be used to fetch the initial data for a pipeline (e.g. usage of $match, $sort, $geonear at the beginning of a pipeline) as well as subsequent $lookup and $graphLookup stages. Once data has been fetched into the aggregation pipeline for processing (e.g. passing through stages like $project, $unwind, and $group) further manipulation will be in-memory (possibly using temporary files if the allowDiskUse option is set).

通常,您可以通过以下方式优化聚合管道:

In general, you can optimize aggregation pipelines by:

  • 使用 $match 阶段启动管道以限制对相关文档的处理.
  • 确保初始 $match/$sort 阶段由 高效索引.
  • 使用 $match$limit$skip 及早过滤数据.
  • 尽量减少不必要的阶段和文档操作(如果需要复杂的聚合体操,可能需要重新考虑您的架构).
  • 如果您升级了 MongoDB 服务器,则可以利用较新的聚合运算符.例如,MongoDB 3.4 添加了许多新的聚合阶段和表达式,包括支持用于处理数组、字符串和构面.
  • Starting a pipeline with a $match stage to restrict processing to relevant documents.
  • Ensuring the initial $match / $sort stages are supported by an efficient index.
  • Filtering data early using $match, $limit , and $skip .
  • Minimizing unnecessary stages and document manipulation (perhaps reconsidering your schema if complicated aggregation gymnastics are required).
  • Taking advantage of newer aggregation operators if you have upgraded your MongoDB server. For example, MongoDB 3.4 added many new aggregation stages and expressions including support for working with arrays, strings, and facets.

还有许多聚合管道优化可以自动发生取决于您的 MongoDB 服务器版本.例如,相邻的阶段可以合并和/或重新排序,以在不影响输出结果的情况下改进执行.

There are also a number of Aggregation Pipeline Optimizations that automatically happen depending on your MongoDB server version. For example, adjacent stages may be coalesced and/or reordered to improve execution without affecting the output results.

在 MongoDB 3.4 中,聚合框架 explain 选项提供有关如何处理管道的信息,但不支持与 executionStats 模式相同级别的详细信息find() 查询.如果您专注于优化初始查询执行,您可能会发现使用 executionStatsallPlansExecution 详细程度.

As at MongoDB 3.4, the Aggregation Framework explain option provides information on how a pipeline is processed but does not support the same level of detail as the executionStats mode for a find() query. If you are focused on optimizing initial query execution you will likely find it beneficial to review the equivalent find().explain() query with executionStats or allPlansExecution verbosity.

在 MongoDB 问题跟踪器中有一些相关的功能请求需要关注/投票,以帮助优化/分析聚合管道的更详细的执行统计信息:

There are a few relevant feature requests to watch/upvote in the MongoDB issue tracker regarding more detailed execution stats to help optimize/profile aggregation pipelines:

这篇关于Mongodb Explain for Aggregation 框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆