Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 方法链接性能问题_C#_Performance_Entity Framework_Method Chaining - Fatal编程技术网

C# 方法链接性能问题

C# 方法链接性能问题,c#,performance,entity-framework,method-chaining,C#,Performance,Entity Framework,Method Chaining,我在寻找建议/建议。我有一个长期运行的过程,我正在努力优化。这是运行时间最长的进程,每次调用返回填充列表的时间为1分钟48秒。我在这里循环,每个请求有1000条记录,所以你可以想象分钟加起来有多快。我已经将所有其他方法优化到每1000次少于10秒。我希望能够加快速度 GetLoans()从数据库检索数据并将其发送到getLoansList()以创建一个要发送到restful Web服务的集合 GetRequestedWatches()返回一个类似于“1,2”的字符串,因此速度非常快 前面带有“

我在寻找建议/建议。我有一个长期运行的过程,我正在努力优化。这是运行时间最长的进程,每次调用返回填充列表的时间为1分钟48秒。我在这里循环,每个请求有1000条记录,所以你可以想象分钟加起来有多快。我已经将所有其他方法优化到每1000次少于10秒。我希望能够加快速度

  • GetLoans()从数据库检索数据并将其发送到getLoansList()以创建一个要发送到restful Web服务的集合

  • GetRequestedWatches()返回一个类似于“1,2”的字符串,因此速度非常快

  • 前面带有“\”的变量是实例化时加载的类级属性

我尝试过快速加载和延迟加载。目前为止,快速加载的性能最好

如有任何建议,将不胜感激

private List<SBLead> GetLoanList(IEnumerable<LoanCategoryMonitor> loans)
{
    return (loans.Where(selectedItem => selectedItem.Loan != null)
        .Select(selectedItem => new SBLead
        {
            ApiKey = _settings.apiKey,
            CrmId = selectedItem.LoanNumber.ToString(),
            CrmName = _crmName,
            LoanType = selectedItem.Loan.Loan_Type,
            Email = selectedItem.Loan.abcBorrower?.EMail_Address,
            FirstName = selectedItem.Loan?.Borr_First_Name,
            LastName = selectedItem.Loan?.Borr_Last_Name,
            Address = selectedItem.Loan?.Address,
            City = selectedItem.Loan?.City,
            State = selectedItem.Loan?.State,
            Zip = selectedItem.Loan?.Zip,
            Phone = string.Empty,
            WatchTypes = GetRequestedWatches(selectedItem.Category.GetEnumFromString<Category>()),
            UserEmail = _defaultLoanOfficerEmail,
            UserName = _defaultLoanOfficerName
        })).ToList();
}

public IEnumerable<LoanCategoryMonitor> GetLoans()
{
    var loanCollection = _be.LoanCategoryMonitors
        .Include(c => c.Loan)
        .Where(r => r.ReadyForUpdate == true && r.LoanExtracts == null &&
                    r.Category != Category.None.ToString())
        .AsNoTracking()
        .ToList();

    return loanCollection;
}
私有列表GetLoanList(IEnumerable贷款)
{
返回(loans.Where(selectedItem=>selectedItem.loans!=null)
.选择(selectedItem=>new SBLead
{
ApiKey=\u settings.ApiKey,
CrmId=selectedItem.LoanNumber.ToString(),
CrmName=_CrmName,
LoanType=选择editem.Loan.Loan\u类型,
Email=选择editem.Loan.abcBorrower?.Email\u地址,
FirstName=选择editem.Loan?.Borr\u First\u Name,
LastName=selectedItem.Loan?.Borr\u Last\u Name,
Address=选择Item.Loan?.Address,
City=选择Item.Loan?.City,
State=选择editem.Loan?.State,
Zip=选择editem.Loan?.Zip,
Phone=string.Empty,
WatchTypes=GetRequestedWatches(selectedItem.Category.GetEnumFromString()),
UserEmail=\u defaultLoanOfficeMail,
用户名=_defaultLoanOfficerName
})).ToList();
}
公共IEnumerable GetLoans()
{
var loanCollection=\u be.LoanCategoryMonitors
.包括(c=>c.贷款)
.Where(r=>r.ReadyForUpdate==true&&r.LoanExtracts==null&&
r、 Category!=Category.None.ToString()
.AsNoTracking()
.ToList();
归还贷款;
}

我找到了。我丢失了abcBorrower的一个Include。新的执行速度几乎是瞬间的,我可以接受。这是每1000条记录节省1分48秒。感谢您的输入


那么,实际花费的时间在哪里?查询数据库?其他地方?你确定你已经正确地测量并正确地识别了导致速度减慢的原因吗?如果你经常同时这样做,那么我强烈建议对任何执行IO的方法改为
async/await
。@spender GetLoanList()返回包含1000个实体的列表几乎需要2分钟。你知道为什么吗?对于一个查询来说,这似乎需要很长的时间才能完成。不,我不知道为什么。这就是为什么它出现在这里。我假设这是因为它从子实体加载值减慢了它的速度。所以关闭所有延迟加载。据我所知,
GetLoanList
不需要点击数据库,对吗?从所有模型属性中删除
virtual
,然后重新运行代码。目前,这会引发一系列NullReferenceException。现在,您应该添加相应的
Include(c=>c.Loan.abcBorrower)
等。因此,数据在查询时从数据库中提取。您还应该在查询时检查
selectedItem.Loan!=null
,如果可能的话,不要在一个具体化的列表上。1000条记录的2秒似乎很慢。到目前为止,我已经将性能提高了1000%。我现在需要停止并获取新的基准。这里的这一条是节省了大量的时间。很高兴你找到了答案。这就是为什么我倾向于推荐一个SP用于非琐碎的东西。它通常可以节省大量的开发时间和执行时间。我必须承认,当我看到“如何将此SQL转换为LINQ”之类的问题时,我有点翻白眼了!
Email = selectedItem.Loan.abcBorrower?.EMail_Address,