Use LATERAL joins for followee reposts/favorites queries#759
Merged
rickyrombo merged 1 commit intomainfrom Apr 9, 2026
Merged
Conversation
…e scans The planner was scanning all saves/reposts for a given item ID (92k+ rows for popular playlists) then hash joining against follows. LATERAL forces nested loop with index lookups from the small my_follows CTE instead. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the followee_reposts / followee_favorites JSON subqueries used when hydrating tracks and playlists by rewriting them to use CROSS JOIN LATERAL, so Postgres drives the lookup from the (bounded) my_follows CTE and performs index point-lookups rather than scanning large reposts/saves sets for popular items.
Changes:
- Rewrites followee repost/favorite subqueries in
GetTracksandGetPlaylistsSQL to useCROSS JOIN LATERAL. - Adds
LIMIT 1per followee to prevent multiple matches from the same followee row in the lateral lookup. - Regenerates sqlc outputs to reflect the updated SQL.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| api/dbv1/queries/get_tracks.sql | Uses LATERAL joins for followee repost/favorite lookups in the tracks hydration query. |
| api/dbv1/queries/get_playlists.sql | Uses LATERAL joins for followee repost/favorite lookups in the playlists hydration query. |
| api/dbv1/get_tracks.sql.go | sqlc-regenerated Go code containing the updated GetTracks SQL text. |
| api/dbv1/get_playlists.sql.go | sqlc-regenerated Go code containing the updated GetPlaylists SQL text. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
get_playlists.sqlandget_tracks.sqlto useCROSS JOIN LATERALmy_followsCTE (~5k rows max) and do index point lookups on saves/reposts PKs, instead of scanning the full saves/reposts table for a given item ID (92k+ rows for popular playlists)Test plan
EXPLAIN ANALYZEon production data: query time dropped significantly for popular playlistssqlc generatesucceedsgo build ./...passes🤖 Generated with Claude Code