Fix N+1 queries on new case contact view#6843
Open
cliftonmcintosh wants to merge 3 commits intorubyforgood:mainfrom
Open
Fix N+1 queries on new case contact view#6843cliftonmcintosh wants to merge 3 commits intorubyforgood:mainfrom
cliftonmcintosh wants to merge 3 commits intorubyforgood:mainfrom
Conversation
…inate N+1 queries
…1 queries in case contacts view
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.
What github issue is this PR for, if any?
Resolves #6836
What changed, and why?
Eliminated four N+1 query sets that fired on every page load of
/case_contacts/new_design.N+1 #1 —
casa_casesper row in the datatableCaseContactDatatable#dataaccessedcase_contact.casa_case&.case_numberfor every row. The existing.left_joins(:casa_case)inraw_recordsjoins the table for SQL filtering but does not populate the Ruby association in memory. Each row triggered a separateSELECT … FROM casa_cases WHERE id = ?.Fix: Added
:casa_caseto theincludeschain inCaseContactDatatable#raw_records.N+1 #2 —
followups.requested.exists?per row in the datatable:followupswas already in theincludes, so the records were loaded in memory. However,.requested.exists?always issues a new SQLSELECT 1 … WHERE status = ? LIMIT 1regardless, becauseexists?always hits the database.Fix: Replaced with
case_contact.followups.any?(&:requested?), which operates on the already-loaded in-memory collection.requested?is generated automatically byenum :status, {requested: 0, resolved: 1}on theFollowupmodel.N+1 #3 —
casa_orgsper row in the index viewThe index view calls
policy(case_contact).edit?for each row, which callssame_org?→record.casa_org.casa_orgis ahas_one :through :casa_caseonCaseContact. Even thoughcasa_casewas already preloaded, thehas_one :throughassociation target on theCaseContactobject itself was not, so each policy check fired its ownSELECT … FROM casa_orgs INNER JOIN casa_cases ….Fix: Added
:casa_orgdirectly to thepreloadchain inLoadsCaseContacts#all_case_contacts.N+1 #4 —
contact_topicsper row in the index viewThe index view calls
case_contact.contact_topics.map(&:question)for each row.contact_topicsishas_many :through :contact_topic_answers. The existing preloadcontact_topic_answers: :contact_topicloaded topics onto each answer object, but did not populate thecontact_topicsassociation target on theCaseContactitself.Fix: Added
:contact_topicsdirectly to thepreloadchain inLoadsCaseContacts#all_case_contacts.How is this tested? (please write rspec and jest tests!) 💖💪
Manual verification: Reloaded
/case_contacts/new_designwith thenew_case_contact_tableFlipper flag enabled and confirmed that none of the fourN+1 queries detectedwarnings appeared in the Rails log.Possible automated test: A query-count regression test could be added to
spec/datatables/case_contact_datatable_spec.rbto ensure the datatable never re-introduces per-row queries forcasa_caseorfollowups. The test would useActiveSupport::Notificationsto count SQL events and assert the total stays below a fixed threshold regardless of result set size.Example regression tests we could add if you want
AI Usage Disclosure
This PR was developed with the assistance of Claude Code. Claude helped diagnose the N+1 sources, identify the correct preload targets, and draft the fixes. All changes were reviewed and verified manually.