Ber*_*rip 2 linq asp.net asp.net-mvc ef-code-first entity-framework-core
我有一个 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();
}
Run Code Online (Sandbox Code Playgroud)
我确信这是一个正确的查询,但我不知道为什么该查询花费了太多时间。
谢谢你的帮助!!
它被称为笛卡尔爆炸。EF Core 生成 SQL,该 SQL 返回大量记录,这些记录将在客户端聚合。
示意性地:FairSponsors * FairSponsor.Company.FileRepos * WebFairHalls * WebFairHall.HallSeatingOrders * WebFairHall.HallSeatingOrder.Company.FileRepos * HallExpertComments * Poducts * FairSponsors. 记录太多了不是吗?
EF Core 有运算符AsSplitQuery()。尝试应用于您的查询,可能会加快返回结果的速度,但不会太多。请求的每个集合Include都会产生额外的查询。
还可以尝试删除AsNoTracking()或添加AsNoTrackingWithIdentityResolution()- 在这种情况下跟踪可以提高查询速度。