Skip to content

Conversation

@NuclearTacos
Copy link
Contributor

@NuclearTacos NuclearTacos commented Jan 23, 2026

Refactored attendance occurrence retrieval logic to improve performance by fetching attendance occurrences separately and matching them in memory.

Proposed Changes

The GetLocationsAndSchedules method in the Group Scheduler block was using a correlated subquery pattern to fetch AttendanceOccurrences within the main LINQ projection. This caused Entity Framework 6 to execute a separate database query for each group/location/schedule combination (N+1 query problem), resulting in poor performance when viewing schedules for multiple groups across several weeks.

This PR refactors the data retrieval to use a "fetch then stitch" pattern:

  1. First query: Fetch all GroupLocationSchedule data, projecting to an anonymous type first to avoid EF6 materialization issues with class constructors, then converting to GroupLocationSchedule objects in memory with empty AttendanceOccurrences lists.

  2. Second query: Fetch all relevant AttendanceOccurrence records in a single query using IN clauses based on the distinct group, location, and schedule IDs from the first query.

  3. In-memory matching: Iterate through the GroupLocationSchedule objects and assign the appropriate AttendanceOccurrences from the second query result.

This reduces database round-trips from N+1 (potentially hundreds) down to just 2, significantly improving page load times for the Group Scheduler block.

Fixes: #6662

Types of changes

What types of changes does your code introduce to Rock?
Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality, which has been approved by the core team)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • This is a single-commit PR. (If not, please squash your commit and re-submit it.)
  • I verified my PR does not include whitespace/formatting changes -- because if it does it will be closed without merging.
  • I have read the Contributing to Rock doc
  • By contributing code, I agree to license my contribution under the Rock Community License Agreement
  • Unit tests pass locally with my changes
  • I have added any required unit tests or integration tests that prove my fix is effective or that my feature works
  • I have included updated language for the Rock Documentation (if appropriate)

Further comments

This is a performance optimization with no functional changes to the Group Scheduler behavior. The same data is retrieved and processed; only the retrieval pattern has changed to reduce database load.

Documentation

No documentation changes required. This is an internal performance optimization with no UI or behavioral changes.

Migrations

If your pull request requires a migration, please exclude the migration from the Rock.Migration project, but submit it with your pull request. Please add a note to your pull request that provides a heads up that a migration file is present.

No migrations required.

Refactored attendance occurrence retrieval logic to improve performance by fetching attendance occurrences separately and matching them in memory.
@NuclearTacos
Copy link
Contributor Author

Of note, this fix is currently live in our environments and saved us from the repeated issue that is referenced in this merge, and our SQL wait times have been significantly improved.

(don't worry about the new job running at the end of the day)
image

@NuclearTacos
Copy link
Contributor Author

A solution was made for the issue in the this commit. 49530db . I'm going to try to validate that fully resolves our issue, and may come back to close this PR or update it with a next step :)

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.

Performance Issues with Volunteer Scheduling Board

1 participant