From 60eb455fe802a54685dbdc77fe276bd7cc176be0 Mon Sep 17 00:00:00 2001 From: Spinnich Date: Sat, 1 Aug 2026 19:35:23 +0000 Subject: [PATCH] perf(roms): cover the sibling dedup window with its index `idx_roms_sibling_cover` (0090) was built for the sibling_roms self-join, but the group_by_meta_id dedup window in `filter_roms` reads the same shape plus two columns the index does not carry: `flashpoint_id`, the tail of the partition's COALESCE chain, and `fs_name_no_ext`, the tiebreaker each partition sorts by. A covering index has to carry every referenced column, so those two omissions dropped the window to a full scan of the wide roms row -- JSON metadata blobs included -- and the gallery runs that window up to four times per page load (page, count, char_index, rom_id_index). Grouping is on by default, so this is the default path for every large library. Measured on a 92.8k-rom MariaDB copy (963 MB roms table, 256 MB buffer pool) with the queries the endpoint actually generates: page of 72 5.43s -> 0.80s count 5.55s -> 1.01s rom_id_index 5.48s -> 1.10s char_index 5.46s -> 1.06s combined 21.93s -> 3.97s EXPLAIN moves from `type=ALL, key=NULL` to `type=index, Using index`. Real `fs_name_no_ext` values are far shorter than the column's varchar(450), so the index grows 4 MB -> 7 MB and rebuilds in ~5s at that row count. The wider index costs the sibling_roms join ~170ms -> ~209ms per 72-rom page. Keeping the narrow index alongside a second dedup-only one recovers that, but the two would share their first eight columns and the per-page totals came out level (2.13s vs 2.12s), so a single index wins on write amplification and stops the two consumers drifting apart again. Co-Authored-By: Claude Opus 5 --- .../versions/0107_roms_dedup_cover_index.py | 87 +++++++++++++++++++ backend/models/rom.py | 8 +- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 backend/alembic/versions/0107_roms_dedup_cover_index.py diff --git a/backend/alembic/versions/0107_roms_dedup_cover_index.py b/backend/alembic/versions/0107_roms_dedup_cover_index.py new file mode 100644 index 000000000..6370fb1ae --- /dev/null +++ b/backend/alembic/versions/0107_roms_dedup_cover_index.py @@ -0,0 +1,87 @@ +"""Extend the sibling covering index to cover the group_by_meta_id dedup + +0090 added `idx_roms_sibling_cover` for the sibling_roms self-join. The +group_by_meta_id dedup window in `filter_roms` reads the same shape, but two of +its inputs are missing from that index: `flashpoint_id` (the last provider in +the partition's COALESCE chain) and `fs_name_no_ext` (the tiebreaker the window +sorts each partition by). A covering index has to carry every referenced +column, so those two omissions dropped the window to a full scan of the wide +roms row -- JSON metadata blobs and all -- once per query. + +The gallery runs that window up to four times per page load (page, count, +char_index, rom_id_index), so the scan is paid repeatedly. Measured on a +92.8k-rom MariaDB copy with a 256 MB buffer pool against a 963 MB roms table: +each of those four queries went 5.4-5.6s -> 0.8-1.1s (21.9s -> 4.0s combined), +with EXPLAIN moving from `type=ALL, key=NULL` to `type=index, Using index`. + +Adding `fs_name_no_ext` (varchar 450) widens the index, but real names are far +shorter than the column's maximum: on that dataset the index grew 4 MB -> 7 MB. + +The wider index costs the sibling_roms join a little more to scan (~170ms -> +~209ms for a 72-rom page, measured over alternating A/B runs). Keeping the old +narrow index alongside a second dedup-only one recovers that, but the two would +share their first eight columns, and per page load the totals came out the same +(2.13s vs 2.12s), so one index wins on write amplification and on not letting +the two consumers drift apart again. + +Revision ID: 0107_roms_dedup_cover_index +Revises: 0106_hash_search_indexes +Create Date: 2026-08-01 00:00:00.000000 + +""" + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "0107_roms_dedup_cover_index" +down_revision = "0106_hash_search_indexes" +branch_labels = None +depends_on = None + +INDEX_NAME = "idx_roms_sibling_cover" +OLD_INDEX_COLUMNS = [ + "platform_id", + "igdb_id", + "moby_id", + "ss_id", + "launchbox_id", + "ra_id", + "hasheous_id", + "tgdb_id", + "id", +] +NEW_INDEX_COLUMNS = [ + "platform_id", + "igdb_id", + "moby_id", + "ss_id", + "launchbox_id", + "ra_id", + "hasheous_id", + "tgdb_id", + "flashpoint_id", + "fs_name_no_ext", + "id", +] + + +def upgrade() -> None: + with op.batch_alter_table("roms", schema=None) as batch_op: + batch_op.drop_index(INDEX_NAME, if_exists=True) + batch_op.create_index( + INDEX_NAME, + NEW_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) + batch_op.create_index( + INDEX_NAME, + OLD_INDEX_COLUMNS, + unique=False, + if_not_exists=True, + ) diff --git a/backend/models/rom.py b/backend/models/rom.py index f7e9be911..95a7b0f9b 100644 --- a/backend/models/rom.py +++ b/backend/models/rom.py @@ -315,7 +315,11 @@ class Rom(BaseModel): __table_args__ = ( # Enforce unique fs name per platform to avoid duplicates Index("idx_roms_platform_id_fs_name", "platform_id", "fs_name", unique=True), - # Covers the sibling_roms view self-join + # Covers the sibling_roms view self-join and the group_by_meta_id dedup + # window. Both read only these columns, so the index has to carry every + # one of them: a single missing column (flashpoint_id or fs_name_no_ext, + # the window's partition tail and sort tiebreaker) drops the plan to a + # full scan of the wide roms row, JSON metadata blobs included. Index( "idx_roms_sibling_cover", "platform_id", @@ -326,6 +330,8 @@ class Rom(BaseModel): "ra_id", "hasheous_id", "tgdb_id", + "flashpoint_id", + "fs_name_no_ext", "id", ), Index("idx_roms_platform_fs_size", "platform_id", "fs_size_bytes"),