From 3668bf1ce9fb60f3ecfab8391720334dca66e04c Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:22:49 -0700 Subject: [PATCH] Channel list: read channels through a covering index, not their rows (#1061) A channel row stores its yt-dlp info_json (up to megabytes) before collection_id. The Channel list scanned channel and joined on collection_id, which walked every info_json; cold, that took 25 s on a large library. The query now scans collection and finds each channel through channel_collection_summary_idx, which now also covers minimum_frequency and url. Co-authored-by: Claude Opus 5.5 --- ..._12_01_1300_channel_list_covering_index.py | 26 ++++++++++++++++ modules/videos/channel/lib.py | 7 +++-- modules/videos/channel/test/test_lib.py | 30 +++++++++++++++++++ modules/videos/models.py | 5 ++-- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 alembic/versions/2026_12_01_1300_channel_list_covering_index.py diff --git a/alembic/versions/2026_12_01_1300_channel_list_covering_index.py b/alembic/versions/2026_12_01_1300_channel_list_covering_index.py new file mode 100644 index 00000000..9d86026e --- /dev/null +++ b/alembic/versions/2026_12_01_1300_channel_list_covering_index.py @@ -0,0 +1,26 @@ +"""Add minimum_frequency and url to channel_collection_summary_idx so it covers the Channel list. + +A channel row stores its yt-dlp info_json (up to megabytes). The Channel list read every channel row; from +a cold disk cache that was seconds even when only the start of each row was read. + +Revision ID: 2026_12_01_1300 +Revises: 2026_12_01_1200 +""" +from alembic import op + +# revision identifiers, used by Alembic. +revision = '2026_12_01_1300' +down_revision = '2026_12_01_1200' +branch_labels = None +depends_on = None + + +def upgrade(): + op.execute('DROP INDEX IF EXISTS channel_collection_summary_idx') + op.execute('CREATE INDEX channel_collection_summary_idx' + ' ON channel (collection_id, video_count, total_size, minimum_frequency, url)') + + +def downgrade(): + op.execute('DROP INDEX IF EXISTS channel_collection_summary_idx') + op.execute('CREATE INDEX channel_collection_summary_idx ON channel (collection_id, video_count, total_size)') diff --git a/modules/videos/channel/lib.py b/modules/videos/channel/lib.py index ca2937e4..52af3106 100644 --- a/modules/videos/channel/lib.py +++ b/modules/videos/channel/lib.py @@ -36,6 +36,9 @@ async def get_minimal_channels(session: Session) -> List[dict]: with session_curs(session) as curs: # Get all channels, even if they don't have videos. Also get the minimum frequency download because this is the # one that will consume the most resources. + # A channel row stores its info_json (up to megabytes) before collection_id, so scanning channel to join on + # collection_id reads all of it. CROSS JOIN makes SQLite scan collection and find each channel through + # channel_collection_summary_idx, which covers the channel columns read here. stmt = ''' SELECT c.id, col.name AS "name", @@ -45,8 +48,8 @@ async def get_minimal_channels(session: Session) -> List[dict]: c.video_count, c.total_size, c.minimum_frequency - FROM channel AS c - INNER JOIN collection col ON col.id = c.collection_id + FROM collection AS col + CROSS JOIN channel AS c ON c.collection_id = col.id LEFT JOIN tag t ON t.id = col.tag_id ''' curs.execute(stmt) diff --git a/modules/videos/channel/test/test_lib.py b/modules/videos/channel/test/test_lib.py index 987d22d1..a8a02f67 100644 --- a/modules/videos/channel/test/test_lib.py +++ b/modules/videos/channel/test/test_lib.py @@ -1,3 +1,4 @@ +import re from typing import List import pytest @@ -285,3 +286,32 @@ async def test_tag_channel_move_background_task( assert channel.directory == new_dir, f"Channel directory should be {new_dir}, got {channel.directory}" assert (new_dir / 'test_video.mp4').exists(), "File should have been moved to new directory" assert not test_file.exists(), "File should no longer exist in original directory" + + +@pytest.mark.asyncio +async def test_get_minimal_channels_reads_no_info_json(async_client, test_session, channel_factory, tag_factory): + """The Channel list finds each Channel through an index on collection_id that covers what it reads. + + A channel row stores its yt-dlp info_json (up to megabytes) before collection_id; scanning the channel table + to join on collection_id walked all of it, which took seconds from a cold disk cache.""" + await tag_factory('one') + channel_factory(name='b channel', tag_name='one') + channel_factory(name='A channel') + test_session.commit() + + statements = [] + connection = test_session.connection().connection.dbapi_connection + connection.set_trace_callback(lambda sql: statements.append(sql) if 'minimum_frequency' in sql else None) + try: + channels = await lib.get_minimal_channels(test_session) + finally: + connection.set_trace_callback(None) + + assert [(i['name'], i['tag_name']) for i in channels] == [('A channel', None), ('b channel', 'one')] + assert all(i['directory'] and i['url'] for i in channels) + assert all(i['video_count'] == 0 and i['total_size'] == 0 for i in channels) + + assert len(statements) == 1 + plan = [i[3] for i in connection.execute(f'EXPLAIN QUERY PLAN {statements[0]}')] + assert not any(re.match(r'SCAN c\b', i) for i in plan), plan + assert any(i.startswith('SEARCH c USING COVERING INDEX channel_collection_summary_idx') for i in plan), plan diff --git a/modules/videos/models.py b/modules/videos/models.py index 5b2acb78..93caddc5 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -613,8 +613,9 @@ class Channel(ModelHelper, Base): __tablename__ = 'channel' __table_args__ = ( Index('channel_minimum_frequency_idx', 'minimum_frequency'), - # Covers the Collection listing's summary query; channel rows are wide (info_json). - Index('channel_collection_summary_idx', 'collection_id', 'video_count', 'total_size'), + # Covers the Collection listing's summary query and the Channel list; channel rows are wide (info_json). + Index('channel_collection_summary_idx', 'collection_id', 'video_count', 'total_size', 'minimum_frequency', + 'url'), Index('channel_source_id', 'source_id'), Index('channel_total_size_idx', 'total_size'), Index('channel_video_count_idx', 'video_count'),