我们如何优化此 linq 到实体查询以减少响应时间?

发布于 2024-08-17 02:28:35 字数 637 浏览 6 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆那么伤 2024-08-24 02:28:35

你到底想做什么?你能给我一个关于这里的模式的想法吗?

这对我来说似乎是一个奇怪的说法,因为我不知道架构:

p =>; p.Price.Any(d => d.EventDatetime.Month...

但是,我要在黑暗中尝试一下,并说您可能会遇到以下问题:

eventDC.GetBooks()

如果该方法调用存储过程或以其他方式在数据库上执行“Select * From Books”,那么您实际要做的是:

  1. 从数据库中选择所有书籍
  2. 获取结果列表并仅选择书籍 如果是这种情况

,那么这可能是你最大的问题。

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:

  1. Selecting ALL books from the DB
  2. 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.

划一舟意中人 2024-08-24 02:28:35

通常检查 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文