The old _CHANNEL_STATS_SELECT ran 9 correlated subqueries for each channel row. With 1266 channels that was ~11000 sub-executions per GET /channels request, causing multi-second (or timeout) delays. New approach: 2 CTEs (vinfo for counts/sums, nc for new_count) each do a single aggregated pass over all followed-channel videos, joined back to channels. Only 2 correlated LIMIT-1 subqueries remain for latest_video_id/title (fast with the new index). Also adds 4 indexes on startup (IF NOT EXISTS — safe to deploy): - videos(channel_id, published_at DESC) — latest video lookups - videos(channel_id, indexed_at) — new_count filter - user_videos(video_id, user_id) — watch/download aggregation - user_channels(user_id, status) — followed channel filter Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
4.0 KiB
4.0 KiB