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

匹配操作包括16个字段的范围查询.我使用了.explain()方法来优化范围查询(i.e. create compound indexes).是否有类似的函数用于优化聚合?我在找这样的东西:

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

另外,我关注索引优化对吗?

推荐答案

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

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

对于第二种情况,如果索引出现在聚合管道的开头,则为优化范围查询而创建的索引也将应用于聚合管道的$match阶段.所以你关注索引优化是对的.

Pipeline Operators and Indexes.

Update 2

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

Update 1

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相关问答推荐

$mod只支持数字类型,不支持MongoDb中的array和int

在MongoDB查询中添加来自另一个集合的匹配文档的计数

MongoDB/Mongoose查询:使用优先约束检索从位置A到位置B的路径

聚合管道可以用于更新数据库中的文档吗?

使用 multer 在我的 MERN 前端显示 MongoDB 图像的正确语法是什么?

MongoDB 聚合 - 条件 $lookup 取决于字段是否存在

从具有多个数组匹配 MongoDB 的两个集合中采样数据

mongoDB文档数组字段中的唯一项如何

如何将以下聚合查询转换为 bson 并在 golang 中执行

使用 mongodb 时是否需要规范化数据库?

如何将 MongoDB 集合中的 _id 字段更改为 User_id?

在 Heroku 上使用 Node 读取、写入和存储 JSON

什么是 Mongoose (Nodejs) 复数规则?

在 Mongoose 中填写所有必填字段

从 id 删除 PyMongo 中的文档

Mongoose:查询starts with

MongoDB:聚合框架: $match between fields

Mongoose 为所有嵌套对象添加 _id

聚合 $lookup 匹配管道中文档的总大小超过最大文档大小

MongoDB Compass:select distinct field values