mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 20:36:21 -05:00
Channel statistics: read from covering indexes, not video or file_group rows (#1059)
* Channel statistics: find a Channel's Videos in an index, not their rows Each video row carries its ffprobe_json, so the statistics query read a page per Video to get file_group_id. A Channel with thousands of Videos took seconds from a cold disk cache. An index of (channel_id, file_group_id) replaces video_channel_id_idx, and the tagged-Videos subquery reads that index instead of looking up each tagged FileGroup's video row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Channel statistics: read FileGroup sizes from a covering index After the video index, the remaining cold cost was reading each Video's file_group row for its size and length: a scattered page per Video. An index of file_group (id, size, length) holds them densely. SQLite prefers the rowid for an id lookup, so the query names the index with INDEXED BY. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Channel statistics: widen the FileGroup index to cover published dates A Video's neighbors are found by the published dates of its Channel's Videos, which reads the same scattered file_group rows. One index of file_group (id, published_datetime, size, length) serves both, so an upgrade builds one index rather than two. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2e46f3a6e3
commit
570c63ffc7
4 changed files with 79 additions and 4 deletions
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
import re
|
||||
from http import HTTPStatus
|
||||
|
||||
import pytest
|
||||
|
|
@ -144,6 +145,42 @@ async def test_channel_statistics_with_tagged_videos(async_client, test_session,
|
|||
assert statistics['video_tags'] == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_channel_statistics_read_no_rows(async_client, test_session, channel_factory, video_factory,
|
||||
tag_factory):
|
||||
"""A Channel's statistics are read from indexes, never from video or file_group rows.
|
||||
|
||||
Reading a large Channel's rows from a cold disk cache is a scattered page per Video, which took seconds."""
|
||||
channel = channel_factory()
|
||||
video = video_factory(channel_id=channel.id)
|
||||
video_factory(channel_id=channel.id)
|
||||
video.add_tag(test_session, (await tag_factory()).name)
|
||||
test_session.commit()
|
||||
|
||||
# get_statistics uses a raw cursor, so trace the sqlite3 connection itself; the traced SQL has its
|
||||
# parameters filled in.
|
||||
statements = []
|
||||
connection = test_session.connection().connection.dbapi_connection
|
||||
connection.set_trace_callback(lambda sql: statements.append(sql) if 'video_tags' in sql else None)
|
||||
try:
|
||||
statistics = channel.get_statistics()
|
||||
finally:
|
||||
connection.set_trace_callback(None)
|
||||
assert statistics['video_count'] == 2
|
||||
assert statistics['video_tags'] == 1
|
||||
|
||||
assert len(statements) == 1
|
||||
plan = [i[3] for i in connection.execute(f'EXPLAIN QUERY PLAN {statements[0]}')]
|
||||
video_steps = [i for i in plan if re.match(r'(SEARCH|SCAN) (video|v)\b', i)]
|
||||
assert video_steps, plan
|
||||
for step in video_steps:
|
||||
assert 'COVERING INDEX video_channel_file_group_idx' in step, plan
|
||||
file_group_steps = [i for i in plan if re.match(r'(SEARCH|SCAN) fg\b', i)]
|
||||
assert file_group_steps, plan
|
||||
for step in file_group_steps:
|
||||
assert 'COVERING INDEX file_group_published_size_length_idx' in step, plan
|
||||
|
||||
|
||||
def test_channel_directory_video_counts(test_session, channel_factory, video_factory):
|
||||
"""A Channel can count the video FileGroups in its directory, and which Channel they are assigned to.
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,9 @@ AUDIO_PLAYLIST_MIMETYPES = ('audio/x-mpegurl', 'audio/mpegurl', 'audio/x-scpls',
|
|||
class Video(ModelHelper, Base):
|
||||
__tablename__ = 'video'
|
||||
__table_args__ = (
|
||||
Index('video_channel_id_idx', 'channel_id'),
|
||||
# Covers a Channel's Videos: video rows are wide (ffprobe_json), so finding a Channel's FileGroups must not
|
||||
# read them.
|
||||
Index('video_channel_file_group_idx', 'channel_id', 'file_group_id'),
|
||||
Index('video_source_id_idx', 'source_id'),
|
||||
Index('video_view_count_idx', 'view_count'),
|
||||
# Do not reuse ids of deleted Videos (AUTOINCREMENT keeps a sequence table).
|
||||
|
|
@ -949,10 +951,11 @@ class Channel(ModelHelper, Base):
|
|||
-- Count Videos with at least one Tag; a Video may use multiple Tags.
|
||||
(SELECT COUNT(DISTINCT tf.file_group_id)
|
||||
FROM tag_file tf
|
||||
INNER JOIN video v ON v.file_group_id = tf.file_group_id
|
||||
WHERE v.channel_id = :id) AS "video_tags"
|
||||
WHERE tf.file_group_id IN (SELECT v.file_group_id FROM video v
|
||||
WHERE v.channel_id = :id)) AS "video_tags"
|
||||
FROM video
|
||||
LEFT JOIN file_group fg on fg.id = video.file_group_id
|
||||
LEFT JOIN file_group fg INDEXED BY file_group_published_size_length_idx
|
||||
ON fg.id = video.file_group_id
|
||||
WHERE channel_id = :id \
|
||||
'''
|
||||
curs.execute(stmt, dict(id=self.id))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue