Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions backend/alembic/versions/0097_roms_platform_fs_size_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Add composite index on roms (platform_id, fs_size_bytes)

GET /api/platforms and GET /api/stats both sum roms.fs_size_bytes (per platform
and across the whole library). fs_size_bytes was not part of any index, so the
database had to read actual rows from the very wide, JSON-heavy roms table,
taking 5-11s on large libraries. This composite index lets both sums resolve as
index-only scans that never touch the wide rows; the optimizer picks it for both
query shapes on its own.

Revision ID: 0097_roms_platform_fs_size_index
Revises: 0096_fix_virtual_collections
Create Date: 2026-07-16 00:00:00.000000

"""

from alembic import op

# revision identifiers, used by Alembic.
revision = "0097_roms_platform_fs_size_index"
down_revision = "0096_fix_virtual_collections"
branch_labels = None
depends_on = None

INDEX_NAME = "idx_roms_platform_fs_size"
INDEX_COLUMNS = ["platform_id", "fs_size_bytes"]


def upgrade() -> None:
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.create_index(
INDEX_NAME,
INDEX_COLUMNS,
unique=False,
if_not_exists=True,
)


def downgrade() -> None:
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.drop_index(INDEX_NAME, if_exists=True)
3 changes: 3 additions & 0 deletions backend/models/rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ class Rom(BaseModel):
"tgdb_id",
"id",
),
# Covering index so per-platform and whole-library SUM(fs_size_bytes)
# are index-only scans that never touch the wide roms rows
Index("idx_roms_platform_fs_size", "platform_id", "fs_size_bytes"),
Index("idx_roms_name", "name"),
Index("idx_roms_name_sort_key", "name_sort_key"),
Index("idx_roms_igdb_id", "igdb_id"),
Expand Down
Loading