Optimize to avoid 502 errors caused by multiple queries
This commit is contained in:
parent
d65daa92d8
commit
e2da713248
|
|
@ -52,6 +52,15 @@ wb.add_worksheet(name: sheet_name) do |sheet|
|
|||
wrap_text_style = wb.styles.add_style({:alignment => {:horizontal => :center, :vertical => :center, :wrap_text => true}})
|
||||
types = [:time]
|
||||
styles = [date_time_style]
|
||||
|
||||
# 優化:一次性載入所有資料,避免 N+1 查詢
|
||||
signup_ids = @seminar_signups.map(&:id)
|
||||
signup_values_index = {}
|
||||
SeminarSignupValue.where(:seminar_signup_id.in => signup_ids).each do |sv|
|
||||
key = "#{sv.seminar_signup_id}_#{sv.seminar_signup_field_id}"
|
||||
signup_values_index[key] = sv
|
||||
end
|
||||
|
||||
@seminar_signups.each_with_index do |signup, i|
|
||||
row2 = []
|
||||
row2 << signup.created_at
|
||||
|
|
@ -65,12 +74,14 @@ wb.add_worksheet(name: sheet_name) do |sheet|
|
|||
row2 << "#{signup[:email]} "
|
||||
row2 << "#{signup.note} "
|
||||
seminar_signup_fields.each do |rf|
|
||||
key = "#{signup.id}_#{rf.id}"
|
||||
signup_value = signup_values_index[key]
|
||||
if rf.can_muti_lang_input?
|
||||
@site_in_use_locales.each do |l|
|
||||
row2 << (@seminar.get_attribute_value(rf,signup.id).get_value_by_locale(l) rescue '')
|
||||
row2 << (signup_value&.get_value_by_locale(l) rescue '')
|
||||
end
|
||||
else
|
||||
row2 << (@seminar.get_attribute_value(rf,signup.id).get_value_by_locale(I18n.locale) rescue '')
|
||||
row2 << (signup_value&.get_value_by_locale(I18n.locale) rescue '')
|
||||
end
|
||||
end
|
||||
if i == 0
|
||||
|
|
@ -82,4 +93,4 @@ wb.add_worksheet(name: sheet_name) do |sheet|
|
|||
sheet.add_row row2 , :types => types, :style => styles
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in New Issue