Hello! We are having some performance issues with our countDocuments() query as it is taking > 17 seconds (our client-side timeout is 17s) for most queries to complete execution during our load testing. This is only happening during load testing and the query returns in ~300ms when the system is not under load. This query, in particular, is the only one that has performance issues as I can compare it to the others from the Profiler within Atlas. The collection itself has 680k documents and we are using Mongoose within our node.js app.
I am wondering if there are any best practices for optimizing the countDocuments() operation?
Models.Pass.countDocuments({ show: showId }).maxTimeMS(17000)
Here is the the log document from the Profiler:
{
"command": {
"aggregate": "passes",
"pipeline": [
{
"$match": {
"show": {
"$oid": "5d8021bd1c4eef00086d4fb6"
}
}
},
{
"$group": {
"_id": 1,
"n": {
"$sum": 1
}
}
}
],
"cursor": {},
"lsid": {
"id": {
"$uuid": "c54a4dc4-d595-fe6d-16b9-8517594f2da1"
}
},
"$clusterTime": {
"clusterTime": {
"$timestamp": {
"t": 1584299634,
"i": 255
}
},
"signature": {
"hash": {
"$binary": "PHh4eHh4eD4=",
"$type": 0
},
"keyId": 6778896889304580000
}
},
"$db": "main"
},
"planSummary": [
{
"COUNT_SCAN": {
"show": 1
}
}
],
"numYields": 1836,
"queryHash": "97FA1A2E",
"planCacheKey": "D86294E6",
"ok": 0,
"errMsg": "Error in $cursor stage :: caused by :: operation was interrupted because a client disconnected",
"errName": "ClientDisconnect",
"errCode": 279,
"reslen": 311,
"locks": {
"ReplicationStateTransition": {
"acquireCount": {
"w": 1838
}
},
"Global": {
"acquireCount": {
"r": 1838
}
},
"Database": {
"acquireCount": {
"r": 1837
}
},
"Collection": {
"acquireCount": {
"r": 1837
}
},
"Mutex": {
"acquireCount": {
"r": 2
}
}
},
"protocol": "op_msg",
"millis": 17594
}