【发布时间】:2017-12-30 11:44:59
【问题描述】:
我的应用程序中有两个模型:获取客户名称的客户端和获取有关购买信息的付款。结果,我得到了每个客户在时间间隔内的购买清单 - fromDate 和 toDate。但是所有这些过程都需要太多时间。因为客户的数据库大约有 1.500 条记录并且付款 = 50 万。那么如何加快这个过程呢?
public async Task<List<SomeModel>> SomeMethod(DateTime? fromDate, DateTime? toDate)
{
var clients = await _db.Clients.ToListAsync();
var totals = new List<SomeModel>();
foreach (var client in clients)
{
var payment = await _db.Payments.Where(pay => pay.ClientId == client.Id).Where(
p =>
DateTime.Compare(p.TradeDate, (DateTime)fromDate) >= 0 &&
DateTime.Compare(p.TradeDate, (DateTime)toDate) <= 0).ToListAsync();
var totalsByCust = new SomeModel{ Username = client.Username };
foreach (var item in payment)
{
totalByCust.Bcf += item.Bcf;
totalByCust.Ecn += item.Ecn;
totalByCust.Ecbt += item.Ecbt;
totalByCust.OpenGl += item.OpenGl;
totalByCust.JeyK += item.JeyK;
}
totals.Add(totalByCust);
}
return totals;
}
【问题讨论】:
-
@rene 会发生什么?此过程将在服务器上花费相同的时间...(
-
不,不会的,试试看。
-
一个 SQL
JOIN将比向数据库服务器串行发送 1500 个查询更有效。 -
@rene 我的错,已编辑。
-
@不是。我相信的最后一次编辑:p
标签: c# asp.net sql-server entity-framework linq