How can we optimize this linq to entity query to decrease the response time?
IQueryable<WebEvent> mySearch =
eventDC.GetBooks()
.W开发者_JAVA百科here(p => p.Price.Any(d => d.EventDatetime.Month == fromDate.Month
&& d.EventDatetime.Year == fromDate.Year))
.WithGroup(groupId)
.OrderBy(p => p.Price.Where(r => r.Datetime >= fromDate)
.OrderBy(q => q.Datetime)
.FirstOrDefault().Datetime);
List<Book>ventsList = mySearch.ToList<Book>();
We have such a long query, and it consume much time to get the books and sorting, after performance test , we found response time for the page which contains this query exceed 10 seconds, and we need to seek to solve this and reduce the response time.
Do anyone have any suggestions ?
What exactly are you trying to do? Can you give me an idea of the schema here?
This seems like an odd statement to me since I don't know the schema:
p => p.Price.Any(d => d.EventDatetime.Month...
However, I'm gonna take a shot in the dark here and say that you might have an issue with:
eventDC.GetBooks()
if that method calls a Stored Procedure or otherwise does a "Select * From Books" on the database, then what you're actually doing is:
- Selecting ALL books from the DB
- Taking the resulting list and selecting only the books you want from it
If this is the case, then that's probably your biggest problem.
Typically examine the SQL to see what it's producing, which you can do inline. There is a tool that can help you do that, it's called LinqPad, and you can create a LINQ query and play around with tweaking the LINQ query. Also, looking for places to add indexes; this can speed up performance too (too many indexes can hurt performance so be careful too).
精彩评论