我正在构建一个从Rails应用程序中提取大量数据的Rails插件。它动态构建跨越多个表的查询,并且会返回大量结果。下面是拉动购买数据进行了狂欢(Rails的购物车)的内置查询的一个示例:是否可以在Rails/ActiveRecord中运行find_by_sql查询而不将所有结果加载到内存中?
select orders.user_id as from_id, variants.product_id as to_id from orders, line_items, variants where orders.user_id is not null and variants.product_id is not null and orders.id = line_items.order_id and line_items.variant_id = variants.id order by from_id;
的问题是,ActiveRecord的负载可达所有结果到内存中。有没有办法避免这种情况,而不必放弃编写特定于DB的代码? (我知道find_each的,但不允许进行排序。)
find_in_batches就像find_each,因为它不支持排序(或自定义的SQL):随着努力teency位,在每个分页SQL被包裹起来。 – scotchi 2010-10-26 05:10:06