Optimise PackManager.bulk_query_id using SQL aggregation #2171
Open
alastair wants to merge 2 commits into
Open
Optimise PackManager.bulk_query_id using SQL aggregation #2171alastair wants to merge 2 commits into
alastair wants to merge 2 commits into
Conversation
The "unpublished sounds" calculation used a cached value of p.sounds.count() total sounds which already counted only public sounds. This meant that unpublished sounds always showed 0 (total public sounds - total public sounds). Instead, compute all (sounds - public sounds), and only show the count to the pack owner.
The old implementation prefetched every public sound of every pack (with license, geotag and tags) and aggregated per-pack data in Python, so its cost scaled with the total number of sounds in the packs. Rewrite it to compute the same data with four bounded queries: 1. Pack queryset annotated with per-pack aggregates (total sound count, ratings, distinct license pairs, geotag existence) 2. Window query selecting the top-3 newest public sounds per pack 3. Grouped SoundTag query for per-pack tag counts 4. SoundSimilarityVector lookup for the selected sounds Warm render of the profile-page "latest packs" section for a user with large packs drops from ~130 ms to ~36 ms, with section HTML verified byte-identical against a production data snapshot.
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.
Issue(s)
#1387
Description
The old implementation prefetched every public sound of every pack
(with license, geotag and tags) and aggregated per-pack data in Python,
so its cost scaled with the total number of sounds in the packs.
Rewrite it to compute the same data in sql.
Runtime for a user with large packs goes down from ~130 ms to ~36 ms
Deployment steps: