Fix N+1 query pattern in search title field fetching#7
Draft
Conversation
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 title field fetching
Fix N+1 query pattern in search title field fetching
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.
The
search()function fetched title fields individually for each result usingfrappe.db.get_value()in a loop, generating N database queries for N results with title fields. For a typical search returning 20 results across 3 doctypes, this resulted in 20 queries instead of 3.Changes
frappe.get_all()with IN filter instead of individualget_value()calls{doctype: {name: title}}for O(1) assignment during sortingImpact
Query count reduced from O(N) to O(M) where M is the number of unique doctypes with title fields. Typical improvement: 20 queries → 3 queries (85% reduction).
Original prompt
This section details on the original issue you should resolve
<issue_title>N+1 Query Pattern in Title Field Fetching</issue_title>
<issue_description>
Metadata
frappe_search/api/search.py:363-376Problem Description
The
search()function fetches the title field value for each search result individually usingfrappe.db.get_value()inside a nested loop. With 20 results across multiple doctypes, this generates 20+ additional queries per search.Impact Analysis
Code Location
Root Cause
The loop structure iterates through
allowed_doctypesand then through allresults, fetching the title field value individually for each matching result. Whilefrappe.get_meta()is cached,frappe.db.get_value()is not and executes a query per call.Proposed Solution
Batch fetch title fields by grouping results per doctype and using a single query per doctype:
Implementation Steps
{doctype: {name: title}}Testing Recommendations
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.