我在Rails中有一个控制器,它使用FasterCSV生成CSV报告。这些报告将包含大约20,000行,也许更多。
当我在下面的实现中创建csv_string时,大约需要30秒或更长时间。有没有更好/更快的方法来导出数据?无需将数据全部存储在csv_string的内存中即可输出数据的任何方式
我目前的实现如下:
@report_data = Person.find(:all, :conditions => "age > #{params[:age]}")
csv_string = FasterCSV.generate do |csv|
@report_data.each do |e|
values = [e.id, e.name, e.description, e.phone, e.address]
csv << values
end
end
send_data csv_string, :type => "text/plain",
:filename=>"report.csv", :disposition => 'attachment'发布于 2010-02-25 00:36:51
我会尝试使用find_in_batches,一次消除内存中那么多的ActiveRecord对象。
http://ryandaigle.com/articles/2009/2/23/what-s-new-in-edge-rails-batched-find
我相信这应该会有相当大的帮助,在内存中创建和拥有许多ActiveRecord对象是非常糟糕的。
https://stackoverflow.com/questions/2327228
复制相似问题