Asp.net Core Linq查询花费太多时间 [英] Asp.net Core Linq query takes too much time

查看:159
本文介绍了Asp.net Core Linq查询花费太多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a linq query which is takes 31 second. This is the first time that im getting that much late query and i dont know what to do. Let me show you to my query :

        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();
        }

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

im sure about that this is a right query but i dont know why that query is tooking too much time.

感谢您的帮助!

推荐答案

它称为笛卡尔爆炸.EF Core生成SQL,该SQL返回很多记录,这些记录将在客户端进行汇总.

It is called Cartesian Explosion. EF Core produces SQL which returns a lot of records which will be aggregated on the client side.

示意性地:FairSponsors * FairSponsor.Company.FileRepos * WebFairHalls * WebFairHall.HallSeatingOrders * WebFairHall.HallSeatingOrder.Company.FileRepos * HallExpertComments *产品* FairSponsors.记录太多了,不是吗?

Schematically: FairSponsors * FairSponsor.Company.FileRepos * WebFairHalls * WebFairHall.HallSeatingOrders * WebFairHall.HallSeatingOrder.Company.FileRepos * HallExpertComments * Poducts * FairSponsors. Too much records, isn't?

EF Core具有运算符 AsSplitQuery().尝试将其应用于您的查询,可能会加快返回结果的速度,但不要太大.在 Include 中请求的每个集合都将产生其他查询.

EF Core has operator AsSplitQuery(). Try to apply to your query and probably it will speedup returning result, but not too much. Each collection requested in Include will produce additional query.

还尝试删除 AsNoTracking()或添加 AsNoTrackingWithIdentityResolution()的情况-跟踪可能会提高查询速度.

Also try to play with removing AsNoTracking() or adding AsNoTrackingWithIdentityResolution() - it is case when tracking may improve query speed.

这篇关于Asp.net Core Linq查询花费太多时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆