ruby on rails - extremely slow csv exporting -
we trying generate csv file users download. however, it's extremely slow, 5 minutes 10k lines of csv.
any idea on improving? code below:
def download_data start_date, end_date = get_start_end_date report_lines = @report.report_lines.where("report_time between (?) , (?)", start_date, end_date) csv_string = csv.generate |csv| report_lines.each |report_data| csv << [report_data.time, report_data.name, report_data.value] end end respond_to |format| format.csv { send_data(csv_string, :filename => "#{time.now}.csv", :type => "text/csv") } end end
i start checking if report_time
indexed, unindexed report_time
going contribute slowness. refer active record migration
details on adding index.
second, trim down result need, i.e. instead of selecting columns, select time
, name
, , value
:
report_lines = @report.report_lines .where("report_time between (?) , (?)", start_date, end_date) .select('time, name, value')
try with:
def download_data start_date, end_date = get_start_end_date report_lines = @report.report_lines.where("report_time between (?) , (?)", start_date, end_date).select('time, name, value') csv_string = csv.generate |csv| report_lines.map { |row| csv << row } end respond_to |format| format.csv { send_data(csv_string, :filename => "#{time.now}.csv", :type => "text/csv") } end end
Comments
Post a Comment