实践社区首页的'最热'tab的性能优化

dev_forum
cxt 5 years ago
parent 32ecb2ed6d
commit f8ba7399df

@ -22,17 +22,17 @@ class SubjectsController < ApplicationController
# 最热排序 # 最热排序
if reorder == "myshixun_count" if reorder == "myshixun_count"
if select if select
@subjects = Subject.find_by_sql("SELECT subjects.id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status,
subjects.shixuns_count, COUNT(myshixuns.id) AS myshixun_member_count FROM myshixuns, stage_shixuns, subjects subjects.shixuns_count, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns
WHERE myshixuns.shixun_id = stage_shixuns.shixun_id AND stage_shixuns.subject_id = subjects.id on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where
AND `subjects`.`hidden` = 0 AND `subjects`.`status` = 2 AND `subjects`.`name` like '%#{search}%' subjects`.`hidden` = 0 AND `subjects`.`status` = 2 AND `subjects`.`name` like '%#{search}%'
AND `subjects`.`repertoire_id` = #{select} GROUP BY subjects.id ORDER BY myshixun_member_count DESC") AND `subjects`.`repertoire_id` = #{select} GROUP BY subjects.id ORDER BY myshixun_member_count DESC")
else else
@subjects = Subject.find_by_sql("SELECT subjects.id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status, @subjects = Subject.find_by_sql("SELECT subjects.id, subjects.user_id, subjects.name, subjects.stages_count, subjects.repertoire_id, subjects.status,
subjects.shixuns_count, COUNT(myshixuns.id) AS myshixun_member_count FROM myshixuns, stage_shixuns, subjects subjects.shixuns_count, sum(shixuns.myshixuns_count) AS myshixun_member_count FROM subjects join stage_shixuns
WHERE myshixuns.shixun_id = stage_shixuns.shixun_id AND stage_shixuns.subject_id = subjects.id on stage_shixuns.subject_id = subjects.id join shixuns on shixuns.id = stage_shixuns.shixun_id where
AND `subjects`.`hidden` = 0 AND `subjects`.`status` = 2 AND `subjects`.`name` like '%#{search}%' `subjects`.`hidden` = 0 AND `subjects`.`status` = 2 AND `subjects`.`name` like '%#{search}%'
GROUP BY subjects.id ORDER BY myshixun_member_count DESC") GROUP BY subjects.id ORDER BY myshixun_member_count DESC")
end end
else else
# 我的路径 # 我的路径
@ -68,6 +68,8 @@ class SubjectsController < ApplicationController
@subjects = @subjects.page(page).per(limit).includes(:shixuns) @subjects = @subjects.page(page).per(limit).includes(:shixuns)
else else
@subjects = @subjects[offset, limit] @subjects = @subjects[offset, limit]
subject_ids = @subjects.pluck(:id)
@subjects = Subject.where(id: subject_ids).order("field(id,#{subject_ids.join(',')})").includes(:shixuns)
end end
end end

@ -31,7 +31,7 @@ class Subject < ApplicationRecord
# 挑战过路径的成员数 # 挑战过路径的成员数
def member_count def member_count
shixuns.sum(:myshixuns_count) shixuns.pluck(:myshixuns_count).sum
end end
def all_score def all_score

Loading…
Cancel
Save