I currently have a query using mongodb3.4, that needs to be paged. Paging needs to return the total number and number of pages.
but that"s the problem. Paging queries are fast, but counting is slow
count statement is as follows:
db.message.find(
{
"field_1":"ajian",
"field_2":{ "$exists": true },
"field_3":{ "$exists": true },
"field_4":{ "$exists": true },
"field_5":{"$regex":value1},
"field_6":{"$regex":value2}
}
).count()
it takes 2.5 seconds to return the result!
the info results of my explain are as follows:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "engine.message",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"field_1" : {
"$eq" : "ajian"
}
},
{
"field_2" : {
"$regex" : ""
}
},
{
"field_3" : {
"$regex" : ""
}
},
{
"field_4" : {
"$exists" : true
}
},
{
"field_5" : {
"$exists" : true
}
},
{
"field_6" : {
"$exists" : true
}
}
]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [
{
"field_1" : {
"$eq" : "ajian"
}
},
{
"field_2" : {
"$regex" : ""
}
},
{
"field_3" : {
"$regex" : ""
}
},
{
"field_4" : {
"$exists" : true
}
},
{
"field_5" : {
"$exists" : true
}
},
{
"field_6" : {
"$exists" : true
}
}
]
},
"direction" : "forward"
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1537253,
"executionTimeMillis" : 2536,
"totalKeysExamined" : 0,
"totalDocsExamined" : 1610191,
"executionStages" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [
{
"field_1" : {
"$eq" : "ajian"
}
},
{
"field_2" : {
"$regex" : ""
}
},
{
"field_3" : {
"$regex" : ""
}
},
{
"field_4" : {
"$exists" : true
}
},
{
"field_5" : {
"$exists" : true
}
},
{
"field_6" : {
"$exists" : true
}
}
]
},
"nReturned" : 1537253,
"executionTimeMillisEstimate" : 2417,
"works" : 1610193,
"advanced" : 1537253,
"needTime" : 72939,
"needYield" : 0,
"saveState" : 12588,
"restoreState" : 12588,
"isEOF" : 1,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 1610191
},
"allPlansExecution" : []
},
"serverInfo" : {
"host" : "secret",
"port" : 27017,
"version" : "3.4.10",
"gitVersion" : "2234234232325323343"
},
"ok" : 1.0
}
Why is mongo"s count count so slow? How do the gods solve this problem when they do paging? How should I solve this problem?