Reputation: 13
var product = db.GetCollection<Product>("Product");
var lookup1 = new BsonDocument(
"$lookup",
new BsonDocument {
{ "from", "Variant" },
{ "localField", "Maincode" },
{ "foreignField", "Maincode" },
{ "as", "variants" }
}
);
var pipeline = new[] { lookup1};
var result = product.Aggregate<Product>(pipeline).ToList();
The data of collection a is very large so it takes me 30 seconds to put the data in the list. What should I do to make a faster lookup?
Upvotes: 1
Views: 936
Reputation: 28376
What that query is doing is retrieving every document from the Product
collection, and then for each document found, perform a find query in the Variant
collection. If there is no index on the Maincode
field in the Variant
collection, it will be reading the entire collection for each document.
This means that if there are, say, 1000 total products, with 3000 total variants (3 per product, on average), this query will be reading all 1000 documents from Product
, and if that index isn't there, it would read all 3000 documents from Variant
1000 times, i.e. it will be examining 3 million documents.
Some ways to possibly speed this up:
{Maincode:1}
in the Variant
collection
This will reduce the number of documents that must be read in order to complete the lookupUpvotes: 1