diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index c6b78decc..c213303e9 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -528,6 +528,7 @@ def get_roms( player_counts_logic=player_counts_logic, group_by_meta_id=group_by_meta_id, updated_after=updated_after, + include_file_stats=True, ) # Get the char index for the roms diff --git a/backend/handler/database/roms_handler.py b/backend/handler/database/roms_handler.py index e7058d9f6..c3638f1fd 100644 --- a/backend/handler/database/roms_handler.py +++ b/backend/handler/database/roms_handler.py @@ -549,6 +549,7 @@ class DBRomsHandler(DBBaseHandler): player_counts_logic: str = "any", user_id: int | None = None, updated_after: datetime | None = None, + include_file_stats: bool = False, session: Session = None, # type: ignore ) -> Query[Rom]: from handler.scan_handler import MetadataSource @@ -566,11 +567,16 @@ class DBRomsHandler(DBBaseHandler): ), # Show notes indicator on cards selectinload(Rom.notes), - # Gallery card needs file types and counts - undefer(Rom.multi_file), - undefer(Rom.top_level_file_count), ) + # Correlated subqueries and only undefer when the caller serializes the + # gallery-card flags. Feeds and filter-value lookups don't need them. + if include_file_stats: + query = query.options( + undefer(Rom.multi_file), + undefer(Rom.top_level_file_count), + ) + # Handle platform filtering - platform filtering always uses OR logic since ROMs belong to only one platform if platform_ids: query = self._filter_by_platform_ids(query, platform_ids) diff --git a/backend/models/rom.py b/backend/models/rom.py index 81631911a..e0896b910 100644 --- a/backend/models/rom.py +++ b/backend/models/rom.py @@ -446,12 +446,8 @@ class Rom(BaseModel): return f"{self.fs_name} ({self.id})" -# Gallery-card flags derived from rom_files, computed by the database as -# correlated subqueries so the list endpoint never has to load the rom_files -# rows themselves. Deferred so single-row queries that don't read the flags -# (scan, file CRUD, etc.) don't pay for the subqueries; the gallery list and -# detail endpoints opt in via `undefer`. Defined out-of-line because they -# reference both Rom and RomFile. +# Correlated scalar subqueries against rom_files, deferred and opt-in via `undefer` +# Revisit (real columns, JOIN/aggregate, or added indexes) if gallery latency regresses _rom_full_path = func.concat(Rom.fs_path, "/", Rom.fs_name) Rom.multi_file = column_property(