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

Modify Rails/Pluck to ignore map/collect when used inside blocks to prevent potential N+1 queries #1388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

masato-bkn
Copy link
Contributor

@masato-bkn masato-bkn commented Nov 23, 2024

This PR modifies the Rails/Pluck cop to ignore map/collect when used inside blocks to prevent potential N+1 queries.

When the receiver's relation is not loaded and pluck is used inside an iteration, it can result in N+1 queries. This happens because pluck executes a database query on every iteration when the relation is not loaded.

Example

users = User.all
5.times do
  users.map { |user| user[:id] } # Only one query is executed
end
users = User.all
5.times do
  users.pluck(:id) # A query is executed on every iteration
end
users = User.all
users.load

5.times do
  users.pluck(:id) # Since the records are loaded, only one query is executed
end

As shown above, replacing map/collect with pluck in such cases can degrade performance due to the N+1 queries. To prevent this, it is better to ignore map/collect when used inside an iteration.

Challenge

Since Ruby has numerous methods for performing iterations, it is difficult to list all of them. Therefore, this PR assumes that map/collect used inside blocks are likely part of an iteration and ignores offenses.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@masato-bkn masato-bkn force-pushed the modify_rails_pluck_to_ignore_map_when_used_inside_block branch 2 times, most recently from e92bc30 to 5cf076c Compare November 23, 2024 10:03
@masato-bkn masato-bkn marked this pull request as ready for review November 23, 2024 10:06
@masato-bkn masato-bkn force-pushed the modify_rails_pluck_to_ignore_map_when_used_inside_block branch from 5cf076c to 24c7a77 Compare November 23, 2024 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant