优化min& nHibernate中的最大查询

时间:2011-12-30 11:38:18

标签: c# nhibernate query-optimization

我在nHibernate中有以下查询。这个想法是获得某个数据组的第一个和最后一个测量时间。

var measurements = _session.Query<Measurement>()
                           .Where(x => categories.Contains(x.CategoryId));
first = measurements.Min(o => o.StartTime);
last = measurements.Max(o => o.StartTime);

SQL Server Profiler提供以下输出:

exec sp_executesql N'select cast(min(measuremen0_.StartTime) as DATETIME) as col_0_0_ from Measurement measuremen0_ where measuremen0_.Category in (@p0 , @p1)',N'@p0 int,@p1 int',@p0=7654321,@p1=3324673

exec sp_executesql N'select cast(max(measuremen0_.StartTime) as DATETIME) as col_0_0_ from Measurement measuremen0_ where measuremen0_.Category in (@p0 , @p1)',N'@p0 int,@p1 int',@p0=7654321,@p1=3324673

我可以以某种方式优化此而不使用HQL ,以便它只会向数据库服务器创建一个请求吗?

1 个答案:

答案 0 :(得分:3)

你看过Future Queries了吗?我认为它也适用于linq查询。