diff --git a/modules/videos/models.py b/modules/videos/models.py index 93caddc5..7f1b1d19 100644 --- a/modules/videos/models.py +++ b/modules/videos/models.py @@ -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. diff --git a/modules/videos/video/test/test_api.py b/modules/videos/video/test/test_api.py index 8ff706ea..c5594c61 100644 --- a/modules/videos/video/test/test_api.py +++ b/modules/videos/video/test/test_api.py @@ -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.