Files
romm/backend/tests/handler/database
Spinnich 850d7e5f2a perf(gallery): materialize the ids sibling_roms matches on
`sibling_roms` was a view that self-joined `roms` on an OR of seven
equalities across seven columns. The compared values come from the joined
row, so no index can drive that OR: MariaDB answers it by block-nested-loop
scanning every ROM on the platform. Every gallery page paid that for its
version badges, and the gallery's "Versions" filter asked the same view for
an EXISTS over the whole library.

Migration 0126 moves the ids into `rom_identity_keys`, one narrow row per
ROM per provider it matched, and redefines `sibling_roms` as a self-join of
that table. Triggers on `roms` keep it in sync, guarded by `NEW.x <=> OLD.x`
so the writes that touch no identity id skip the resync; deletes ride the
foreign key's cascade. This is the move 0101 made for `virtual_collections`.

Matching is unchanged: the same seven providers, still scoped to a platform.
No application code changes and no API shape changes.

Measured on a clone of a real 96k-ROM, 123-platform library:

  GET /api/roms?duplicate=true    977,571 ms -> 22,149 ms    (45x)
  PlayStation gallery (9,196)       1,039.0 ms -> 382.2 ms   (2.7x)
  Arcade gallery (7,719)              844.5 ms -> 296.2 ms   (2.9x)
  all-games gallery                 3,023.9 ms -> 2,690.5 ms (1.1x)

The all-games page barely moves because its rows are drawn from 123 mostly
small platforms, so the per-platform scan was cheap per row. Its residual is
almost entirely the group_by_meta_id dedup window (2,535 ms of 2,676 ms
measured on the same clone), a separate problem.

The view deliberately has no DISTINCT: a pair matched by two providers emits
two rows, and DISTINCT would stop the view merging into the caller's query
at 11x the cost. Both readers already collapse duplicates.

The ANALYZE after the backfill is load-bearing. Without sampled statistics
the optimizer estimates thousands of rows per lookup and picks a worse join
order for the rest of the list query, leaving the gallery slower than before
this ran.

Fixes #4391
Related to #4382

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-08 10:39:32 +00:00
..
2025-10-23 11:12:17 -04:00
2026-08-01 14:49:58 -04:00