Entity Framework 对象 LINQ 查询超时问题

Entity Framework object LINQ query Timeout issues

我试图修改我的连接字符串以包含延长的超时时间,并且我已经确认在 sql 服务器端,为我的 EF 对象提供数据的视图会在几秒钟内执行 return总共3000条或更少的记录。

但是当我尝试通过代码 运行 它时,我现在 运行 遇到了超时问题,我正在寻求一些建议来解决这个问题。我得到 "Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding." 我在特定错误上找到的大多数解决方案都建议修改连接字符串或 this.context.CommandTimeout... 我无法弄清楚在这种情况下如何使用。

我已经包括了我用来获取所需数据的方法。如果有更有效的方法请告诉我。

输入参数是:

目的是 return 完整列表。

它挂在了,因为它跳过了所有条件位: var qUniqueOffers = query.GroupBy(q => q.Plan_Number).ToList();

谢谢。


private List<PromotionItem> QueryPromotion(int? inputSKU, int? inputStoreNum, DateTime? inputStartDate)
{
    log.Info("Client requested QueryPromotion");
    List<PromotionItem> resultQuery = new List<PromotionItem>();

    try
    {
        using (DWH_Entities db = new DWH_Entities())
        {
            var query = db.vw_Web_Promotion.AsQueryable();

            // filter promotion results that don't match SKU#
            if (inputSKU != null)
                query = query.Where(q => q.Sku_Number == inputSKU);
            // filter promotion results that don't match Store Num
            if (inputStoreNum != null)
                query = query.Where(q => q.Store_Number == inputStoreNum);
            // filter promotion results that don't match Promotion Start Date
            if (inputStartDate != null)
                query = query.Where(q => q.Start_Date >= inputStartDate);
            // Group promotions By Plan Number ('Promotion ID')
            var qUniqueOffers = query
                                .GroupBy(q => q.Plan_Number)
                                .ToList();
            // Select first from each group to get unique details
            var qOffers = qUniqueOffers
                        .Select(g => g.OrderBy(gi => gi.Plan_Number).First())
                        .ToList();

            foreach (var qo in qOffers)
            {
                resultQuery.Add(new PromotionItem
                {
                    PromotionNumber = qo.Plan_Number.Trim(),
                    PromotionDescription = qo.Plan_Description.Trim(),
                    StartDate = qo.Start_Date,
                    EndDate = qo.End_Date
                });
            }
        }
    }
    catch (Exception e)
    {
        log.Error("[" + e.TargetSite + "] | " + e.Message);
        throw e;
    }

    return resultQuery;
}

如果您使用的是最新的 EF 版本,请执行以下操作以增加超时:

using (DWH_Entities db = new DWH_Entities())
{
    db.Database.CommandTimeout = 300;
    ...

如果你想在最短的时间内完成记录,请尝试以下操作:

var temp = query.ToList();
var qUniqueOffers = temp.GroupBy(q => q.Plan_Number)
                                .ToList();
// Group promotions By Plan Number ('Promotion ID')
var qUniqueOffers = query
                    .GroupBy(q => q.Plan_Number)
                    .ToList();
// Select first from each group to get unique details
var qOffers = qUniqueOffers
            .Select(g => g.OrderBy(gi => gi.Plan_Number).First())
            .ToList();

您编写上述 LINQ 的方式意味着您通过网络提取大量数据(第一个 ToList),然后获取数据的一个子集(使用 First 和第二 ToList)。考虑将其更改为:

// Group promotions By Plan Number ('Promotion ID')
var qUniqueOffers = query
                    .GroupBy(q => q.Plan_Number)
// Select first from each group to get unique details
var qOffers = qUniqueOffers
            .Select(g => g.OrderBy(gi => gi.Plan_Number).First())
            .ToList();

这应该会导致从数据库发送的数据少很多 - 这有望使其更快。

如 所述:

ToList() always forces everything ahead of it to evaluate immediately, as opposed to deferred execution.