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

Performance: save some queries when pluck is not needed #3160

Merged
merged 1 commit into from Jun 14, 2019
Merged

Performance: save some queries when pluck is not needed #3160

merged 1 commit into from Jun 14, 2019

Conversation

rhymes
Copy link
Contributor

@rhymes rhymes commented Jun 14, 2019

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Documentation Update

Description

Rails pluck forces 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?

  • docs.dev.to
  • readme
  • no documentation needed

@pr-triage pr-triage bot added the PR: unreviewed bot applied label for PR's with no review label Jun 14, 2019
Copy link
Contributor Author

@rhymes rhymes left a 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)
Copy link
Contributor Author

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)
Copy link
Contributor Author

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

Copy link
Member

@maestromac maestromac left a 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!

@pr-triage pr-triage bot added PR: reviewed-approved bot applied label for PR's where reviewer approves changes and removed PR: unreviewed bot applied label for PR's with no review labels Jun 14, 2019
@maestromac maestromac merged commit c42c28a into forem:master Jun 14, 2019
@pr-triage pr-triage bot added PR: merged bot applied label for PR's that are merged and removed PR: reviewed-approved bot applied label for PR's where reviewer approves changes labels Jun 14, 2019
@rhymes rhymes deleted the rhymes/less-plucking branch June 14, 2019 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: merged bot applied label for PR's that are merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants