Rails 3,Eagerly Load Model

时间:2011-12-20 17:43:01

标签: mysql ruby-on-rails database performance eager-loading

我有一个查询正在运行大量这些,

print Account.where("id%100=0").where(:has_videos=>true).count
print Account.where("id%100=1").where(:has_videos=>true).count
print Account.where("id%100=2").where(:has_videos=>true).count

我想知道是否有一种方法可以使用eager_load或类似的东西到这个仍然可以快速工作的点,但只需要一次拉动。

1 个答案:

答案 0 :(得分:1)

尝试

Account.group('id % 100').count

这将返回一个散列,其中键是以模100为单位的残差,值是计数。