Skip to content

Optimise PackManager.bulk_query_id using SQL aggregation #2171

Open
alastair wants to merge 2 commits into
masterfrom
optimise-latest-packs
Open

Optimise PackManager.bulk_query_id using SQL aggregation #2171
alastair wants to merge 2 commits into
masterfrom
optimise-latest-packs

Conversation

@alastair

@alastair alastair commented Jul 21, 2026

Copy link
Copy Markdown
Member

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:

alastair added 2 commits July 21, 2026 11:41
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant