[Perf] Cap my_follows CTE at 200 in GetTracks/GetPlaylists#793
Merged
raymondjacobson merged 1 commit intomainfrom May 8, 2026
Merged
[Perf] Cap my_follows CTE at 200 in GetTracks/GetPlaylists#793raymondjacobson merged 1 commit intomainfrom
raymondjacobson merged 1 commit intomainfrom
Conversation
The CTE feeds two consumers (followee_reposts, followee_favorites) that each emit at most 3 rows ordered by follower_count DESC. The existing LIMIT 5000 was non-binding for nearly every user and materialized far more rows than the LATERAL ever needs. Verified on the prod read replica with user 20 (1752 follows) and a 10-track id list, three warm runs each (PR 1 MATERIALIZED applied): LIMIT 5000: 86, 97, 92 ms (mean 92) LIMIT 200: 43, 31, 23 ms (mean 32) ~2.9x Edge case: a user whose only follower-of-X is ranked >200 by their own follower_count will no longer surface in followee_*. Acceptable trade-off: those low-fanout reposts are dominated by the top-200 in the social-proof rendering anyway.
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.
Summary
Lower
LIMIT 5000toLIMIT 200in themy_followsCTE used byGetTracksandGetPlaylists. The CTE feeds two consumers (followee_reposts,followee_favorites) that each emit at most 3 rows ordered byfollower_count DESC, so 5000 was almost always materializing more than the LATERAL ever consumed.Impact
Verified on the prod read replica with user 20 (1752 follows), 10-track id list, three warm runs each (with #792 /
MATERIALIZEDapplied):Stacks with #792.
GetTracksandGetPlayliststogether represent ~310M calls / 4.5B ms inpg_stat_statements.Risk
follower_countwill no longer surface infollowee_reposts/followee_favorites. Acceptable trade-off — those low-fanout reposts are already dominated by the top-200 in the rendered top-3 social proof.TestGetTrackPersonalization(added in [Perf] MATERIALIZE my_follows CTE in GetTracks/GetPlaylists #792) and the rest of the suite cover the personalization shape.Test plan
go test -count=1 ./api/...(full suite, all green)/v1/tracks/trending?user_id=Wem1e(Phuture, 1752 follows) — 400-600ms warmStacks on
🤖 Generated with Claude Code