Mongodb解释聚合框架 [英] Mongodb Explain for Aggregation framework

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

问题描述

MongoDB中的Aggregation框架有解释功能吗?我在文档中看不到它.

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?

我知道找到您就是这样做

I know with find you just do

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的旧版本,您将需要使用

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:

  • 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()查询相同级别的详细信息.如果您专注于优化初始查询执行,则可能会发现使用

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:

  • SERVER-19758: Add "executionStats" and "allPlansExecution" explain modes to aggregation explain
  • SERVER-21784: Track execution stats for each aggregation pipeline stage and expose via explain
  • SERVER-22622: Improve $lookup explain to indicate query plan on the "from" collection

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

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