parent
fb978471d4
commit
f0e4dbf453
@ -0,0 +1,24 @@
|
||||
#coding=utf-8
|
||||
# 通过配置服务器的crontab每天定时更新实践项目的star数量
|
||||
namespace :daily_update_star_for_shixun do
|
||||
desc "update star count every day for shixun "
|
||||
task :execute => :environment do
|
||||
shixuns = Shixun.select(:id, :averge_star)
|
||||
|
||||
shixuns.find_in_batches(batch_size: 10000) do |group|
|
||||
update_sql = "UPDATE shixuns SET averge_star = CASE id "
|
||||
Parallel.each(group, in_threads: 6) do |shixun|
|
||||
averge_star = Game.find_by_sql("select ifnull(sum(g.star),0)/ifnull(count(*),1) as averge_star from (games g left join
|
||||
(myshixuns m join shixuns s on s.id = m.shixun_id) on m.id = g.myshixun_id)
|
||||
where star != 0 and s.id = #{shixun.id}").first.try(:averge_star)
|
||||
averge_star = averge_star || 5
|
||||
|
||||
update_sql += " WHEN #{shixun.id} THEN #{averge_star.round(1)&.to_i} "
|
||||
end
|
||||
|
||||
update_sql += " ELSE averge_star END WHERE id IN (#{group.map(&:id).join(',')})"
|
||||
Rails.logger.info "########## #{Time.now} 更新实践项目averge_starSQL语句: #{update_sql}"
|
||||
ActiveRecord::Base.connection.execute(update_sql) if group.present?
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
#coding=utf-8
|
||||
# 通过配置服务器的crontab每天定时更新实践项目的myshixuns数量
|
||||
namespace :update_myshixuns_count_for_shixun do
|
||||
desc "update myshixuns count for shixun every day"
|
||||
task :execute => :environment do
|
||||
shixuns = Shixun.joins(:myshixuns).select("shixuns.id, COUNT(myshixuns.id) AS query_myshixuns_count")
|
||||
.group("shixuns.id")
|
||||
|
||||
shixuns.find_in_batches(batch_size: 10000) do |group|
|
||||
update_sql = "UPDATE shixuns SET myshixuns_count = CASE id "
|
||||
Parallel.each(group, in_threads: 6) do |shixun|
|
||||
update_sql += " WHEN #{shixun.id} THEN #{shixun.query_myshixuns_count&.to_i} "
|
||||
end
|
||||
|
||||
update_sql += " ELSE myshixuns_count END WHERE id IN (#{group.map(&:id).join(',')})"
|
||||
Rails.logger.info "########## #{Time.now} 更新实践项目myshixuns_count的SQL语句: #{update_sql}"
|
||||
ActiveRecord::Base.connection.execute(update_sql) if group.present?
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue