我在mongo 2.4.4中有一个match-unwind-group-sort
聚合管道,我需要加快聚合速度.
匹配操作包括16个字段的范围查询.我使用了.explain()
方法来优化范围查询(i.e. create compound indexes).是否有类似的函数用于优化聚合?我在找这样的东西:
db.col.aggregate([]).explain()
另外,我关注索引优化对吗?
我在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
关于aggregate和explain的更多信息:在版本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