使用 FasterCSV 和 Rails 导出大量数据
我在 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'
I have a controller in Rails that generates CSV reports using FasterCSV. These reports will contain approximately 20,000 rows, maybe more.
It takes about 30 seconds or more when creating the csv_string
in my implementation below. Is there a better/faster way to export the data? Any way to output the data without having to store it all in memory in the csv_string
?
My current implementation is as follows:
@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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试使用 find_in_batches 来一次消除内存中的许多 ActiveRecord 对象。
http://ryandaigle. com/articles/2009/2/23/what-s-new-in-edge-rails-batched-find
我相信这应该有很大帮助,在内存中创建和拥有许多 ActiveRecord 对象是 slooowww。
I would try using find_in_batches, to eliminate that many ActiveRecord objects in memory at once.
http://ryandaigle.com/articles/2009/2/23/what-s-new-in-edge-rails-batched-find
I believe that should help quite a bit, creating and having many ActiveRecord objects in memory is slooowww.