Tom*_*fty 19 indexing mongodb aggregation-framework
我match-unwind-group-sort在mongo 2.4.4中有一个聚合管道,我需要加快聚合.
匹配操作包含16个字段的范围查询.我已经使用该.explain()方法来优化范围查询(即创建复合索引).是否有类似的功能来优化聚合?我正在寻找类似的东西:
db.col.aggregate([]).explain()
Run Code Online (Sandbox Code Playgroud)
另外,我是否正确专注于索引优化?
Raf*_*afa 18
对于第一个问题,是的,您可以解释聚合.
db.collection.runCommand("aggregate", {pipeline: YOUR_PIPELINE, explain: true})
Run Code Online (Sandbox Code Playgroud)
对于第二个,您创建的用于优化范围查询的索引也将应用于聚合管道的$ match阶段(如果它们出现在管道的开头).因此,您应该专注于索引优化.
请参阅管道运算符和索引.
更新2
更多关于聚合和解释:在版本2.4上它是不可靠的; 在2.6+上它不提供查询执行数据.https://groups.google.com/forum/#!topic/mongodb-user/2LzAkyaNqe0
更新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
}
Run Code Online (Sandbox Code Playgroud)
服务器版本.
$ mongo so
MongoDB shell version: 2.4.5
connecting to: so
> db.version()
2.4.5
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11428 次 |
| 最近记录: |