[英]Very slow LINQ with where statements
I have a LINQ that looks like this: 我有一个看起来像这样的LINQ:
var something = db.Calculations
.Where(x => x.CalculationDate.Date == justTheDate
&& x.CalculationID == externalObject.CalculationID
&& x.CalculationDate >= minDate
&& x.CalculationDate <= maxDate).GroupBy(x =>
(int)x.CalculationDate.TimeOfDay.TotalMinutes);
where justTheDate
, minDate
, maxDate
(DateTime) and externalObject.CalculationID
(string) are all variables found in the scope. 其中
justTheDate
, minDate
, maxDate
(DateTime)和externalObject.CalculationID
(字符串)都是在范围内找到的变量。
When I enumerate it (using something like something.Select(x=>x.Last()).ToList()
) it takes around 15 seconds. 当我枚举它时(使用诸如
something.Select(x=>x.Last()).ToList()
类的something.Select(x=>x.Last()).ToList()
)大约需要15秒钟。 There is a lot of data but not close to what is expected in production. 有大量数据,但与实际生产预期相差甚远。
Anyway to make this query faster? 无论如何,使这个查询更快?
You can use this code to get generated SQL query: 您可以使用以下代码来获取生成的SQL查询:
var objectQuery = something as System.Data.Objects.ObjectQuery;
string strQuery = objectQuery.ToTraceString();
after get query you can execute it on database directly. 获取查询后,您可以直接在数据库上执行它。 If it is slow again you should create an appropriate index for your where clause columns.
如果再次变慢,则应为where子句列创建适当的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.