Materialize record releases to improve performance

This commit is contained in:
Claudio Ortolina
2026-02-16 12:30:41 +00:00
parent d6c19d3a40
commit 98f901c020
4 changed files with 98 additions and 16 deletions
+4 -8
View File
@@ -3,7 +3,7 @@ defmodule MusicLibrary.Collection do
import MusicLibrary.Records, only: [order_alphabetically: 0]
alias MusicLibrary.Records
alias MusicLibrary.Records.{Record, SearchIndex}
alias MusicLibrary.Records.{Record, RecordRelease, SearchIndex}
alias MusicLibrary.Repo
def search_records(query, opts \\ []) do
@@ -121,13 +121,9 @@ defmodule MusicLibrary.Collection do
end
def collected_releases_query do
from r in fragment("records, json_each(records.release_ids)"),
where: fragment("records.purchased_at IS NOT NULL"),
select: %{
record_id: fragment("records.id"),
cover_hash: fragment("records.cover_hash"),
release_id: r.value
}
from rr in RecordRelease,
where: not is_nil(rr.purchased_at),
select: %{record_id: rr.record_id, cover_hash: rr.cover_hash, release_id: rr.release_id}
end
defp base_search do
@@ -0,0 +1,11 @@
defmodule MusicLibrary.Records.RecordRelease do
use Ecto.Schema
@primary_key false
schema "record_releases" do
field :record_id, :binary_id
field :release_id, :string
field :cover_hash, :string
field :purchased_at, :utc_datetime
end
end
+4 -8
View File
@@ -2,7 +2,7 @@ defmodule MusicLibrary.Wishlist do
import Ecto.Query, warn: false
alias MusicLibrary.Records
alias MusicLibrary.Records.SearchIndex
alias MusicLibrary.Records.{RecordRelease, SearchIndex}
alias MusicLibrary.Repo
def search_records(query, opts \\ []) do
@@ -22,13 +22,9 @@ defmodule MusicLibrary.Wishlist do
end
def wishlisted_releases_query do
from r in fragment("records, json_each(records.release_ids)"),
where: fragment("records.purchased_at IS NULL"),
select: %{
record_id: fragment("records.id"),
cover_hash: fragment("records.cover_hash"),
release_id: r.value
}
from rr in RecordRelease,
where: is_nil(rr.purchased_at),
select: %{record_id: rr.record_id, cover_hash: rr.cover_hash, release_id: rr.release_id}
end
defp base_search do