perf(db): narrow mirror of the hot roms columns for list queries

Open
#5,848 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Refactor
Clarity
Clearly specified
Activity status
Active
Tech stack
mariadb, postgresql, python, sql

Research direction

Start with utils/roms_columns.py and handler/database/roms_handler.py, then locate the roms_facets migrations and triggers for the four existing mirrors. The issue identifies the candidate columns and affected list-query shapes but leaves the final mirror design and query-builder changes open. Done means the chosen narrow mirror stays synchronized across MariaDB, MySQL and PostgreSQL, list queries read the appropriate columns, and endpoint timing at 128 MB and 512 MB confirms the intended improvement without result changes.

Written by the indexing model from the issue text.

Description

Is your feature request related to a problem? Please describe.

On MariaDB, roms is about 1.1 GB at 100k ROMs (about 11 KB per row), because the provider metadata JSON is stored inline. Any list query that has no index to read has to scan those wide rows, and a stock MariaDB image runs with a 128 MB buffer pool, so the scan goes to disk every time. PostgreSQL moves the blobs out of the main heap with TOAST, so this is mostly a MariaDB problem.

The discussion on #5845 made this concrete. That PR's grouped-gallery query was about 3x slower than master on a 128 MB pool and about 2x faster on 512 MB, because it fetched about 16,000 wide roms rows by primary key. The PR is being fixed by driving the query from the existing covering index, so this issue does not block it. But adding one covering index per slow query does not scale.

Describe the solution you'd like

A narrow, trigger-maintained mirror of the columns that list queries sort and filter on, preferably by extending roms_facets. That table already mirrors platform_id, the filter values and the provider ids, already has its triggers, and already has a visibility join. The new columns would be name_sort_key, fs_name_no_ext, generated_primary_region, fs_size_bytes and the generated sort columns.

@andest01 ran a spike in #5845 (SQL level only, MariaDB, 128 MB pool, 100k ROMs) with a throwaway narrow copy of the scalar columns:

Query roms (ms) Narrow copy (ms)
grouped window (the #5845 shape) ~4,300 ~125
count: rated, 10+ ratings 2,992 46
size desc, first 72 2,228 56
name asc, offset 50,000 11 6
release date desc 28 26

The copy is 23 MB of data against 1,140 MB. Where roms already has an index to read, the two tie. Where it has to scan, the copy is 20x to 65x faster. Every query shape returned identical results, and the grouped window matched for all 12 test users. The list queries only touch 1 to 5 roms columns. The rest are read when the 72 page rows are loaded, which took 21 to 32 ms.

Describe alternatives you've considered

  • A new, fifth mirror table. It would be smaller than roms_facets, which is 55 MB because of its JSON columns, but it needs another trigger set. The spike suggests measuring both.
  • A true split of roms into hot and cold tables. Rejected: the STORED generated columns are expressions over the blobs (utils/roms_columns.py), so moving the blobs out breaks them.
  • Shrinking or rebuilding roms (free space, ROW_FORMAT=COMPRESSED, page compression). The columns average about 6 KB against 11 KB stored per row. This is being checked separately, and it should be settled first, since it could make a mirror unnecessary.

Additional context

Open questions and costs:

  • roms already has 8 triggers, one insert and one update trigger for each of four mirrors. Each new mirrored column needs a migration that updates the triggers, and drift between the mirror and roms is the main correctness risk.
  • Write cost isn't explained yet. Updating 1,000 rows took about 803 ms on roms and about 6 ms on the copy. The test didn't separate the triggers' cost from the cost of the 44 secondary indexes on roms.
  • Pointing the query builder in handler/database/roms_handler.py at the mirror is the largest cost, and it hasn't been measured. It has to stay correct on MariaDB, MySQL and PostgreSQL.
  • A first prototype could cover only the grouped gallery query, timed through the real endpoint at 128 MB and 512 MB.

AI disclosure: this issue was drafted by Claude Code from the #5845 discussion and the spike write-up attached there.

🤖 Generated with Claude Code

Dominant language
Python
Stars
13.3k
Forks
747
Avg merge
20h 58m
Merged PRs (30d)
207

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rommapp/romm

All issues in rommapp/romm

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.