【问题标题】:"Execution Timeout" on converting LINQ query results using ToList()使用 ToList() 转换 LINQ 查询结果的“执行超时”
【发布时间】:2018-06-20 09:59:31
【问题描述】:

正如标题所述,我正在维护的模块上收到“等待操作超时”消息(内部异常消息:“超时过期”)。每次应用程序尝试使用ToList() 转换查询结果时,无论结果数量如何,它都会超时。

需要将其转换为列表的原因:需要将结果导出到 Excel 以供下载。

下面是代码:

public Tuple<IEnumerable<ProductPriceSearchResultDto>, int> GetProductPriceSearchResults(ProductPriceFilterDto filter, int? pageNo = null)
{
    //// Predicate builder
    var predicate = GetProductPriceSearchFilter(filter);

    //// This runs for approx. 1 minute before throwing a "Wait operation timed out" message...
    var query = this.GetProductPriceSearchQuery()
                    .Where(predicate)
                    .Distinct()
                    .OrderBy(x => x.DosageFormName)
                    .ToList();

    return Tuple.Create<IEnumerable<ProductPriceSearchResultDto>, int>(query, 0);
}

我的查询:

var query = (from price in this.context.ProductPrice.AsExpandable()
             join product in this.context.vwDistributorProducts.AsExpandable() 
                 on price.DosageFormCode equals product.DosageFormCode
             join customer in this.context.vwCustomerBranch.AsExpandable()
                 on price.CustCd equals customer.CustomerCode
             where price.CountryId == CurrentUserService.Identity.CountryId && !product.IsInactive
             select new { price.PriceKey, price.EffectivityDateFrom, price.ContractPrice, price.ListPrice,
                          product.DosageFormName, product.MpgCode, product.DosageFormCode,
                          customer.CustomerName }).GroupBy(x => x.DosageFormCode)
             .Select(x => x.OrderByDescending(y => y.EffectivityDateFrom).FirstOrDefault())
             .Select(
                 x =>
                 new ProductPriceSearchResultDto
                 {
                     PriceKey = x.PriceKey,
                     DosageFormCode = x.DosageFormCode,
                     DosageFormName = x.DosageFormName,
                     EffectiveFrom = x.EffectivityDateFrom,
                     Price = x.ListPrice,
                     MpgCode = x.MpgCode,
                     ContractPrice = x.ContractPrice,
                     CustomerName = x.CustomerName
                 }); 

return query;

注意事项:

  1. ProductPrice 是一个表,并且有一个指向列 CountryIdDosageFormCode 的非聚集索引。
  2. vwDistributorProductsvwCustomerBranch 是从客户端数据库复制的视图。

我已经无计可施了。我该如何摆脱这个错误?我需要更改代码中的某些内容吗?

编辑: 尽可能地,我不想求助于设置命令超时,因为 1.) 到目前为止,如果没有它,应用程序还可以......除了这个功能和 2. ) 这已经是一个巨大的应用程序,我不想让其他模块的性能处于危险之中。

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • @DeeJ。您是否将 LINQ to Objects 与 LINQ to SQL 混淆了? L2S 是一个不同的 ORM。至于错误本身,它意味着 查询 超时,而不是结果的转换。很可能您在其中一个连接或调用.AsExpandable() 时将整个表加载到内存中(那是什么?)。
  • @DeeJ。您不应该需要任何与 EF 或任何 ORM 的联接。您应该指定适当的关系和导航属性,以便您不必 JOIN,并且 ORM 可以生成正确的查询,最终不会将整个数据库加载到内存中。
  • 嗨@PanagiotisKanavos,谢谢你的信息。这是否意味着我应该更改查询的编写方式?
  • @Deej。 LINQ 中没有 AsExpandable() 函数。该调用几乎可以肯定最终会在内存中加载每个产品、价格、分支等以进行内存连接,因此超时

标签: c# entity-framework linq


【解决方案1】:

我会尝试记录转换成的 sql。 然后可以使用实际的 sql 来获取查询计划,这可能会导致您更接近根本原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多