2011-12-20 75 views
1

我有一個查詢,在一次運行大量的這些,Rails 3中,熱切負荷模型

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或東西到一個點的方式這仍然很快,但只需要一次拉。

+0

難道你不能只加載它們在一個查詢,然後打破那個結果集,只要你喜歡? – 2011-12-20 17:47:41

回答

1

嘗試

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

這將返回一個散列結果,其中的關鍵是殘留物模100和值是計數。

+0

真是太棒了。你能解釋小組功能如何工作多一點?像其他方式一樣使用它? – BananaNeil 2011-12-21 21:27:58

+0

Rails只是暴露了SQL組的功能。適合彙總數據,例如訂單數量,但每個客戶的總數不同 – 2011-12-21 21:33:45