Asp.net Core Linq 查询花费太多时间

问题描述 投票:0回答:1

我有一个 linq 查询,需要 31 秒。这是我第一次收到这么晚的询问,我不知道该怎么办。 让我向您展示我的查询:

        public IEnumerable<WebFairField> WebFairFieldForFair(Guid ID)
        {
            return TradeTurkDBContext.WebFairField.Where(x => x.DataGuidID==ID)
             .Include(x => x.Category)
             //
             .Include(x=>x.FairSponsors)
             .ThenInclude(x=>x.Company)
             .ThenInclude(x=>x.FileRepos)
             //
             .Include(x=>x.WebFairHalls)
             .ThenInclude(x=>x.HallSeatingOrders)
             .ThenInclude(x=>x.Company)
             .ThenInclude(x=>x.FileRepos)
             //
             .Include(x=>x.HallExpertComments)
             .Include(x=>x.Products)
             .Include(x=>x.FairSponsors)
             .AsNoTracking().ToList();
        }

我确信这是一个正确的查询,但我不知道为什么该查询花费了太多时间。

感谢您的帮助!

asp.net asp.net-mvc linq entity-framework-core ef-code-first
1个回答
5
投票

这称为笛卡尔爆炸。 EF Core 生成 SQL,该 SQL 返回大量记录,这些记录将在客户端聚合。

示意性地:

FairSponsors * FairSponsor.Company.FileRepos * WebFairHalls * WebFairHall.HallSeatingOrders * WebFairHall.HallSeatingOrder.Company.FileRepos * HallExpertComments * Poducts * FairSponsors
。记录太多了不是吗?

EF Core 有运算符

AsSplitQuery()
。尝试应用于您的查询,可能会加快返回结果的速度,但不会太多。
Include
中请求的每个集合都会产生额外的查询。

还可以尝试删除

AsNoTracking()
或添加
AsNoTrackingWithIdentityResolution()
- 在这种情况下,跟踪可以提高查询速度。

© www.soinside.com 2019 - 2024. All rights reserved.