Draft
Fix N+1 query pattern in availability check for members#259
Conversation
Copilot stopped work on behalf of
mrrobot47 due to an error
January 12, 2026 16:14
Member
|
@copilot continue |
…ttern Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Contributor
Author
I've implemented the fix for the N+1 query pattern by adding request-level caching for member availability data. The changes are minimal and focused:
With 5 mandatory members and a 10-day availability window, this reduces database queries from 50 to just 5 (90% reduction). Latest commit: 09964ad |
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.
This PR fixes the N+1 query pattern in the
check_availability()function by implementing request-level caching for member availability data.Problem
The
check_availability()function is called for every date when calculating available appointment slots. Inside this function, it loads theUser Appointment Availabilitydocument for each mandatory member in a loop, resulting in redundant database queries.With 5 mandatory members and a 10-day availability window, a single time slot request results in:
get_doc()calls just for this one functionSolution Implemented
Implemented Option 2 from the proposed solutions: request-level caching using
frappe.local.Changes Made
Added
get_member_availability_cached()helper functionfrappe.local.appointment_cachedaysandtime_slotsfor potential reuse in other functionsUpdated
check_availability()functionfrappe.get_doc()for each member on every date checkPerformance Impact
get_doc()callsget_doc()calls (one per unique member, cached for the request)This aligns with the estimated 50-70% overall performance gain mentioned in the issue.
Testing
The implementation is minimal and focused, affecting only the caching mechanism without altering the core logic or functionality of appointment slot calculations.
Original prompt
This section details on the original issue you should resolve
<issue_title>N+1 Query Pattern in Availability Check for Members</issue_title>
<issue_description>
Metadata
frappe_appointment/frappe_appointment/doctype/appointment_group/appointment_group.py:292-298Problem Description
The
check_availability()function is called for every date when calculating available appointment slots. Inside this function, it loads theUser Appointment Availabilitydocument for each mandatory member in a loop.This function is called:
With 5 mandatory members and a 10-day availability window, a single time slot request results in:
get_doc()calls just for this one functionCode Location
check_availability() - lines 292-298:
Root Cause
Proposed Solution
Option 1: Batch fetch at the start of slot calculation
Option 2: Add request-level caching
Implementation Steps
frappe.localcheck_availability()to use cached dataAdditional Notes
is_member_on_leave_or_is_holiday()(see Issue 4)_get_time_slots_for_day()function calls thi...💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.