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

@ -47,17 +47,13 @@ func AlbumsByArtist() Options {
}
func AlbumsByArtistID(artistId string) Options {
filters := []Sqlizer{
persistence.Exists("json_tree(participants, '$.albumartist')", Eq{"value": artistId}),
}
roles := []model.Role{model.RoleAlbumArtist}
if conf.Server.Subsonic.ArtistParticipations {
filters = append(filters,
persistence.Exists("json_tree(participants, '$.artist')", Eq{"value": artistId}),
)
roles = append(roles, model.RoleArtist)
}
return addDefaultFilters(Options{
Sort: "max_year",
Filters: Or(filters),
Filters: persistence.ParticipantIDFilter("album", artistId, roles...),
})
}
@ -68,8 +64,8 @@ func AlbumsByContributingArtistID(artistId string) Options {
return addDefaultFilters(Options{
Sort: "max_year",
Filters: And{
persistence.Exists("json_tree(participants, '$.artist')", Eq{"value": artistId}),
persistence.NotExists("json_tree(participants, '$.albumartist')", Eq{"value": artistId}),
persistence.ParticipantIDFilter("album", artistId, model.RoleArtist),
persistence.NotParticipantIDFilter("album", artistId, model.RoleAlbumArtist),
},
})
}
@ -104,13 +100,11 @@ func SongsByAlbum(albumId string) Options {
}
// SongsByArtistID matches media files where the artist participates as album or track artist, in
// album order. Semi-joins media_file_artists; scanning the participants JSON is ~10x slower at scale.
// album order.
func SongsByArtistID(artistId string) Options {
return addDefaultFilters(Options{
Sort: "album",
Filters: Expr(
"media_file.id IN (SELECT media_file_id FROM media_file_artists WHERE artist_id = ? AND role IN (?, ?))",
artistId, model.RoleArtist.String(), model.RoleAlbumArtist.String()),
Sort: "album",
Filters: persistence.ParticipantIDFilter("media_file", artistId, model.RoleArtist, model.RoleAlbumArtist),
})
}

View file

@ -219,7 +219,7 @@ var _ = Describe("Items", func() {
albumRepo := ds.Album(context.Background()).(*tests.MockAlbumRepo)
sql, _, err := albumRepo.Options.Filters.ToSql()
Expect(err).NotTo(HaveOccurred())
Expect(sql).To(ContainSubstring("json_tree"))
Expect(sql).To(ContainSubstring("album_artists"))
})
It("lists artists when IncludeItemTypes=MusicArtist", func() {
@ -708,7 +708,7 @@ var _ = Describe("Items", func() {
Expect(w.Code).To(Equal(http.StatusOK))
sql, args, err := albumRepo.Options.Filters.ToSql()
Expect(err).NotTo(HaveOccurred())
Expect(sql).NotTo(ContainSubstring("json_tree")) // not treated as an artist-parent filter
Expect(sql).NotTo(ContainSubstring("album_artists")) // not treated as an artist-parent filter
Expect(sql).To(ContainSubstring("library_id"))
Expect(args).To(ContainElement(2))
})
@ -724,7 +724,7 @@ var _ = Describe("Items", func() {
sql, args, err := albumRepo.Options.Filters.ToSql()
Expect(err).NotTo(HaveOccurred())
// Falls back to treating "99" as an (empty-matching) artist-parent id...
Expect(sql).To(ContainSubstring("json_tree"))
Expect(sql).To(ContainSubstring("album_artists"))
// ...while still scoping to the user's own accessible libraries.
Expect(sql).To(ContainSubstring("library_id"))
Expect(args).To(ContainElement(1))