I have total 1644662 records in one of my collections, I have created index on one of my column named "document_type_id". When I run a query
db.getCollection('my_collection').find({"document_type_id": {"$in": ["7"]}}).count()
It returns count "753800" in 0.285 secs. And When I run a query
db.getCollection('my_collection').find({"document_type_id": {"$in": ["3"]}}).count()
It returns count "819438" in 0.365 secs. Both queries working perfect. But problem comes when I run query by combining both
db.getCollection('my_collection').find({"document_type_id": {"$in": ["3", "7"]}}).count()
This query takes 54 secs to return the count. Execution Plan for third query is as follows:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.my_collection",
"indexFilterSet" : false,
"parsedQuery" : {
"document_type_id" : {
"$in" : [
"3",
"7"
]
}
},
"winningPlan" : {
"stage" : "COUNT",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"document_type_id" : 1.0
},
"indexName" : "document_type_id_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"document_type_id" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"document_type_id" : [
"[\"3\", \"3\"]",
"[\"7\", \"7\"]"
]
}
}
}
},
"rejectedPlans" : []
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 0,
"executionTimeMillis" : 74166,
"totalKeysExamined" : 1573238,
"totalDocsExamined" : 1573238,
"executionStages" : {
"stage" : "COUNT",
"nReturned" : 0,
"executionTimeMillisEstimate" : 73504,
"works" : 1573239,
"advanced" : 0,
"needTime" : 1573238,
"needYield" : 0,
"saveState" : 14007,
"restoreState" : 14007,
"isEOF" : 1,
"invalidates" : 0,
"nCounted" : 1573238,
"nSkipped" : 0,
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 1573238,
"executionTimeMillisEstimate" : 73253,
"works" : 1573239,
"advanced" : 1573238,
"needTime" : 0,
"needYield" : 0,
"saveState" : 14007,
"restoreState" : 14007,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 1573238,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1573238,
"executionTimeMillisEstimate" : 2729,
"works" : 1573239,
"advanced" : 1573238,
"needTime" : 0,
"needYield" : 0,
"saveState" : 14007,
"restoreState" : 14007,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"document_type_id" : 1.0
},
"indexName" : "document_type_id_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"document_type_id" : []
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"document_type_id" : [
"[\"3\", \"3\"]",
"[\"7\", \"7\"]"
]
},
"keysExamined" : 1573238,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
},
"serverInfo" : {
"host" : "nb-fara",
"port" : 27017,
"version" : "3.4.9",
"gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e"
},
"ok" : 1.0
}
document_type_idnumeric in string ? 2) you have FETCH stage in your explain output, what's your actual query that generated the explain ? 3) what's your MongoDB deployment topology ? standalone, replicaset or sharded cluster.Object.bsonsize(db. my_collection.findOne())