perf: restore UNION ALL pagination for /events/past#2682
Merged
Conversation
…memory sort Extract fetch_upcoming_events and fetch_past_events into two new methods: - paginated_events(upcoming:) — builds a UNION ALL across workshops, meetings, and events with LIMIT/OFFSET at the database level - load_events(rows) — fetches only the 20 visible rows with full eager loading, preserving UNION sort order Previously, all past workshops (~1100) were loaded into AR objects and sorted/paginated in Ruby. Now only the current page's 20 rows ever leave the database. Benchmark (first load / cached): Past events page: 11.3s → 1.3s DB queries count: ~119 → 118 (same — view N+1s unchanged) Allocations: 25M → 2.9M (cached)
This was referenced Jul 2, 2026
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.
Problem
The
/events/pastpage times out on Heroku (503) because it loads all past workshops (~2,500), events (~250), and meetings (~50) into ActiveRecord objects, sorts them in Ruby, then paginates. This is the same issue described in the original #2666.What happened to the original fix
PR #2666 built a database-level UNION ALL + LIMIT/OFFSET pagination approach, which reduced the page from 11.3s to 1.3s. However, it was accidentally lost during branch cleanup — it was merged into
bullet-n-plus-one-fixesvia #2666, but then that branch was force-pushed before merging tomaster, dropping the UNION ALL commit. Thebullet-n-plus-one-fixesbranch that eventually merged only contained the smaller organisers eager-load fix.This fix
Cherry-picks the original UNION ALL commit (
f775e3bd) from the survivingarel-union-paginationbranch, then applies the production fix that was learned later::organisersfromEvent.includesandMeeting.includesinload_events, because those models'organisersscope generates SQL without properly joiningpermissions, causingPG::UndefinedTableon production data (fixed in39da6926on master).:organisersonWorkshop.includes, because Workshop's complex associations forceeager_load(LEFT JOINs), which correctly includes the permissions table.:venuetoMeeting.includesand:hosttoWorkshop.includesto match the current master's eager loading pattern.How it works
paginated_events(upcoming:)— builds an Arel UNION ALL query across Workshops, Meetings, and Events with LIMIT/OFFSET at the database level. Only 20(id, event_type)pairs leave the database.load_events(rows)— fetches full ActiveRecord objects for just those 20 rows with eager loading, preserving the UNION sort order.Verification
spec/controllers/events_controller_spec.rb— 4 examples, all passingspec/features/listing_events_spec.rb— 4 examples, all passing