Draft
Conversation
- sync.py: Fetch refresh_token and linked_user fields directly, eliminating N get_doc() calls - daily.py: Filter accounts by refresh_token to reduce unnecessary document loads Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize get_doc() usage in sync and daily tasks
Eliminate N+1 queries in scheduled tasks
Jan 13, 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.
Both
sync_emails()andenable_pubsub_everyday()fetched account names withget_all()then immediately loaded full documents withget_doc()in a loop. Withsync_emails()running every 30 minutes, this creates unnecessary database load.Changes
sync.py - Eliminated
get_doc()entirelydaily.py - Filter at query time instead of in Python
Note:
daily.pystill requiresget_doc()sinceenable_pubsub()accesses thelabelschild table, but the filter reduces unnecessary iterations.Original prompt
This section details on the original issue you should resolve
<issue_title>get_doc() in Loop for Sync and Daily Tasks</issue_title>
<issue_description>
Metadata
frappe_gmail_thread/tasks/sync.py:12frappe_gmail_thread/tasks/daily.py:20Problem Description
Both scheduled tasks (
sync_emailsandenable_pubsub_everyday) fetch Gmail Account names withget_all(), then immediately load the full document withget_doc()in a loop. This creates unnecessary database queries when only specific fields are needed.The
sync_emailstask runs every 30 minutes, andenable_pubsub_everydayruns daily. With many Gmail accounts, these N+1 query patterns add up.Code Location
sync.py (line 12):
daily.py (line 20):
Root Cause
The pattern of:
This is inefficient because:
nameget_doc()requires a separate database round-tripProposed Solution
For sync.py - Only need
refresh_tokenandlinked_user:For daily.py -
enable_pubsubneeds full doc, but can batch check first:If
enable_pubsub()truly needs the full document, at minimum filter accounts that have the necessary tokens:Implementation Steps
sync.py:
fieldsto include["name", "refresh_token", "linked_user"]get_doc()callget_all()daily.py:
refresh_tokento reduce iterationsenable_pubsub()can be refactored to accept specific fields, do soTest both scheduled tasks with multiple Gmail accounts
Additional Notes
sync()function called byenqueue()does need the full document, but that's inside the background jobComments on the Issue (you are @copilot in this section)
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.