PERF: Load topic bookmarks for the user in user_post_bookmarks (#10197)
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.
This commit is contained in:
parent
d5c56a846a
commit
e0713455ca
|
@ -435,7 +435,7 @@ class TopicView
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_post_bookmarks
|
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
|
end
|
||||||
|
|
||||||
def reviewable_counts
|
def reviewable_counts
|
||||||
|
|
|
@ -334,6 +334,19 @@ describe TopicView do
|
||||||
end
|
end
|
||||||
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
|
context '.topic_user' do
|
||||||
it 'returns nil when there is no user' do
|
it 'returns nil when there is no user' do
|
||||||
expect(TopicView.new(topic.id, nil).topic_user).to be_blank
|
expect(TopicView.new(topic.id, nil).topic_user).to be_blank
|
||||||
|
|
Loading…
Reference in New Issue