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 <noreply@anthropic.com>
This commit is contained in:
Spinnich
2026-08-01 19:35:23 +00:00
parent dea964f17e
commit 60eb455fe8
2 changed files with 94 additions and 1 deletions

View File

@@ -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,
)

View File

@@ -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"),