我们如何优化此 linq 到实体查询以减少响应时间?
IQueryable<WebEvent> mySearch =
eventDC.GetBooks()
.Where(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>();
我们有这么长的查询,获取书籍和排序消耗了很多时间,经过性能测试,我们发现包含这个查询的页面响应时间超过10秒,我们需要寻求解决这个问题并减少响应时间。
有人有什么建议吗?
IQueryable<WebEvent> mySearch =
eventDC.GetBooks()
.Where(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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你到底想做什么?你能给我一个关于这里的模式的想法吗?
这对我来说似乎是一个奇怪的说法,因为我不知道架构:
但是,我要在黑暗中尝试一下,并说您可能会遇到以下问题:
如果该方法调用存储过程或以其他方式在数据库上执行“Select * From Books”,那么您实际要做的是:
,那么这可能是你最大的问题。
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:
However, I'm gonna take a shot in the dark here and say that you might have an issue with:
if that method calls a Stored Procedure or otherwise does a "Select * From Books" on the database, then what you're actually doing is:
If this is the case, then that's probably your biggest problem.
通常检查 SQL 以查看它生成的内容,您可以内联执行此操作。有一个工具可以帮助您做到这一点,它称为 LinqPad,您可以创建 LINQ 查询并尝试调整 LINQ 查询。另外,寻找添加索引的地方;这也可以提高性能(太多索引会损害性能,所以也要小心)。
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).