diff --git a/cmd/scan.go b/cmd/scan.go index d37ccd69f..681bdcdf0 100644 --- a/cmd/scan.go +++ b/cmd/scan.go @@ -63,6 +63,8 @@ func trackScanAsSubprocess(ctx context.Context, progress <-chan *scanner.Progres } func runScanner(ctx context.Context) { + defer db.Init(ctx)() + sqlDB := db.Db() defer db.Db().Close() ds := persistence.New(sqlDB) diff --git a/db/migrations/20251101180108_add_media_file_artists_artist_id_index.sql b/db/migrations/20251101180108_add_media_file_artists_artist_id_index.sql new file mode 100644 index 000000000..f3162ff59 --- /dev/null +++ b/db/migrations/20251101180108_add_media_file_artists_artist_id_index.sql @@ -0,0 +1,7 @@ +-- +goose Up +-- Add index on artist_id for media_file_artists table to improve query performance +-- This index is crucial for the RefreshStats query in artistRepository +CREATE INDEX IF NOT EXISTS media_file_artists_artist_id ON media_file_artists(artist_id); + +-- +goose Down +DROP INDEX IF EXISTS media_file_artists_artist_id; diff --git a/persistence/artist_repository.go b/persistence/artist_repository.go index 07a4babe3..7e4c0dfca 100644 --- a/persistence/artist_repository.go +++ b/persistence/artist_repository.go @@ -399,7 +399,7 @@ func (r *artistRepository) RefreshStats(allArtists bool) (int64, error) { } // Template for the batch update with placeholder markers that we'll replace - // This now calculates per-library statistics and stores them in library_artist.stats + // This calculates per-library statistics and stores them in library_artist.stats batchUpdateStatsSQL := ` WITH artist_role_counters AS ( SELECT mfa.artist_id,