Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Rails 3,急切地加载模型_Mysql_Ruby On Rails_Database_Performance_Eager Loading - Fatal编程技术网

Mysql Rails 3,急切地加载模型

Mysql Rails 3,急切地加载模型,mysql,ruby-on-rails,database,performance,eager-loading,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 我想知道是否有一种方法可以使用“急切加载”或类似的方法,使其仍

我有一个查询正在同时运行很多这样的查询

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
我想知道是否有一种方法可以使用“急切加载”或类似的方法,使其仍然能够快速工作,但只需要一次拉动。

试试看

Account.group('id % 100').count
这将返回一个散列,其中键是剩余模100,值是计数

试试看

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

这将返回一个散列,其中键是剩余模100,值是计数

你不能在一个查询中加载所有结果,然后按你喜欢的方式破坏结果集吗?你不能在一个查询中加载所有结果,然后按你喜欢的方式破坏结果集吗?这太棒了。你能再解释一下组函数是如何工作的吗?与其他使用方法一样,Rails只是公开了SQL组函数。非常适合聚合数据,例如订单数量,但每个客户都有单独的合计,这太棒了。你能再解释一下组函数是如何工作的吗?与其他使用方法一样,Rails只是公开了SQL组函数。适用于聚合数据,例如订单数量,但每个客户有单独的总计