PERF: improve performance for featured user calculation

This commit is contained in:
Sam Saffron 2015-04-22 23:54:54 +10:00
parent 6037a6b0c9
commit 40b22bf367
1 changed files with 4 additions and 1 deletions

View File

@ -24,6 +24,8 @@ class TopicFeaturedUsers
def self.ensure_consistency!(topic_id=nil)
filter = "#{"AND t.id = #{topic_id.to_i}" if topic_id}"
sql = <<SQL
WITH cte as (
@ -38,6 +40,7 @@ WITH cte as (
NOT p.hidden AND
p.user_id <> t.user_id AND
p.user_id <> t.last_post_user_id
#{filter}
GROUP BY t.id, p.user_id
),
@ -69,7 +72,7 @@ WHERE x.id = tt.id AND
COALESCE(featured_user2_id,-99) <> COALESCE(featured_user2,-99) OR
COALESCE(featured_user3_id,-99) <> COALESCE(featured_user3,-99) OR
COALESCE(featured_user4_id,-99) <> COALESCE(featured_user4,-99)
) #{"AND x.id = #{topic_id.to_i}" if topic_id}
)
SQL
Topic.exec_sql(sql)