We are facing an issue in our query where using “sort” significantly increases execution time.
The query is this:
db.collections.aggregate([
{$match: {$and: [{"products.productInfo.productId": {"$gte": 93143, "$lte": 93643}}]}},
{$unwind: "$products"},
{$match: {$and: [{"products.productInfo.productId": {"$gte": 93143, "$lte": 93643}}]}},
{$sort: {"products.productInfo.productId": 1}},
{$limit: 50}
])
We use unwind because our document has a list of products and filters from a range of ids. The productId is an index.
ExecutionStats without the “sort” clause (average time below 1s):
“nReturned” : 163
“executionTimeMillis” : 16
“totalKeysExamined” : 351
“totalDocsExamined” : 297
ExecutionStats with the “sort” clause (average time 14s):
“nReturned” : 1919
“executionTimeMillis” : 17504
“totalKeysExamined” : 847869
“totalDocsExamined” : 451385
I read this in a post:
This happens because of $unwind stage.Your query performance is slow because query is not considering index after the $unwind stage.Check that with explain.you will get to know.This happens because after the $unwind whole documents will change and it becomes different that is stored in RAM for indexing purpose.
Is that why sort degrades the query?