Skip to content

Commit

Permalink
PERF: Load topic bookmarks for the user in user_post_bookmarks (#10197)
Browse files Browse the repository at this point in the history
Instead of loading all of the user bookmarks using all the post IDs in a topic, load all the bookmarks for a user using the topic ID. This eliminates a costly WHERE ID IN query.
  • Loading branch information
martin-brennan committed Jul 9, 2020
1 parent d5c56a8 commit e071345
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/topic_view.rb
Expand Up @@ -435,7 +435,7 @@ def links
end

def user_post_bookmarks
@user_post_bookmarks ||= Bookmark.where(user: @user, post_id: unfiltered_post_ids)
@user_post_bookmarks ||= @topic.bookmarks.where(user: @user)
end

def reviewable_counts
Expand Down
13 changes: 13 additions & 0 deletions spec/components/topic_view_spec.rb
Expand Up @@ -334,6 +334,19 @@
end
end

context "#user_post_bookmarks" do
let!(:user) { Fabricate(:user) }
let!(:bookmark1) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic), user: user) }
let!(:bookmark2) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic), user: user) }
let!(:bookmark3) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic)) }

it "returns all the bookmarks in the topic for a user" do
expect(TopicView.new(topic.id, user).user_post_bookmarks.pluck(:id)).to match_array(
[bookmark1.id, bookmark2.id]
)
end
end

context '.topic_user' do
it 'returns nil when there is no user' do
expect(TopicView.new(topic.id, nil).topic_user).to be_blank
Expand Down

0 comments on commit e071345

Please sign in to comment.