Fix N+1 query issues in get_linked_todos and get_linked_events functions#338
Draft
Copilot wants to merge 3 commits into
Draft
Fix N+1 query issues in get_linked_todos and get_linked_events functions#338Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Copilot stopped work on behalf of
mrrobot47 due to an error
January 12, 2026 16:20
Member
|
@copilot continue |
Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Co-authored-by: mrrobot47 <25586785+mrrobot47@users.noreply.github.com>
Author
I've completed the N+1 query fix implementation. The changes eliminate the query loops in both Key changes in commit 4355547:
All linting checks and security scans passed with 0 alerts. |
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 eliminates N+1 query performance issues in the
get_linked_todos()andget_linked_events()functions innext_crm/api/activities.py.Problem
The original implementation executed database queries inside loops:
get_linked_todos(): Queried Event details and Event Participants separately for each ToDo with a linked eventget_linked_events(): Queried Event Participants for each eventSolution Implemented
get_linked_todos()function["in", event_names]filterevents_mapandparticipants_map) for O(1) access during data applicationget_linked_events()function["in", event_names]filterparticipants_mapfor O(1) access during data applicationPerformance Impact
Validation
The implementation maintains backward compatibility while significantly improving database query efficiency.
Original prompt
This section details on the original issue you should resolve
<issue_title>N+1 Query in get_linked_todos() and get_linked_events()</issue_title>
<issue_description>
Metadata
next_crm/api/activities.py:563-593,617-622Problem Description
The
get_linked_todos()function queries Event details and Event Participants inside a loop for each ToDo that has a linked event. Similarly,get_linked_events()queries Event Participants for each event. This creates N+1 query patterns that scale with the number of todos/events.Impact Analysis
Code Location
Root Cause
Proposed Solution
Batch fetch Event and Event Participants data: