ror export csv bring a serious performance problems

750 Views Asked by At
export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
..
end

format.csv {send_data(export), :type => 'text/csv;header=present',:filename => 'export.csv') }

I want to export large amounts of data.But it bring a serious performance problems and it filled my memory.Any better ways to export csv?

1

There are 1 best solutions below

1
Tim Peters On

FCSV is the same as FasterCSV, right?

2 suggestions to deal with the memory:

  1. Write the output to a temporary file, then send that. That way you don't need the whole thing in memory
  2. Stream the output directly to the user instead of building it up in memory. For example, see accepted answer to this question: Streaming CSV Download from Rails 3.2 app

If the generation take too much time to execute, then you'll have to profile the code then try to improve it... Alternatively, do the generation as a background task and allow the user to fetch it later.