perf: reduce memory consumption across services#1047
Open
nledez wants to merge 2 commits intocgwire:mainfrom
Open
perf: reduce memory consumption across services#1047nledez wants to merge 2 commits intocgwire:mainfrom
nledez wants to merge 2 commits intocgwire:mainfrom
Conversation
| assignees = db.relationship( | ||
| "Person", secondary=TaskPersonLink.__table__, lazy="selectin" | ||
| ) | ||
| assignees = db.relationship("Person", secondary=TaskPersonLink.__table__) |
Contributor
There was a problem hiding this comment.
Please keep the selectin selection. It is more adapted to our case, where most consuming queries need it.
- Fix N+1 queries in get_comments() by batch-fetching persons - Stream preview files in backup_service with yield_per(500) - Use with_entities for preview file lookup in playlists_service - Replace manual dict init patterns with defaultdict - Flush index documents in batches instead of accumulating - Change Task.assignees from eager selectin to lazy loading, add explicit selectinload only where assignees are accessed
Use yield_per(500) and Flask streaming response to avoid loading entire query results and building the full CSV string in memory.
bb5f96b to
0340b9f
Compare
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.
Problem
Several SQLAlchemy patterns cause excessive memory usage: Task.assignees is eagerly loaded via selectin on every query (70-80% don't need it), get_comments() triggers N+1 queries fetching persons one by one, backup_service loads all preview files into memory at once, and playlists_service fetches full ORM objects when only 2 fields are needed.
Solution