add index to fix order slow issue

dev_partners
p31729568 6 years ago
parent 1864620376
commit 26455d0efb

@ -24,10 +24,8 @@ class Management::SchoolDataContrastService
reports = reports.where("school_name LIKE :keyword OR school_id LIKE :keyword", keyword: "%#{keyword}%")
end
reports = reports.group(:school_id)
count = reports.count.count
reports = reports.limit(PAGE_SIZE).offset(offset)
count = reports.count('distinct(school_id)')
reports = reports.group(:school_id).limit(PAGE_SIZE).offset(offset)
[count, reports]
end
@ -56,7 +54,7 @@ class Management::SchoolDataContrastService
end
def select_columns
"school_id, school_name,"\
"school_id, school_name, @total:=0, @other_total:=0,"\
"(@total:=SUM(IF(date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}', #{contrast_column}, 0))) total,"\
"(@other_total:=SUM(IF(date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}', #{contrast_column}, 0))) other_total,"\
"(IF(@other_total - @total = 0, 0.0, round((@other_total - @total) / IF(@total = 0, 1, @total), 5))) percentage,"\

@ -0,0 +1,9 @@
class AddIndexToSchoolDailyReports < ActiveRecord::Migration
def change
add_index :school_daily_reports, [:date, :teacher_increase_count]
add_index :school_daily_reports, [:date, :student_increase_count]
add_index :school_daily_reports, [:date, :course_increase_count]
add_index :school_daily_reports, [:date, :shixun_increase_count]
add_index :school_daily_reports, [:date, :active_user_count]
end
end
Loading…
Cancel
Save