优化时间统计问题

dev_tj
daiao 5 years ago
parent b29c2d8198
commit ed258312ae

@ -10,7 +10,8 @@ class Subjects::ShixunUsedInfoService < ApplicationService
stages.each do |stage| stages.each do |stage|
position = stage.position position = stage.position
shixuns = stage.shixuns.includes(myshixuns: :games, homework_commons: :course) shixuns = stage.shixuns.includes(myshixuns: :games, homework_commons: :course)
shixuns.each_with_index do |shixun, index| shixuns.find_in_batches(batch_size: 1000) do |s|
Parallel.each_with_index(s, in_processes: 2) do |shixun, index|
stage = "#{position}-#{index+1}" stage = "#{position}-#{index+1}"
name = shixun.name name = shixun.name
myshixuns = shixun.myshixuns myshixuns = shixun.myshixuns
@ -24,6 +25,8 @@ class Subjects::ShixunUsedInfoService < ApplicationService
shixun_infos << {stage: stage, name: name, challenge_count: challenge_count, course_count: course_count, shixun_infos << {stage: stage, name: name, challenge_count: challenge_count, course_count: course_count,
school_count: school_count, used_count: used_count, passed_count: passed_count, school_count: school_count, used_count: used_count, passed_count: passed_count,
evaluate_count: evaluate_count, passed_ave_time: passed_ave_time, shixun_id: shixun.id} evaluate_count: evaluate_count, passed_ave_time: passed_ave_time, shixun_id: shixun.id}
end
end end
end end
shixun_infos shixun_infos

@ -9,7 +9,8 @@ namespace :subjects do
buffer_size = 0 buffer_size = 0
column_value = "subject_id, study_count, course_study_count, initiative_study, passed_count, course_used_count, " + column_value = "subject_id, study_count, course_study_count, initiative_study, passed_count, course_used_count, " +
"school_used_count, created_at, updated_at" "school_used_count, created_at, updated_at"
subjects.find_each do |subject| subjects.find_in_batches(batch_size: 50) do |s|
Parallel.each(s, in_processes: 4) do |subject|
puts("---------------------data_statistic: #{subject.id}") puts("---------------------data_statistic: #{subject.id}")
Rails.logger.info("---------------------data_statistic: #{subject.id}") Rails.logger.info("---------------------data_statistic: #{subject.id}")
data = Subjects::DataStatisticService.new(subject) data = Subjects::DataStatisticService.new(subject)
@ -30,6 +31,7 @@ namespace :subjects do
buffer_size = 0 buffer_size = 0
end end
end end
end
if buffer_size > 0 if buffer_size > 0
sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str}" sql = "REPLACE INTO subject_records(#{column_value}) VALUES #{str}"
puts sql puts sql
@ -47,7 +49,9 @@ namespace :subjects do
buffer_size = 0 buffer_size = 0
column_value = "subject_id, school_id, school_name, course_count, student_count, choice_shixun_num, " + column_value = "subject_id, school_id, school_name, course_count, student_count, choice_shixun_num, " +
"choice_shixun_frequency, created_at, updated_at" "choice_shixun_frequency, created_at, updated_at"
subjects.find_each do |subject|
subjects.find_in_batches(batch_size: 50) do |s|
Parallel.each(s, in_processes: 4) do |subject|
puts("---------------------course_info_statistic: #{subject.id}") puts("---------------------course_info_statistic: #{subject.id}")
Rails.logger.info("---------------------course_info_statistic: #{subject.id}") Rails.logger.info("---------------------course_info_statistic: #{subject.id}")
data = Subjects::CourseUsedInfoService.call(subject) data = Subjects::CourseUsedInfoService.call(subject)
@ -67,6 +71,7 @@ namespace :subjects do
end end
end end
end end
end
if buffer_size > 0 if buffer_size > 0
sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str}" sql = "REPLACE INTO subject_course_records(#{column_value}) VALUES #{str}"
puts sql puts sql
@ -84,7 +89,8 @@ namespace :subjects do
buffer_size = 0 buffer_size = 0
column_value = "subject_id, shixun_id, stage, shixun_name, challenge_count, course_count, " + column_value = "subject_id, shixun_id, stage, shixun_name, challenge_count, course_count, " +
"school_count, used_count, passed_count, evaluate_count, passed_ave_time, created_at, updated_at" "school_count, used_count, passed_count, evaluate_count, passed_ave_time, created_at, updated_at"
subjects.find_each(batch_size: 100) do |subject| subjects.find_each(batch_size: 50) do |s|
Parallel.each(s, in_processes: 4) do |subject|
puts("---------------------shixun_info_statistic: #{subject.id}") puts("---------------------shixun_info_statistic: #{subject.id}")
Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}") Rails.logger.info("---------------------shixun_info_statistic: #{subject.id}")
data = Subjects::ShixunUsedInfoService.call(subject) data = Subjects::ShixunUsedInfoService.call(subject)
@ -105,6 +111,7 @@ namespace :subjects do
end end
end end
end end
end
if buffer_size > 0 if buffer_size > 0
sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}" sql = "REPLACE INTO subject_shixun_infos(#{column_value}) VALUES #{str}"
puts sql puts sql

Loading…
Cancel
Save