首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb聚合框架的索引优化

mongodb聚合框架的索引优化
EN

Stack Overflow用户
提问于 2013-10-25 13:35:56
回答 1查看 14.6K关注 0票数 22

我在mongo2.4.4中有一个match-unwind-group-sort聚合管道,我需要加快聚合速度。

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

代码语言:javascript
复制
db.col.aggregate([]).explain()

此外,我是否应该专注于索引优化?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-25 13:52:42

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

代码语言:javascript
复制
db.collection.runCommand("aggregate", {pipeline: YOUR_PIPELINE, explain: true})

对于第二个索引,您为优化范围查询而创建的索引也将应用于聚合管道的$match阶段,如果这些索引发生在管道的开头。因此,将重点放在索引优化上是正确的。

管道操作符和索引

更新2

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

更新1

聚合解释在MongoDB 2.4.5上的转录本。

代码语言:javascript
复制
$ 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
}

服务器版本。

代码语言:javascript
复制
$ mongo so
MongoDB shell version: 2.4.5
connecting to: so
> db.version()
2.4.5
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19591405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档