mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 20:36:21 -05:00
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 <noreply@anthropic.com>
This commit is contained in:
parent
d8eba53c24
commit
3668bf1ce9
4 changed files with 64 additions and 4 deletions
|
|
@ -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)')
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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'),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue