Commit Graph

4 Commits

Author SHA1 Message Date
Zach Clendenen
10865b8663 perf(roms): faster smart collection and filter value queries (#5069)
* perf(roms): stop MariaDB rescanning the library for smart collections

MariaDB plans the smart collection membership IN (subquery) as a semi-join that rescans every rom per page query. A NO_SEMIJOIN hint keeps it a plain subquery; other dialects don't emit the hint.

* perf(roms): parse each distinct filter value list once

The filter dropdown query decoded every JSON column of every row. The columns are now read as text, deduplicated, and each distinct list is parsed once. PostgreSQL decodes JSON in the driver, so its lists are merged as returned.

* test(roms): cover repeated filter value lists in pr-ready code review

* refactor(roms): pr-ready simplify pass

* style(roms): pr-ready polish

* fix(roms): address Greptile review

Fold filter value rows in batches of 5000 instead of materializing the whole result, keeping the parse-each-distinct-list-once dedup across batches, so /api/roms/filters no longer holds one row per ROM in memory (Greptile: filter requests retain every row). The repeated-lists test now runs with a batch size of 2 to cover the cross-batch path.

* fix(roms): pr-ready code review fixes

- Cast filter JSON columns to text in SQL, so PostgreSQL gets the
  parse-once saving and a scalar JSONB value no longer crashes json.loads
- Stream filter rows with yield_per instead of only partitioning
- Name the JSON filter columns instead of hard-coding positions
- Add optimizer_hint_on_mysql next to force_index_on_mysql, with tests
- Make the repeated-lists test span batches regardless of row order

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(roms): pr-ready simplify pass

Collect each filter column's distinct JSON texts and parse them once at
the end, and key the JSON filter columns by their RomFiltersDict field so
the positional unpack and per-field return go away.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* style(roms): pr-ready polish

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-03 10:29:25 -04:00
Sam Dornan
5f50afa123 Search the gallery by providers' alternative titles (#4941)
* Search the gallery by providers' alternative titles

A search for "FF9" or "Final Fantasy 9" did not find "Final Fantasy IX":
the gallery search only matched roms.name and roms.fs_name, while the
alternative titles IGDB, MobyGames and ScreenScraper report lived in the
raw metadata blobs where no index reaches.

Add generated_search_aliases, a STORED generated column the engine fills
from those blobs, so every write path (scans, metadata refreshes, the
bulk update_rom, raw metadata edits) keeps it current with no backfill.
Migration 0145 rebuilds the MariaDB/MySQL FULLTEXT index over
(name, fs_name, aliases) and adds a pg_trgm GIN index on PostgreSQL, and
the search matches the column on both paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy

* Compare the alias JSON type under the table's collation

On a utf8mb3_unicode_ci table, JSON_TYPE's result and the 'ARRAY'
literal carry different collations, so creating the generated column
failed with an illegal mix. CAST re-derives it, as _maria_text does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy

* pr-ready step 2 (code-review): defer the alias column, share search columns

Loading a ROM no longer fetches generated_search_aliases, which only the
gallery search reads. The FULLTEXT match and the ILIKE fallback both take
their columns from one ROM_SEARCH_COLUMNS tuple.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy

* pr-ready step 3 (simplify): register the search indexes, share the CAST

Both search indexes join the generated-column index registry, so any
later rebuild of the alias column puts them back instead of leaving
MariaDB's FULLTEXT index narrowed (and every MATCH failing) or the
PostgreSQL trgm index gone. 0145 shrinks to ensure_roms_columns plus
retiring 0084's index, and its downgrade guards DROP COLUMN with
has_column, since MySQL has no DROP COLUMN IF EXISTS.

_maria_char carries the collation CAST that _maria_text and the alias
expression share, the alias expression extracts each array twice
instead of three times, and the dialect test runs the handler's own
_fulltext_match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy

* Store the search aliases as plain titles

The alias column held each provider's array as JSON text, so a short
ILIKE search for '"', ',' or '[' matched every ROM with any alias. Both
engines print an array as ["a", "b"] however it was stored, so the
expression strips the brackets and quotes and keeps the titles
separated by spaces, which search terms can never match on their own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy

* Shorten the alias-search docstrings to one sentence

The migration and test-module docstrings explained the design in
several paragraphs; AGENTS.md asks for one sentence. The design notes
live in the PR description.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01A62FTdVyav4wK5Nkj55MGy

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-09-30 13:16:13 -05:00
Sam Dornan
84726ba9a2 refactor(backend): move FORCE INDEX hint and FULLTEXT match into sql_dialect (#4825) 2026-09-26 19:08:25 -04:00
Sam Dornan
9c1e6b2d86 refactor(backend): move engine-specific query SQL out of handlers into utils/sql_dialect.py (#4793) 2026-09-26 13:48:33 -04:00