Fix N+1 query pattern in get_linked_gmail_threads API#47
Draft
Fix N+1 query pattern in get_linked_gmail_threads API#47
Conversation
- Replace get_attachments_data() with get_attachments_data_batch() for batch fetching - Fetch threads with specific fields instead of using get_doc() loop - Batch fetch all child emails in single query - Batch fetch all attachment URLs in single query - Replace thread.get_url() with direct URL construction - Reduce queries from 106 to ~3 for typical use case Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
- Address code review feedback to make comment more descriptive - Clarify that Single Email CT is the Gmail Thread Email child table Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix N+1 query pattern in get_linked_gmail_threads API
Fix N+1 query pattern in get_linked_gmail_threads API
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.
Fixes #43
The
get_linked_gmail_threads()API exhibits a severe N+1 query pattern. For 5 threads with 10 emails each having 2 attachments, it executes 106 database queries: 1 for thread names, 5get_doc()calls, and 100 individualget_value()calls for attachment URLs.Changes
Batch attachment fetching
get_attachments_data(email)withget_attachments_data_batch(emails)WHERE name IN (...)instead of N queriesBatch email fetching
get_doc("Gmail Thread")loop with singleget_all("Single Email CT")queryEliminate controller load
thread.get_url()with direct URL construction:f"/app/gmail-thread/{thread.name}"Before/After
Query reduction: 106 → 3 (~97% reduction)
Original prompt
This section details on the original issue you should resolve
<issue_title>N+1 Query Pattern in get_linked_gmail_threads API</issue_title>
<issue_description>
Metadata
frappe_gmail_thread/api/activity.py:28-63Problem Description
The
get_linked_gmail_threads()API endpoint is called from the timeline of any document linked to Gmail threads. It exhibits a severe N+1 query pattern:get_doc()get_attachments_data()which does aget_value()per attachmentFor a document with 5 linked threads, each containing 10 emails with 2 attachments each, this results in:
get_doc()calls (full document loads with child tables)get_value()calls for attachment URLs (5 threads × 10 emails × 2 attachments)Code Location
Main API endpoint (lines 18-82):
Attachment lookup (lines 7-15):
Root Cause
get_all()to get names thenget_doc()in a loop instead of fetching data in a single queryget_value()callsProposed Solution
Batch fetch all data upfront and process in memory: