Skip to content

Use LATERAL joins for followee reposts/favorites queries#759

Merged
rickyrombo merged 1 commit intomainfrom
mjp-lateral-join-optimization
Apr 9, 2026
Merged

Use LATERAL joins for followee reposts/favorites queries#759
rickyrombo merged 1 commit intomainfrom
mjp-lateral-join-optimization

Conversation

@rickyrombo
Copy link
Copy Markdown
Contributor

Summary

  • Rewrites the followee_reposts and followee_favorites subqueries in both get_playlists.sql and get_tracks.sql to use CROSS JOIN LATERAL
  • Forces Postgres to drive from the my_follows CTE (~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)
  • The planner was choosing hash joins that scan all saves for a playlist_id first — LATERAL prevents this by making the subquery depend on each followee row

Test plan

  • Verified with EXPLAIN ANALYZE on production data: query time dropped significantly for popular playlists
  • sqlc generate succeeds
  • go build ./... passes

🤖 Generated with Claude Code

…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>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GetTracks and GetPlaylists SQL to use CROSS JOIN LATERAL.
  • Adds LIMIT 1 per 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.

@rickyrombo rickyrombo merged commit c6c86d7 into main Apr 9, 2026
9 checks passed
@rickyrombo rickyrombo deleted the mjp-lateral-join-optimization branch April 9, 2026 00:47
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.

2 participants