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.
This commit is contained in:
Josh Hawkins
2026-10-15 16:25:59 -05:00
parent 16a377233e
commit d69a41a948
+6 -1
View File
@@ -42,6 +42,7 @@ from frigate.config.camera.snapshots import SnapshotsConfig
from frigate.const import ( from frigate.const import (
CACHE_DIR, CACHE_DIR,
INSTALL_DIR, INSTALL_DIR,
MAX_SEGMENT_DURATION,
PREVIEW_FRAME_TYPE, PREVIEW_FRAME_TYPE,
STREAM_TYPE_MAIN, STREAM_TYPE_MAIN,
STREAM_TYPE_SUB, STREAM_TYPE_SUB,
@@ -350,6 +351,7 @@ async def get_snapshot_from_recording(
.where( .where(
(frame_time >= Recordings.start_time) (frame_time >= Recordings.start_time)
& (frame_time <= Recordings.end_time) & (frame_time <= Recordings.end_time)
& (Recordings.start_time >= frame_time - MAX_SEGMENT_DURATION)
) )
.where(Recordings.camera == camera_name) .where(Recordings.camera == camera_name)
.order_by(Recordings.stream_type.asc(), Recordings.start_time.desc()) .order_by(Recordings.stream_type.asc(), Recordings.start_time.desc())
@@ -369,6 +371,7 @@ async def get_snapshot_from_recording(
.where( .where(
(frame_time >= Recordings.start_time) (frame_time >= Recordings.start_time)
& (frame_time <= Recordings.end_time) & (frame_time <= Recordings.end_time)
& (Recordings.start_time >= frame_time - MAX_SEGMENT_DURATION)
) )
.where(Recordings.camera == camera_name) .where(Recordings.camera == camera_name)
.order_by(Recordings.stream_type.asc(), Recordings.start_time.desc()) .order_by(Recordings.stream_type.asc(), Recordings.start_time.desc())
@@ -433,7 +436,9 @@ async def submit_recording_snapshot_to_plus(
Recordings.start_time, Recordings.start_time,
) )
.where( .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) .where(Recordings.camera == camera_name)
.order_by(Recordings.stream_type.asc(), Recordings.start_time.desc()) .order_by(Recordings.stream_type.asc(), Recordings.start_time.desc())