Reputation: 1879
I have a LINQ Query which searches MongoDB for more than 50 million records. I am using the and operator in my query like below:
(from e in this.collection.AsQueryable<SocialRecord>()
where
bArray.Contains(e.TermMonitorIds) &&
(sources.Contains(e.SocialType)) &&
(e.DateCreated >= fr) &&
(e.DateCreated <= to)
select e)
.OrderByDescending(e => e.SocialCount)
.ToList();
Does it make any performance improvement if I change the query the way below:
(from e in this.collection.AsQueryable<SocialRecord>()
where
(e.DateCreated >= fr) &&
(e.DateCreated <= to) &&
bArray.Contains(e.TermMonitorIds) &&
(sources.Contains(e.SocialType))
select e)
.OrderByDescending(e => e.SocialCount)
.ToList();
..because DateCreated
is the one which separates the huge quantity of records. Is there a way to improve the above query?
Upvotes: 0
Views: 80
Reputation: 82096
If this was an in-memory lookup then yes it would be quicker because C# uses short-circuit evaluation for the &&
operator therefore checking the date range first would filter out nodes which don't match.
However, I am not sure of the inner workings of mongoDB so it would really depend on whether it also supported short-circuit evaluation for the AND
conditions.
Turns out MongoDB does support short-circuit evaluation for $and.
Upvotes: 2