SQL选择Count与哪里很慢

时间:2013-04-17 15:54:46

标签: sql performance linq count

我在程序中多次使用count()函数:

var housewith2floor = from qry in houses
where qry.floor == 2
select qry;

var counthousewith2floor = housewith2floor.count();

var housecolorwhite = from qry in house
where qry.color == "white"
select qry;

var countwhotehouse = housecolorwhite.count();

每个count方法都需要花费大量时间才能执行。该数据库有2百万行数据。我已经为地板柱和颜色列添加了非聚集索引,但计数仍然需要太长时间。有没有其他方法可以让我的计数运行得更快?

1 个答案:

答案 0 :(得分:0)

这不是需要时间的计数。在需要(称为deferred execution)之前,初始语句实际上不会执行。所以这是由

生成的查询
var housewith2floor = from qry in houses
where qry.floor == 2
select qry;

需要时间。

编辑以删除有关索引的陈述,因为我看到您已经创建了它们。

是否有任何表引用或引用“house”,他们是否使用Lazy Loading?

相关问题