这是我的表达:
var d = DateTime.Now.AddDays(-60);
return db.Orders
.Where(o => o.CreateDate > d && o.Status > 3)
.Join(db.OrderItems, o => o.OrderId, o => o.OrderId, (o, p) => new { Order = o, OrderItem = p })
.Join(db.Customers, o => o.Order.CustomerId, o => o.CustomerId, (o, p) => new { o.Order, o.OrderItem, Customer = p })
.GroupBy(p => new {p.Customer.CustomerId, p.Customer.Name})
.Select(o => new CustomerMonthSale
{
CustomerId = o.Key.CustomerId,
Name = o.Key.Name,
Price = o.Sum(p => p.OrderItem.Price * p.OrderItem.Quantity)
})
.OrderByDescending(o => o.Price)
.Take(10).ToList();
需要6秒钟。它应该提供大约100条记录,但在这个查询结束时我得到前10名。当计算订单和orderItems时,它正在访问可能几千条记录。我怀疑问题是数据库的大小,因为我有类似的表达式访问6倍大量的数据,它需要400毫秒。我觉得它选择了......但实际上我不知道。
答案 0 :(得分:0)
你的问题最终会出现:.ToList() 我建议你删除它,我建议改用PLinq(Parallel Linq),所以你的查询就像:
var q = from order in db.Orders.AsParallel()
where order.CreateDate > d && order.Status > 3
join orderitems in db.OrderItems.AsParallel() on order.OrderId equals orderitems.OrderId
join customers in db.Customers.AsParallel() on order.CustomerId equals customers.CustomerId
orderby order.Price descending
select new { Coloumn_name = value, Column_name = value };
var FinalList = q.Take(10);
// Choose the column names you want, and assign the value you want i.e. order.CreateDate
// so the select will be: select new {Create_Date = order.CreateDate, Client_Id = order.CustomerId ...etc};
答案 1 :(得分:0)
我已经解决了这个问题。查询是正确的。问题在于“where”声明。它检查了没有索引集的列“createDate”。我已设置索引,整个页面在500ms后就绪。谢谢大家的努力。