db.inventory.aggregate( [
{
$lookup:
{
from: "order",
localField: "_id",
foreignField: "item_id",
as: "inventory_docs"
}
}
] )
The $lookup
is joining based on the item_id
field, which is indexed.
If 100,000 documents pass through this $lookup
, it increased the query time to 4X than without this $lookup
.
Given that the $lookup
is indexed, it is unexpected that the query will be slower by 4X. I was expecting a marginal increase in query time.
Is this also the case for SQL databases? Will an indexed join increase query time by 4X?
explain document:
{
"$lookup": {
"from": "order",
"as": "inventory_docs",
"localField": "_id",
"foreignField": "item_id",
"let": {},
"pipeline": [
{
"$project": {
"_id": 1
}
}
]
},
"totalDocsExamined": 0,
"totalKeysExamined": 100008,
"collectionScans": 0,
"indexesUsed": [
"_id_"
],
"nReturned": 100008,
"executionTimeMillisEstimate": 18801
}