Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PERF: Use different column for better query plan #14748

Merged
merged 1 commit into from Oct 28, 2021
Merged

Conversation

udan11
Copy link
Contributor

@udan11 udan11 commented Oct 27, 2021

Using topics.id provides a better query plan than posts.topic_id which
speeds up search by almost 50%.

Using topics.id provides a better query plan than posts.topic_id which
speeds up search by almost 50%.
@@ -86,8 +86,8 @@ class Post < ActiveRecord::Base

scope :private_posts_for_user, ->(user) do
where(
"posts.topic_id IN (#{Topic::PRIVATE_MESSAGES_SQL_USER})
OR posts.topic_id IN (#{Topic::PRIVATE_MESSAGES_SQL_GROUP})",
"topics.id IN (#{Topic::PRIVATE_MESSAGES_SQL_USER})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a little off because this change assumes that we’ve already joined on the topics table before applying the scope. If I call Post.private_posts_for_user, I think this will blow up?

Copy link
Contributor Author

@udan11 udan11 Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will. At this moment is only used in search.rb anyway, where that is already the case. I think I can add a joins(:topic) to fix that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm maybe we should leave a comment here so that people know this is currently used for search only and that is why we join on the topics table instead of just using the topic_id column

@udan11 udan11 merged commit ba3078e into main Oct 28, 2021
@udan11 udan11 deleted the perf/search_pms branch October 28, 2021 08:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants