我在MongoDB中有以下方面查询,但性能却很差。与未使用汇总的查询相比,以下是多面查询。有谁知道如何使多面查询的性能更好?
db.texts.explain("allPlansExecution").aggregate([ { '$facet': { texts: [ { '$limit': 30 } ] } } ])
以下说明
{
"stages" : [
{
"$cursor" : {
"query" : {
},
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "db.texts",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "COLLSCAN",
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 3497,
"executionTimeMillis" : 231,
"totalKeysExamined" : 0,
"totalDocsExamined" : 3497,
"executionStages" : {
"stage" : "COLLSCAN",
"nReturned" : 3497,
"executionTimeMillisEstimate" : 0,
"works" : 3499,
"advanced" : 3497,
"needTime" : 1,
"needYield" : 0,
"saveState" : 40,
"restoreState" : 40,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 3497
},
"allPlansExecution" : [ ]
}
}
},
{
"$facet" : {
"texts" : [
{
"$limit" : NumberLong(30)
}
]
}
}
],
"ok" : 1
}
我将上述查询与没有查询的查询进行了比较,并且速度更快
db.texts.explain("allPlansExecution").find({}).limit(30)
解释
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "copybank.texts",
"indexFilterSet" : false,
"parsedQuery" : {
},
"winningPlan" : {
"stage" : "LIMIT",
"limitAmount" : 30,
"inputStage" : {
"stage" : "COLLSCAN",
"direction" : "forward"
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 30,
"executionTimeMillis" : 0,
"totalKeysExamined" : 0,
"totalDocsExamined" : 30,
"executionStages" : {
"stage" : "LIMIT",
"nReturned" : 30,
"executionTimeMillisEstimate" : 0,
"works" : 32,
"advanced" : 30,
"needTime" : 1,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"limitAmount" : 30,
"inputStage" : {
"stage" : "COLLSCAN",
"nReturned" : 30,
"executionTimeMillisEstimate" : 0,
"works" : 31,
"advanced" : 30,
"needTime" : 1,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 0,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 30
}
},
"allPlansExecution" : [ ]
},
"ok" : 1
}