我有从表中接受订单的功能,但不是全部。它跳过(Page * Rows on page)并取(页面上的行)。但是存在很大问题(查看Linq2SqlProfiler)。
如果我打开15页(Skip = 700,Take = 50)我已经执行了 时间: 1700ms !
public IEnumerable<Order> GetAllConfirmedOrders(DateTime firstDay, int? ProviderId = null, Guid? ManagerId = null, DateTime? date1 = null, DateTime? date2 = null, int iSkip = 0, int iTake = 50)
{
var predicate_order = PredicateBuilder.True<Order>();
var predicate_orderlist = PredicateBuilder.True<OrderList>();
if (ProviderId != null) predicate_orderlist = predicate_orderlist.And<OrderList>(ol => ol.ProviderAn == ProviderId);
if (ManagerId != null) predicate_order = predicate_order.And<Order>(o => o.UserId == ManagerId);
if (date1 != null && date2 != null) predicate_order = predicate_order.And<Order>(o => o.DateAdd >= date1 && o.DateAdd <= date2);
else predicate_order = predicate_order.And<Order>(o => o.DateAdd >= firstDay);
var orders = (from o in _dataContext.Orders
join ol in _dataContext.OrderLists.Where(predicate_orderlist) on o.Analit equals ol.OrderAn
where o.Status == 1
orderby o.DateAdd descending
select o).Where(predicate_order).Skip(iSkip).Take(iTake);
return orders;
}
生成了sql条件:
WHERE [t2].[ROW_NUMBER] BETWEEN 50 /* @p2 */ + 1 AND 50 /* @p2 */ + 50 /* @p3 */
ORDER BY [t2].[ROW_NUMBER]
WHERE [t2].[ROW_NUMBER] BETWEEN 450 /* @p2 */ + 1 AND 450 /* @p2 */ + 50 /* @p3 */
ORDER BY [t2].[ROW_NUMBER]
WHERE [t2].[ROW_NUMBER] BETWEEN 700 /* @p2 */ + 1 AND 700 /* @p2 */ + 50 /* @p3 */
ORDER BY [t2].[ROW_NUMBER]
真的很正常吗?有没有办法将执行时间保存在100-200ms附近?
答案 0 :(得分:3)
在查看您提供的示例执行时间之后,当您打开更多数字时,看起来时间线性增加(150 - > 200 - > ... - >> 1005 - > 1700)页面。所以当你到达page15时,查询必须处理更多的记录,以获取对应于第15页的数据。我的猜测不是第15页的问题,但总的来说查询本身需要时间作为数量记录增加。您可以尝试通过调整表的索引来优化查询。