The base collection has 100,000 documents. All of which will be run through a $lookup
stage. The foreign collection that will be looked up is empty.
This query takes 30 seconds.
When I remove the $lookup
stage, this query takes 3 seconds.
What explains why it is so slow, despite the foreign collection being empty, having no documents?
Similarly, If I join based _id
, which is indexed of course, the $lookup
could increase the time of the query by 4 times. I know that the index is used because if I join by some other field that is not indexed, the query actually timeout and returns an error. So how come looking up using an indexed field, _id
, increase the query time by 4x?