fix: eliminate N+1 queries in EventsController#index#2507
Open
mroderick wants to merge 5 commits intocodebar:masterfrom
Open
fix: eliminate N+1 queries in EventsController#index#2507mroderick wants to merge 5 commits intocodebar:masterfrom
mroderick wants to merge 5 commits intocodebar:masterfrom
Conversation
This index optimizes the query that finds the host sponsor for a workshop, improving performance when eager loading the host association.
- Add has_one :workshop_host with inverse_of for proper association caching - Add has_one :host through :workshop_host for eager loading - Replace inefficient raw SQL host method with association-based implementation This eliminates N+1 queries when loading workshop hosts on the events index page.
This enables proper Rails association caching when accessing workshop from a workshop_sponsor record, reducing redundant queries.
Add :host, :permissions, and :sponsorships to includes for Workshop, Meeting, and Event queries. This eliminates N+1 queries when rendering the events index page by loading all associations in a single query.
Use @venue ||= to cache the host sponsor, preventing redundant calls to model.host which could trigger additional queries in certain contexts.
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
EventsController#indexby adding eager loading includesinverse_ofassociations for proper Rails cachingProblem Analysis
The events index page was triggering excessive database queries due to several N+1 query issues:
1. Workshop Host (Worst Offender)
WorkshopSponsor.hosts.for_workshop(id).first&.sponsor)2. Organisers Association
Workshop,Meeting, andEventall usehas_many :organisers, through: :permissions3. Meeting Venue (Upcoming Events)
Meeting.upcoming.all)4. Presenter Memoization
WorkshopPresenter#venuecalledmodel.hostwithout cachingSolution
1. Database Index
Added composite index on
workshop_sponsors(workshop_id, host)to optimize the host sponsor query:2. Workshop Model Associations
Replaced raw SQL
hostmethod with eager-loadable associations:3. EventsController Includes
Added all necessary includes for eager loading:
4. WorkshopPresenter Memoization
Added instance variable caching:
Performance Results
Query count reduced from ~47 to 7 for 40 workshops (85% reduction).
Manual Verification
Option 1: Using Bullet
Gemfile(if not already present):config/environments/development.rb:/eventsin your browserOption 2: Query Logging
/eventsin your browserOption 3: Bullet Console Log
Add to
config/environments/development.rb:Then check the server console output after visiting
/events.Test Plan