Fix N+1 query in Node#tagnames_as_classes#11862
Open
apoorvdarshan wants to merge 1 commit intopubliclab:mainfrom
Open
Fix N+1 query in Node#tagnames_as_classes#11862apoorvdarshan wants to merge 1 commit intopubliclab:mainfrom
apoorvdarshan wants to merge 1 commit intopubliclab:mainfrom
Conversation
Replace Node.find(id) re-query with a direct Tag.joins(:node_tag).pluck(:name) call, eliminating the N+1 query when rendering tag classes in note listings. This reduces two queries per node (Node.find + tag association load) to a single efficient pluck query. Closes publiclab#11853
40c54e8 to
75e176e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Node#tagnames_as_classesby replacingNode.select([:nid]).find(id).tagnameswith a directTag.joins(:node_tag).pluck(:name)callDetails
The previous implementation re-fetched the node via
Node.find(id)to bypass a potentially filteredtagassociation (caused by joins in the calling context). This created an N+1 problem when rendering lists of notes.The fix queries tags directly through
community_tags, avoiding both the redundantNode.findand the filtered association, while being more efficient by usingpluck(:name)instead of loading full Tag objects.Test plan
test/unit/node_test.rbline 291 validates output:'tag-test tag-awesome tag-spectrometer tag-activity-spectrometer tag-sub-tag'/notes,/dashboard) render tag CSS classes correctlyCloses #11853