Skip to content

Performance: Severe N+1 Query Issue in Channel Subscription & Listing #19

@windoze95

Description

@windoze95

File: app/api/channels.py
Lines: ~38-42 (in list_channels) & ~132-140 (in subscribe)

Description:
Multiple endpoints in the Channels router execute database queries in a loop, resulting in an N+1 query performance issue:

  1. In list_channels(), a SELECT count() on the Video table is executed for every single channel in the database sequentially.
  2. In subscribe(), the application iterates through all existing Video records associated with the newly subscribed channel, running a SELECT UserVideoRef query inside the for video in existing_videos loop.
  3. In list_channel_videos(), a similar loop queries UserVideoRef sequentially for every video.

Impact:
For channels with thousands of videos, or an instance with hundreds of channels, these endpoints will take several seconds to resolve or potentially time out, severely degrading database and API performance.

Suggested Fix:

  • Use SQLAlchemy GROUP_BY count or a subquery to fetch video counts for all channels in a single batched query.
  • Use an IN clause (Video.id.in_([...])) to fetch all associated UserVideoRef objects up-front for the batch of videos.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions