mirror of
https://github.com/discourse/discourse.git
synced 2025-06-16 03:51:23 +08:00
PERF: improve performance of weekly job
This commit is contained in:
@ -32,40 +32,58 @@ class ScoreCalculator
|
|||||||
private
|
private
|
||||||
|
|
||||||
def update_posts_score(min_topic_age)
|
def update_posts_score(min_topic_age)
|
||||||
|
limit = 20000
|
||||||
|
|
||||||
components = []
|
components = []
|
||||||
@weightings.each_key { |k| components << "COALESCE(#{k}, 0) * :#{k}" }
|
@weightings.each_key { |k| components << "COALESCE(#{k}, 0) * :#{k}" }
|
||||||
components = components.join(" + ")
|
components = components.join(" + ")
|
||||||
|
|
||||||
builder = SqlBuilder.new(
|
builder = SqlBuilder.new <<SQL
|
||||||
"UPDATE posts SET score = x.score
|
UPDATE posts p
|
||||||
FROM (SELECT id, #{components} as score FROM posts) AS x
|
SET score = x.score
|
||||||
/*where*/"
|
FROM (
|
||||||
|
SELECT id, #{components} as score FROM posts
|
||||||
|
/*where*/
|
||||||
|
limit #{limit}
|
||||||
|
) AS x
|
||||||
|
WHERE x.id = p.id
|
||||||
|
SQL
|
||||||
|
|
||||||
)
|
builder.where("score IS NULL OR score <> #{components}", @weightings)
|
||||||
|
|
||||||
builder.where("x.id = posts.id
|
|
||||||
AND (posts.score IS NULL OR x.score <> posts.score)", @weightings)
|
|
||||||
|
|
||||||
filter_topics(builder, min_topic_age)
|
filter_topics(builder, min_topic_age)
|
||||||
|
|
||||||
builder.exec
|
while builder.exec.cmd_tuples == limit
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_posts_rank(min_topic_age)
|
def update_posts_rank(min_topic_age)
|
||||||
|
limit = 20000
|
||||||
|
|
||||||
builder = SqlBuilder.new("UPDATE posts SET percent_rank = x.percent_rank
|
builder = SqlBuilder.new <<SQL
|
||||||
FROM (SELECT id, percent_rank()
|
UPDATE posts
|
||||||
OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank
|
SET percent_rank = X.percent_rank
|
||||||
FROM posts) AS x
|
FROM (
|
||||||
/*where*/")
|
SELECT posts.id, Y.percent_rank
|
||||||
|
FROM posts
|
||||||
builder.where("x.id = posts.id AND
|
JOIN (
|
||||||
(posts.percent_rank IS NULL OR x.percent_rank <> posts.percent_rank)")
|
SELECT id, percent_rank()
|
||||||
|
OVER (PARTITION BY topic_id ORDER BY SCORE DESC) as percent_rank
|
||||||
|
FROM posts
|
||||||
|
) Y ON Y.id = posts.id
|
||||||
|
/*where*/
|
||||||
|
LIMIT #{limit}
|
||||||
|
) AS X
|
||||||
|
WHERE posts.id = X.id
|
||||||
|
SQL
|
||||||
|
|
||||||
|
builder.where("posts.percent_rank IS NULL OR Y.percent_rank <> posts.percent_rank")
|
||||||
|
|
||||||
filter_topics(builder, min_topic_age)
|
filter_topics(builder, min_topic_age)
|
||||||
|
|
||||||
builder.exec
|
while builder.exec.cmd_tuples == limit
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_topics_rank(min_topic_age)
|
def update_topics_rank(min_topic_age)
|
||||||
|
Reference in New Issue
Block a user