My goal is to export votes, per model object for a given collection. In this example I use .all, but in the wild, it will be a .where that yields the large set. I have the following query that I am concerned about:

我的目标是根据给定集合的每个模型对象导出投票。在这个例子中,我使用.all,但在野外,它将是一个产生大集合的.where。我有以下关注的问题:

Model.all.each{|x|puts x.votes.count}.explain

   (0.4ms)  SELECT COUNT(*) FROM `votes` WHERE `votes`.`entry_id` = 1
0
   (0.3ms)  SELECT COUNT(*) FROM `votes` WHERE `votes`.`entry_id` = 2
0
   (0.4ms)  SELECT COUNT(*) FROM `votes` WHERE `votes`.`entry_id` = 3
1
   (0.3ms)  SELECT COUNT(*) FROM `votes` WHERE `votes`.`entry_id` = 4
0
   (0.2ms)  SELECT COUNT(*) FROM `votes` WHERE `votes`.`entry_id` = 5
0

Individually count is very quick, but I don't feel comfortable calling All, especially when I have a collection in the thousands or millions. Is there a way to count records as a batch opporation?

单独计数非常快,但我觉得打电话给All感觉不舒服,特别是当我有数千或数百万的收藏品时。有没有办法将记录计为批量操作?

1 个解决方案

#1


0

Not sure what you mean by "All", but do the same thing, only try using GROUP BY the ID and the COUNT function. Something like this:

不确定你的意思是“全部”,但做同样的事情,只尝试使用GROUP BY ID和COUNT函数。像这样的东西:

SELECT COUNT(*) FROM Votes GROUP BY entry_id

更多相关文章

  1. mysql批量结束线程
  2. mysql批量删除相同前缀的表和修改表名
  3. 使用自动化测试框架selenium,批量的进行截图
  4. 利用Dnspod api批量更新添加DNS解析【python脚本】
  5. 批量重命名文件——python实现
  6. caffe python批量抽取图像特征
  7. PXE高效能网络批量装机
  8. Linux 批量添加和删除用户
  9. 使用cobbler实现批量自动部署Linux

随机推荐

  1. C# mono android 图片上传进度条实现
  2. android 2.2+ 完全退出程序的方法
  3. android SpannableString使用详解,替代多
  4. android中Search之Creating a Searchable
  5. Android(安卓)MediaPlayer研究问题
  6. 判断手机类型
  7. Linearlayout 添加divider
  8. Android开发者网址导航
  9. SmartQ7 Android 改变屏幕 分辨率
  10. [Android病毒分析]木马病毒之DroidKongFu