Fix N+1 query pattern in daily reminder cron (3500→5 queries)#776
Draft
Fix N+1 query pattern in daily reminder cron (3500→5 queries)#776
Conversation
Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix N+1 query pattern in daily reminder cron
Fix N+1 query pattern in daily reminder cron (3500→5 queries)
Jan 12, 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.
Daily reminder task was executing 6-7 queries per employee, creating severe database load at scale (3500 queries for 500 employees, 30-60s execution time).
Fixes: #766
Changes
Replaced per-employee queries with batch operations:
frappe.get_all()with["in", holiday_lists]filteremployee IN (...)clausefrappe.get_all()with["in", employee_names]filtercustom_working_hours/custom_work_schedulefieldsBefore:
After:
Removed:
get_employee_daily_working_norm()- replaced withcalculate_daily_norm()using fetched fieldsis_holiday_or_leave()- logic inlined with batch-fetched datareported_time_by_employee()- logic inlined with batch-fetched dataget_leave_info()- replaced with batch SQL queryImpact
Query count: 3500→5 (99.8% reduction)
Execution time: 30-60s→1-2s (estimated)
Scales O(1) vs O(N) with employee count
Original prompt
This section details on the original issue you should resolve
<issue_title>N+1 Query Pattern in Daily Reminder Cron</issue_title>
<issue_description>
Severity: Critical
Category: Database/Scheduled Tasks
Location
next_pms/timesheet/tasks/daily_reminder_for_time_entry.pysend_reminder()Description
The daily reminder cron loops through all active employees and makes multiple database queries per employee, resulting in severe N+1 performance issues. For an organization with 500 employees, this task could generate 2000+ database queries.
Problematic Code
Nested N+1 in
is_holiday_or_leave():Nested N+1 in
reported_time_by_employee():Impact
Recommended Fix
1. Batch-fetch holiday lists for all employees: