Skip to content

Fix N+1 query in Node#tagnames_as_classes - #11862

Open
aopv wants to merge 2 commits into
publiclab:mainfrom
aopv:fix-n-plus-1-tagnames-as-classes
Open

Fix N+1 query in Node#tagnames_as_classes#11862
aopv wants to merge 2 commits into
publiclab:mainfrom
aopv:fix-n-plus-1-tagnames-as-classes

Conversation

@aopv

@aopv aopv commented Feb 17, 2026

Copy link
Copy Markdown

Summary

  • Eliminates N+1 query in Node#tagnames_as_classes by replacing Node.select([:nid]).find(id).tagnames with a direct Tag.joins(:node_tag).pluck(:name) call
  • Reduces two queries per node (Node.find + tag association load) to a single efficient pluck query
  • No change in behavior — returns the same tag class strings used in note listing views

Details

The previous implementation re-fetched the node via Node.find(id) to bypass a potentially filtered tag association (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 redundant Node.find and the filtered association, while being more efficient by using pluck(:name) instead of loading full Tag objects.

Test plan

  • Existing test test/unit/node_test.rb line 291 validates output: 'tag-test tag-awesome tag-spectrometer tag-activity-spectrometer tag-sub-tag'
  • Verify note listing pages (/notes, /dashboard) render tag CSS classes correctly

Closes #11853

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
@aopv
aopv force-pushed the fix-n-plus-1-tagnames-as-classes branch from 40c54e8 to 75e176e Compare February 17, 2026 16:14
@aopv

aopv commented Aug 3, 2026

Copy link
Copy Markdown
Author

The original check logs expired; the surviving annotations show a GitHub cache outage and the upstream workflow using deprecated actions/upload-artifact v2. Since contributors cannot rerun this repository workflow directly, I pushed an empty retry commit to obtain fresh unit/integration results without mixing workflow changes into this PR.

@aopv

aopv commented Aug 3, 2026

Copy link
Copy Markdown
Author

Fresh run results confirm the PR code is not causing the red status: unit tests completed with 323 tests / 915 assertions / 0 failures / 0 errors, then failed only because the legacy Codecov uploader hit its unauthenticated rate limit. Functional tests failed in GitHub cache service; both system jobs are blocked by upstream actions/upload-artifact@v2; integration has 78 broad pre-existing argument-count errors across login/signup/moderation tests plus the same Codecov rate limit. No workflow-only changes were mixed into this model PR.

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.

Avoid N+1 query in Tag#tagnames_as_classes

1 participant