PERF: Avoid running ignored users DB query for anon users. (#14487)

This commit is contained in:
Alan Guo Xiang Tan 2021-10-01 15:01:27 +08:00 committed by GitHub
parent c8d5c049eb
commit 0359adc0b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 7 deletions

View File

@ -725,6 +725,7 @@ class TopicView
return posts.where(post_type: Post.types[:regular]) if @only_regular return posts.where(post_type: Post.types[:regular]) if @only_regular
visible_types = Topic.visible_post_types(@user) visible_types = Topic.visible_post_types(@user)
if @user.present? if @user.present?
posts.where("posts.user_id = ? OR post_type IN (?)", @user.id, visible_types) posts.where("posts.user_id = ? OR post_type IN (?)", @user.id, visible_types)
else else
@ -791,22 +792,24 @@ class TopicView
@contains_gaps = false @contains_gaps = false
@filtered_posts = unfiltered_posts @filtered_posts = unfiltered_posts
if @user
sql = <<~SQL sql = <<~SQL
SELECT ignored_user_id SELECT ignored_user_id
FROM ignored_users as ig FROM ignored_users as ig
JOIN users as u ON u.id = ig.ignored_user_id INNER JOIN users as u ON u.id = ig.ignored_user_id
WHERE ig.user_id = :current_user_id WHERE ig.user_id = :current_user_id
AND ig.ignored_user_id <> :current_user_id AND ig.ignored_user_id <> :current_user_id
AND NOT u.admin AND NOT u.admin
AND NOT u.moderator AND NOT u.moderator
SQL SQL
ignored_user_ids = DB.query_single(sql, current_user_id: @user&.id) ignored_user_ids = DB.query_single(sql, current_user_id: @user.id)
if ignored_user_ids.present? if ignored_user_ids.present?
@filtered_posts = @filtered_posts.where.not("user_id IN (?) AND id <> ?", ignored_user_ids, first_post_id) @filtered_posts = @filtered_posts.where.not("user_id IN (?) AND posts.id <> ?", ignored_user_ids, first_post_id)
@contains_gaps = true @contains_gaps = true
end end
end
# Filters # Filters
if @filter == 'summary' if @filter == 'summary'