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
Performance: save some queries when pluck is not needed #3160
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explained what happens in those cases where pluck can be avoided :D
| @@ -9,7 +9,7 @@ def index | |||
| includes(:reporter, :notes). | |||
| order("feedback_messages.created_at DESC"). | |||
| page(params[:page] || 1).per(5) | |||
| @email_messages = EmailMessage.find_for_reports(@feedback_messages.pluck(:id)) | |||
| @email_messages = EmailMessage.find_for_reports(@feedback_messages) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EmailMessage.find_for_reports uses feedback messages IDs for a query.
If we don't use pluck but pass the object themselves Rails is going to issue only one query using a subselect. something like:
SELECT * FROM email_messages WHERE feedback_message_id IN (SELECT id FROM feedback_messages) instead of two separate queries
| @@ -11,7 +11,7 @@ def index | |||
| order("hotness_score DESC").limit(100) | |||
| @articles = @articles.cached_tagged_with(params[:tag]) if params[:tag].present? | |||
|
|
|||
| @rating_votes = RatingVote.where(article: @articles.pluck(:id), user: current_user) | |||
| @rating_votes = RatingVote.where(article: @articles, user: current_user) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, Rails will transform this in a single select statement, @articles are already going to be used by the view to render the page, there's no need to extract ids pre-emptively
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know about this. This is super cool.
LGTM!
What type of PR is this? (check all applicable)
Description
Rails
pluckforces an additional SQL query, such query is not always needed when the objects are already in memory or the requested ID is the primary key ID.Added to documentation?