mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-01 20:36:21 -05:00
Video page: find a Video's neighbors from indexes, not rows (#1062)
* 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> * Video page: find a Video's neighbors from indexes, not rows The previous/next Videos are found by numbering every Video in the Channel by its published date, which read each Video's file_group row. Cold, that took 11 s on a Channel with about 2,000 Videos. The query now reads the published dates from file_group_published_size_length_idx, and orders its result so previous and next cannot swap. 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
3668bf1ce9
commit
47c943878d
2 changed files with 36 additions and 4 deletions
|
|
@ -190,13 +190,16 @@ class Video(ModelHelper, Base):
|
|||
|
||||
with session_curs(session) as curs:
|
||||
if self.file_group.published_datetime:
|
||||
# Get videos next to this Video's upload date.
|
||||
# Get videos next to this Video's upload date. Every Video in the Channel is numbered, so read
|
||||
# the published dates from indexes: reading each video and file_group row is a scattered page.
|
||||
stmt = '''
|
||||
WITH numbered_videos AS (SELECT fg.id AS fg_id,
|
||||
v.id AS v_id,
|
||||
ROW_NUMBER() OVER (ORDER BY published_datetime ASC) AS row_number
|
||||
FROM file_group fg
|
||||
LEFT OUTER JOIN video v on fg.id = v.file_group_id
|
||||
FROM video v
|
||||
CROSS JOIN file_group fg
|
||||
INDEXED BY file_group_published_size_length_idx
|
||||
ON fg.id = v.file_group_id
|
||||
WHERE v.channel_id = :channel_id
|
||||
AND fg.published_datetime IS NOT NULL)
|
||||
SELECT v_id
|
||||
|
|
@ -204,7 +207,8 @@ class Video(ModelHelper, Base):
|
|||
WHERE row_number IN (SELECT row_number + i
|
||||
FROM numbered_videos
|
||||
CROSS JOIN (SELECT -1 AS i UNION ALL SELECT 0 UNION ALL SELECT 1) n
|
||||
WHERE fg_id = :fg_id) \
|
||||
WHERE fg_id = :fg_id)
|
||||
ORDER BY row_number \
|
||||
'''
|
||||
else:
|
||||
# No videos near this Video with upload dates, recommend the files next to this Video.
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
import re
|
||||
from datetime import timedelta
|
||||
from http import HTTPStatus
|
||||
from json import dumps
|
||||
|
|
@ -69,6 +70,33 @@ def test_get_video_prev_next(async_client, test_session, channel_factory, video_
|
|||
raise AssertionError(f'Assert failed for {id_=} {prev_title=} {next_title=}') from e
|
||||
|
||||
|
||||
def test_get_video_prev_next_reads_no_rows(async_client, test_session, channel_factory, video_factory):
|
||||
"""A Video's neighbors by published date are found from indexes, never from video or file_group rows.
|
||||
|
||||
Every Video in the Channel is numbered by its published date; reading each of their rows from a cold disk
|
||||
cache took seconds on a large Channel."""
|
||||
channel = channel_factory()
|
||||
now_ = now()
|
||||
videos = [video_factory(channel.id, upload_date=now_ + timedelta(seconds=i), title=f'vid{i}') for i in range(3)]
|
||||
test_session.commit()
|
||||
|
||||
statements = []
|
||||
connection = test_session.connection().connection.dbapi_connection
|
||||
connection.set_trace_callback(lambda sql: statements.append(sql) if 'numbered_videos' in sql else None)
|
||||
try:
|
||||
prev_video, next_video = videos[1].get_surrounding_videos()
|
||||
finally:
|
||||
connection.set_trace_callback(None)
|
||||
assert (prev_video, next_video) == (videos[0], videos[2])
|
||||
|
||||
assert len(statements) == 1
|
||||
plan = [i[3] for i in connection.execute(f'EXPLAIN QUERY PLAN {statements[0]}')]
|
||||
row_steps = [i for i in plan if re.match(r'(SEARCH|SCAN) (v|fg)\b', i)]
|
||||
assert row_steps, plan
|
||||
for step in row_steps:
|
||||
assert 'COVERING INDEX' in step, plan
|
||||
|
||||
|
||||
def test_get_video_prev_next_no_upload_date(async_client, test_session, video_factory, channel_factory):
|
||||
"""
|
||||
Previous and Next Videos should be those next to a Video file when videos do not have upload dates.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue