Asp.net 核心 Linq 查询花费太多时间
Asp.net Core Linq query takes too much time
我有一个需要 31 秒的 linq 查询。这是我第一次收到这么晚的查询,我不知道该怎么做。
让我向您展示我的查询:
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();
}
我确定这是一个正确的查询,但我不知道为什么该查询花费了太多时间。
感谢您的帮助!!
这叫做笛卡尔爆炸。 EF Core 产生 SQL 其中 returns 很多记录,这些记录将在客户端聚合。
示意图:FairSponsors * FairSponsor.Company.FileRepos * WebFairHalls * WebFairHall.HallSeatingOrders * WebFairHall.HallSeatingOrder.Company.FileRepos * HallExpertComments * 产品 * FairSponsors。记录太多了,不是吗?
EF Core 有运算符 AsSplitQuery()
。尝试应用于您的查询,它可能会加快返回结果的速度,但不会太多。 Include
中请求的每个集合都会产生额外的查询。
也尝试删除 AsNoTracking()
或添加 AsNoTrackingWithIdentityResolution()
- 这种情况下跟踪可能会提高查询速度。
我有一个需要 31 秒的 linq 查询。这是我第一次收到这么晚的查询,我不知道该怎么做。 让我向您展示我的查询:
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();
}
我确定这是一个正确的查询,但我不知道为什么该查询花费了太多时间。
感谢您的帮助!!
这叫做笛卡尔爆炸。 EF Core 产生 SQL 其中 returns 很多记录,这些记录将在客户端聚合。
示意图:FairSponsors * FairSponsor.Company.FileRepos * WebFairHalls * WebFairHall.HallSeatingOrders * WebFairHall.HallSeatingOrder.Company.FileRepos * HallExpertComments * 产品 * FairSponsors。记录太多了,不是吗?
EF Core 有运算符 AsSplitQuery()
。尝试应用于您的查询,它可能会加快返回结果的速度,但不会太多。 Include
中请求的每个集合都会产生额外的查询。
也尝试删除 AsNoTracking()
或添加 AsNoTrackingWithIdentityResolution()
- 这种情况下跟踪可能会提高查询速度。