如何减少实体框架创建的SQL数量?

时间:2014-04-28 17:51:42

标签: sql entity-framework

我有以下的Linq To EF ......

    private IQueryable<VRTSystem> GetObjectSetWithIncludes() {
        return GetObjectSet()
          .Include(s => s.Site.Address.GeographicalLocation.ParentLocation.ParentLocation)
          .Include(s => s.Dhr.Select(d => d.DhrParts.Select(dp => dp.PhysicalPart.PartDefinition)))
          .Include(s => s.Room.RoomType)
          .Include(s => s.ProductConfiguration.ProductConfigurations_PriceBook.Select(pcpb => pcpb.PriceBook))
          .Include(s => s.ProductConfiguration.Quote_ProductConfigurations)
          .Include(s => s.ProductConfigurationTemplate)
          .Include(s => s.Customer.GeographicalLocation.ParentLocation.ParentLocation)
          .Include(s => s.Customer.CustomerType)
          .Include(s => s.HospitalGroupCustomer.GeographicalLocation.ParentLocation.ParentLocation)
          .Include(s => s.HospitalGroupCustomer.CustomerType)
          .Include(s => s.ServiceCoverHistories.Select(sch => sch.ServiceCoverLevel))
          .Include(s => s.SystemStatus)
          .Include(s => s.ShippingContact.Address)
          .Include(s => s.Quote)
          .Include(s => s.PaidDate)
          .Include(s => s.VRTSystemProductConfigurations
            .Select(
              spc =>
                spc.ProductConfigurationTemplate.ProductConfigurations_PriceBook.Select(
                  pcpb => pcpb.PriceBook)))
          .Include(s => s.VRTSystemProductConfigurations
            .Select(spc => spc.PaidDate))
          .Include(s => s.Quote_ProductConfigurations
            .Select(spc => spc.ProductConfiguration))
          .Include(s => s.ProductConfigurationTemplate
            .ProductConfigurationOptions.Select(pco => pco.ProductConfigurationOptionValues))
          .Include(s => s.SystemProductConfigurationOptionsSelectedValues)
          .Include(s => s.VRTSystemProductConfigurations
            .Select(spc => spc.UpgradeProductConfigurationOptionsSelectedValues));
    }
}

延迟加载已关闭,因为我们通过WCF发送结果,因此需要立即加载所有数据。此外,将使用数据的WPF窗口需要所有相关表。

这已经很好地工作了很长时间,但突然开始抛出异常,查询中有太多的表。我查看了生成的SQL,它几乎是20,000行,并且做了600多个连接!我毫不奇怪它抛出了一定数量的SQL!

我真的不确定它为什么会这么多连接,因为查询没有引用那个数量的表附近的任何地方。

为什么这可能突然开始抛出这个异常的任何想法,以及我可以做些什么来改进这个查询?我意识到最好的答案可能是进行一些重大的重写,将其分解为较小的查询,但除了这样做的复杂性之外,我需要快速修复,因为已部署的系统不再有效,我们需要得到它迫切地再次奔跑。

0 个答案:

没有答案