实体框架导致超时错误

Jon*_*ood 5 c# linq sql-server entity-framework

我正在使用以下实体框架查询.我知道这里有很多事情,但我希望有一个人可能能够发现问题.

var lineItems = from li in Repository.Query<CostingLineItem>()

                let cid = (li.ParentCostingPackage != null) ?
                    li.ParentCostingPackage.ParentCostingEvent.ProposalSection.Proposal.Costing.Id :
                    li.ParentCostingEvent.ProposalSection.Proposal.Costing.Id

                where cid == costingId &&
                    li.OriginalProductId.HasValue &&
                    (li.Quantity.HasValue && li.Quantity.Value > 0) &&  // li.QuantityUnitMultiplier
                    Classifications.Contains(li.OriginalProduct.ClassificationEnumIndex)

                let selectedChoiceId = li.OriginalPackageOptionId.HasValue ?
                    (from c in li.OriginalPackageOption.CostingLineItems
                        orderby (c.IsIncluded ?? false) ? -2 : (c.IsDefaultItem ?? false) ? -1 : c.Id
                        select (int)c.OriginalPackageOptionChoiceId).FirstOrDefault() :
                    0

                where selectedChoiceId == 0 || (li.OriginalPackageOptionChoiceId.HasValue && li.OriginalPackageOptionId.Value == selectedChoiceId)

                let hasProviderAvailable = li.OriginalProductItem.ProductItemVendors.Any(
                    piv => piv.ProductPricings.Any(pp => pp.ProductItemVendor.CompanyId != null || pp.ProductItemVendor.HotelId != null))

                select new
                {
                    LineItem = li,
                    ProductItem = li.OriginalProductItem,
                    Product = li.OriginalProduct,
                    Vendors = li.CostingLineItemVendors,
                    HasProviderAvailable = hasProviderAvailable
                };
Run Code Online (Sandbox Code Playgroud)

这样,此查询会生成以下运行时错误:

等待操作超时

如果我将声明的部分更改为selectedChoiceId以下内容,则错误消失:

let selectedChoiceId = 0
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到该代码如何一直导致超时错误?

(注意:此代码是已经运行了几年的大型应用程序的一部分.所以我真的不认为这与连接字符串或类似的东西有任何关系.如果我进行上述更改,它会始终如一地工作.)

Ger*_*old 0

可以通过多种方式简化查询,这应该使数据库引擎更容易优化。

首先,您可以删除一些空检查 ( HasValue),因为它们与 SQL 无关,但它们确实会使生成的 SQL 膨胀。

其次,我认为这个检查涉及到selectedChoiceId可以大大简化。我认为该声明可能如下所示:

from li in Repository.Query<CostingLineItem>()

let cid = (li.ParentCostingPackage != null) ?
    li.ParentCostingPackage.ParentCostingEvent.ProposalSection.Proposal.Costing.Id :
    li.ParentCostingEvent.ProposalSection.Proposal.Costing.Id

where cid == costingId &&
    li.OriginalProductId.HasValue &&
    li.Quantity > 0 &&  // no null check
    Classifications.Contains(li.OriginalProduct.ClassificationEnumIndex)

let selectedChoiceId = (from c in li.OriginalPackageOption.CostingLineItems
        orderby c.IsIncluded ? -2 : c.IsDefaultItem ? -1 : c.Id // no null checks
        select (int)c.OriginalPackageOptionChoiceId).FirstOrDefault()

where !li.OriginalPackageOptionId.HasValue || li.OriginalPackageOptionId == selectedChoiceId

let hasProviderAvailable = li.OriginalProductItem.ProductItemVendors.Any(
    piv => piv.ProductPricings.Any(pp => pp.ProductItemVendor.CompanyId != null || pp.ProductItemVendor.HotelId != null))

select new
{
    LineItem = li,
    ProductItem = li.OriginalProductItem,
    Product = li.OriginalProduct,
    Vendors = li.CostingLineItemVendors,
    HasProviderAvailable = hasProviderAvailable
}
Run Code Online (Sandbox Code Playgroud)

对于其余的,当然有常见的嫌疑人。随着数据库容量的增加,更好的索引可能变得更加重要。检查(并修复)数据库碎片也会产生重大影响。