improve performance of periodical update job, decrease frequency and shift it by a few minutes

This commit is contained in:
Sam 2013-09-30 16:59:16 +10:00
parent 6fbc5af284
commit 5caa7a0e4d
4 changed files with 20 additions and 10 deletions

View File

@ -15,13 +15,13 @@ class CategoryFeaturedTopic < ActiveRecord::Base
def self.feature_topics_for(c)
return if c.blank?
query = TopicQuery.new(self.fake_admin, per_page: SiteSetting.category_featured_topics, except_topic_id: c.topic_id, visible: true)
results = query.list_category(c).topic_ids.to_a
CategoryFeaturedTopic.transaction do
CategoryFeaturedTopic.delete_all(category_id: c.id)
query = TopicQuery.new(self.fake_admin, per_page: SiteSetting.category_featured_topics, except_topic_id: c.topic_id, visible: true)
results = query.list_category(c)
if results.present?
results.topic_ids.each_with_index do |topic_id, idx|
if results
results.each_with_index do |topic_id, idx|
c.category_featured_topics.create(topic_id: topic_id, rank: idx)
end
end

View File

@ -311,7 +311,7 @@ class Topic < ActiveRecord::Base
FROM posts
WHERE avg_time > 0 AND avg_time IS NOT NULL
GROUP BY topic_id) AS x
WHERE x.topic_id = topics.id")
WHERE x.topic_id = topics.id AND (topics.avg_time <> x.gmean OR topics.avg_time IS NULL)")
end
def changed_to_category(cat)

View File

@ -5,7 +5,7 @@ module Jobs
# This job will run on a regular basis to update statistics and denormalized data.
# If it does not run, the site will not function properly.
class PeriodicalUpdates < Jobs::Scheduled
recurrence { hourly.minute_of_hour(0, 10, 20, 30, 40, 50) }
recurrence { hourly.minute_of_hour(3, 18, 33, 48) }
def execute(args)

View File

@ -27,7 +27,8 @@ class ScoreCalculator
FROM (SELECT id, percent_rank()
OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank
FROM posts) AS x
WHERE x.id = posts.id")
WHERE x.id = posts.id AND
(posts.percent_rank IS NULL OR x.percent_rank <> posts.percent_rank)")
# Update the topics
@ -41,7 +42,16 @@ class ScoreCalculator
AVG(p.score) AS avg_score
FROM posts AS p
GROUP BY p.topic_id) AS x
WHERE x.topic_id = t.id",
WHERE x.topic_id = t.id AND
(
(t.score <> x.avg_score OR t.score IS NULL) OR
(t.has_best_of IS NULL OR t.has_best_of <> (
t.like_count >= :likes_required AND
t.posts_count >= :posts_required AND
x.max_score >= :score_required
))
)
",
likes_required: SiteSetting.best_of_likes_required,
posts_required: SiteSetting.best_of_posts_required,
score_required: SiteSetting.best_of_score_threshold
@ -51,7 +61,7 @@ class ScoreCalculator
FROM (SELECT id, percent_rank()
OVER (ORDER BY SCORE DESC) as percent_rank
FROM topics) AS x
WHERE x.id = topics.id")
WHERE x.id = topics.id AND (topics.percent_rank <> x.percent_rank OR topics.percent_rank IS NULL)")
end