model.mTable = m_ctx.Shop
.Where(t => t.CustomerID== model.mFilter.CustomerID)
.Select(f => new GroupedData
{
iGroupID = f.iCustomerID,
dtGroupDt = new DateTime(f.dtDate.Year, f.dtDate.Month, f.dtDate.Day, f.dtStartTime.Hours, f.dtStartTime.Minutes, f.dtStartTime.Seconds, f.dtStartTime.Milliseconds),
})
.OrderByDescending(f => f.dtGroupDt)
.Take(2)
.ToList();
Shop是表,而CustomerID是表的主键,但是如果有很多数据,那么此查询将给我超时异常。有什么方法可以优化此查询?
这里有3件事对您有帮助:
1)联合dtDate
并dtStartTime
成一列,如果可能的话
2)创建的索引dtDate
(见@TimothyStepansky评论),然后
3)重新排序,你如何执行查询(未经测试,语法可能不完全正确):
model.mTable = m_ctx.Shop
.Where(t => t.CustomerID== model.mFilter.CustomerID)
.OrderByDescending(t => t.dtDate)
.ThenBy(t => t.dtStartTime)
.Select(f => new GroupedData
{
iGroupID = f.iCustomerID,
dtGroupDt = new DateTime(f.dtDate.Year, f.dtDate.Month, f.dtDate.Day, f.dtStartTime.Hours, f.dtStartTime.Minutes, f.dtStartTime.Seconds, f.dtStartTime.Milliseconds),
})
.Take(2)
.ToList();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句