mongodb聚合框架的索引优化 [英] Index optimization for mongodb aggregation framework

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

问题描述

我在mongo 2.4.4中有一个 match-unwind-group-sort 聚合管道,我需要加快聚合。

I have a match-unwind-group-sort aggregation pipeline in mongo 2.4.4 and I need to speed up the aggregation.

匹配操作包含16个字段的范围查询。我已经使用 .explain()方法来优化范围查询(即创建复合索引)。是否有类似的功能来优化聚合?我正在寻找类似的东西:

The match operation consists of range queries on 16 fields. I've used the .explain() method to optimize range queries (i.e. create compound indexes). Is there a similar function for optimizing the aggregation? I'm looking for something like:

db.col.aggregate([]).explain()

另外,我是否专注于索引优化?

Also, am I right to focus on index optimization?

推荐答案

对于第一个问题,是的,您可以解释聚合。

For the first question, yes, you can explain aggregates.

db.collection.runCommand("aggregate", {pipeline: YOUR_PIPELINE, explain: true})

对于第二个,您创建的用于优化范围查询的索引也将应用于聚合管道的 $ match 阶段(如果它们出现在管道的开头)。因此,您应该专注于索引优化。

For the second one, the indexes you create to optimize the range queries will also apply to the $match stage of the aggregation pipeline, if they occur at the beginning of the pipeline. So you are right to focus on index optimizations.

参见管道运营商和索引

更新2

更多关于汇总解释:在版本2.4上它是不可靠的;在2.6+上它不提供查询执行数据。 https://groups.google.com/forum/#!topic/mongodb -user / 2LzAkyaNqe0

More about aggregate and explain: on version 2.4 it is unreliable; on 2.6+ it does not provide query execution data. https://groups.google.com/forum/#!topic/mongodb-user/2LzAkyaNqe0

更新1

成绩单关于MongoDB 2.4.5的聚合解释。

Transcript of an aggregation explain on MongoDB 2.4.5.

$ mongo so
MongoDB shell version: 2.4.5
connecting to: so
> db.q19329239.runCommand("aggregate", {pipeline: [{$group: {_id: '$user.id', hits: {$sum: 1}}}, {$match: {hits: {$gt: 10}}}], explain: true})
{
    "serverPipeline" : [
        {
            "query" : {

            },
            "projection" : {
                "user.id" : 1,
                "_id" : 0
            },
            "cursor" : {
                "cursor" : "BasicCursor",
                "isMultiKey" : false,
                "n" : 1031,
                "nscannedObjects" : 1031,
                "nscanned" : 1031,
                "nscannedObjectsAllPlans" : 1031,
                "nscannedAllPlans" : 1031,
                "scanAndOrder" : false,
                "indexOnly" : false,
                "nYields" : 0,
                "nChunkSkips" : 0,
                "millis" : 0,
                "indexBounds" : {

                },
                "allPlans" : [
                    {
                        "cursor" : "BasicCursor",
                        "n" : 1031,
                        "nscannedObjects" : 1031,
                        "nscanned" : 1031,
                        "indexBounds" : {

                        }
                    }
                ],
                "server" : "ficrm-rafa.local:27017"
            }
        },
        {
            "$group" : {
                "_id" : "$user.id",
                "hits" : {
                    "$sum" : {
                        "$const" : 1
                    }
                }
            }
        },
        {
            "$match" : {
                "hits" : {
                    "$gt" : 10
                }
            }
        }
    ],
    "ok" : 1
}

服务器版本。

$ mongo so
MongoDB shell version: 2.4.5
connecting to: so
> db.version()
2.4.5

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

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