Tyress
Tyress

Reputation: 3653

Very slow LINQ with where statements

I have a LINQ that looks like this:

var something = db.Calculations
            .Where(x => x.CalculationDate.Date == justTheDate 
            && x.CalculationID == externalObject.CalculationID 
            && x.CalculationDate >= minDate 
            && x.CalculationDate <= maxDate).GroupBy(x => 
            (int)x.CalculationDate.TimeOfDay.TotalMinutes);

where justTheDate, minDate, maxDate (DateTime) and externalObject.CalculationID (string) are all variables found in the scope.

When I enumerate it (using something like something.Select(x=>x.Last()).ToList()) it takes around 15 seconds. There is a lot of data but not close to what is expected in production.

Anyway to make this query faster?

Upvotes: 2

Views: 606

Answers (1)

DooDoo
DooDoo

Reputation: 13437

You can use this code to get generated SQL query:

var objectQuery = something as System.Data.Objects.ObjectQuery;
string strQuery = objectQuery.ToTraceString();

after get query you can execute it on database directly. If it is slow again you should create an appropriate index for your where clause columns.

Upvotes: 4

Related Questions