From d69a41a948c5bc5ca4e3d4c4b108d9ac66b75e77 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:25:59 -0500 Subject: [PATCH] bound recording snapshot lookups by segment length Ordering the recording snapshot and Frigate+ lookups by stream_type ahead of start_time broke index order, so SQLite fetched and sorted every older recording for the camera on each request (about 100 ms on a 30 day camera, run on the event loop). A start_time lower bound of the frame time minus MAX_SEGMENT_DURATION keeps the lookup to a few rows. --- frigate/api/media.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frigate/api/media.py b/frigate/api/media.py index e260aa18fd..9613549646 100644 --- a/frigate/api/media.py +++ b/frigate/api/media.py @@ -42,6 +42,7 @@ from frigate.config.camera.snapshots import SnapshotsConfig from frigate.const import ( CACHE_DIR, INSTALL_DIR, + MAX_SEGMENT_DURATION, PREVIEW_FRAME_TYPE, STREAM_TYPE_MAIN, STREAM_TYPE_SUB, @@ -350,6 +351,7 @@ async def get_snapshot_from_recording( .where( (frame_time >= Recordings.start_time) & (frame_time <= Recordings.end_time) + & (Recordings.start_time >= frame_time - MAX_SEGMENT_DURATION) ) .where(Recordings.camera == camera_name) .order_by(Recordings.stream_type.asc(), Recordings.start_time.desc()) @@ -369,6 +371,7 @@ async def get_snapshot_from_recording( .where( (frame_time >= Recordings.start_time) & (frame_time <= Recordings.end_time) + & (Recordings.start_time >= frame_time - MAX_SEGMENT_DURATION) ) .where(Recordings.camera == camera_name) .order_by(Recordings.stream_type.asc(), Recordings.start_time.desc()) @@ -433,7 +436,9 @@ async def submit_recording_snapshot_to_plus( Recordings.start_time, ) .where( - (frame_time >= Recordings.start_time) & (frame_time <= Recordings.end_time) + (frame_time >= Recordings.start_time) + & (frame_time <= Recordings.end_time) + & (Recordings.start_time >= frame_time - MAX_SEGMENT_DURATION) ) .where(Recordings.camera == camera_name) .order_by(Recordings.stream_type.asc(), Recordings.start_time.desc())