Clarify the wording of the Rails/PluckInWhere
cop
#1307
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Clarifies the existing wording to be easier to understand and adds an additional paragraph about possible performance implications of this cop.
These implications have been mentioned before in #310 (comment), but weren't explicitly mentioned in the cop's documentation.
The linked issue mentions MySQL and I have found the same issue multiple times showing up in MySQL on Stack Overflow, but I ran into it on PostgreSQL today. The query was something along the lines of this, albeit a bit more complicated:
The
pluck
in the last line was replaced with aselect
, which caused theBaz
query to be ran as a subquery instead of eagerly. This resulted in major database load, asFoo
s table in this case is around 200GB in size and the query was now using sequential scanning, rather than index scanning.Some research led us to find the related description in the PostgreSQL documentation:
This means that queries such as
SELECT * FROM foo WHERE model_id IN (subquery)
use sequential scanning to match against themodel_id
, rather than index scanning. Eager loading in this case was much faster, as it allowed an index lookup against the static list provided to the query.Note to future readers: Rather than using
column IN (subquery)
you can usecolumn = ANY(ARRAY(subquery))
, which causes the subquery to be executed first and allows for an index scan.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.