perf(persistence): use *_artists join tables for artist participant filters (#5930)

* perf(persistence): use *_artists join tables for artist participant filters

The artist_id/artists_id, role_<role>_id and role_total_id filters, plus the
AlbumsByArtistID/AlbumsByContributingArtistID/SongsByArtistID helpers, scanned
every album or media_file row through json_tree(participants, ...), which no
index can serve — the cause of multi-second artist pages on large libraries
(discussion #5929). Rewrite them to semi-join the album_artists and
media_file_artists tables via a shared ParticipantIDFilter helper. The join
tables are written in the same transaction as the participants JSON, so
results are unchanged.

album_artists' unique constraint led with album_id, so artist-driven lookups
had no usable index. Rebuild the table with the constraint reordered to
(artist_id, album_id, role, sub_role), mirroring media_file_artists, instead
of adding a fourth index: measured within 4% of a dedicated covering index
(geomean -92.5% vs json_tree on a 96k-track production copy) while saving
~7MiB and per-scan write amplification. Album-side consumers (participant
rewrites, FK cascades, markMissing) keep using album_artists_album_id, and
updateParticipants' ON CONFLICT target already names artist_id first. The
rebuild is linear work: 1.1s on a 113k-row production copy.

* chore(gitignore): add temp benchmark files to ignore list

* fix(persistence): clear album_artists when an album is saved without participants

albumRepository.Put skipped updateParticipants when the Participants map was empty, so a hypothetical save with no participants would write {} to the JSON column but leave stale album_artists rows behind, now visible through the semi-join filters. No current caller can hit this (albums built by MediaFiles.ToAlbum always have participants), but make Put unconditional anyway, matching mediaFileRepository.Put, so the join table always moves with the JSON. Raised by Codex review on #5930.
This commit is contained in:
Deluan Quintão 2026-08-10 11:42:27 -04:00 • committed by GitHub
commit 7993fb9158
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 184 additions and 42 deletions

View file

@ -0,0 +1,59 @@
-- +goose Up
-- +goose StatementBegin
-- Lead the unique constraint with artist_id (mirroring media_file_artists) so artist-driven
-- filters and the artist-delete cascade get an index seek; album_id lookups keep album_artists_album_id.
CREATE TABLE album_artists_tmp
(
album_id varchar not null
REFERENCES album (id)
ON DELETE CASCADE,
artist_id varchar not null
REFERENCES artist (id)
ON DELETE CASCADE,
role varchar default '' not null,
sub_role varchar default '' not null,
CONSTRAINT album_artists
UNIQUE (artist_id, album_id, role, sub_role)
);
INSERT INTO album_artists_tmp(album_id, artist_id, role, sub_role)
SELECT album_id, artist_id, role, sub_role
FROM album_artists;
DROP TABLE album_artists;
ALTER TABLE album_artists_tmp RENAME TO album_artists;
CREATE INDEX album_artists_album_id
ON album_artists (album_id);
CREATE INDEX album_artists_role
ON album_artists (role);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
CREATE TABLE album_artists_tmp
(
album_id varchar not null
REFERENCES album (id)
ON DELETE CASCADE,
artist_id varchar not null
REFERENCES artist (id)
ON DELETE CASCADE,
role varchar default '' not null,
sub_role varchar default '' not null,
CONSTRAINT album_artists
UNIQUE (album_id, artist_id, role, sub_role)
);
INSERT INTO album_artists_tmp(album_id, artist_id, role, sub_role)
SELECT album_id, artist_id, role, sub_role
FROM album_artists;
DROP TABLE album_artists;
ALTER TABLE album_artists_tmp RENAME TO album_artists;
CREATE INDEX album_artists_album_id
ON album_artists (album_id);
CREATE INDEX album_artists_role
ON album_artists (role);
-- +goose StatementEnd