more aggressive preloading

This commit is contained in:
byte[] 2019-12-22 18:42:07 -05:00
parent a39adcb9d5
commit 556cd39afd
5 changed files with 34 additions and 11 deletions

View file

@ -101,17 +101,21 @@ defmodule Philomena.Textile.Renderer do
end
defp find_images(text_segments) do
image_ids =
text_segments
|> Enum.flat_map(fn t ->
Regex.scan(~r|>>(\d+)|, t, capture: :all_but_first)
|> Enum.map(fn [first] -> String.to_integer(first) end)
end)
text_segments
|> Enum.flat_map(fn t ->
Regex.scan(~r|>>(\d+)|, t, capture: :all_but_first)
|> Enum.map(fn [first] -> String.to_integer(first) end)
end)
|> find_image_ids()
end
defp find_image_ids([]), do: %{}
defp find_image_ids(image_ids) do
Image
|> where([i], i.id in ^image_ids)
|> where([i], i.hidden_from_users == false)
|> preload(:tags)
|> join(:left, [i], _ in assoc(i, :tags))
|> preload([_i, t], tags: t)
|> Repo.all()
|> Map.new(fn image -> {image.id, image} end)
end

View file

@ -3,6 +3,15 @@ defmodule PhilomenaWeb.CommentLoader do
alias Philomena.Repo
import Ecto.Query
# More aggressive preloading skipped due to overhead
# from scrivener:
#
# |> join(:left, [c], _ in assoc(c, :image))
# |> join(:left, [c, _i], _ in assoc(c, :user))
# |> join(:left, [_c, _i, u], _ in assoc(u, :awards))
# |> join(:left, [_c, _i, _u, a], _ in assoc(a, :badge))
# |> preload([_c, i, u, a, b], [:deleted_by, image: i, user: {u, awards: {a, badge: b}}])
def load_comments(conn, image) do
pref = load_direction(conn.assigns.current_user)

View file

@ -147,8 +147,12 @@ defmodule PhilomenaWeb.ImageController do
|> where(id: ^id)
|> join(:inner_lateral, [i], _ in fragment("SELECT COUNT(*) FROM tag_changes t WHERE t.image_id = ?", i.id))
|> join(:inner_lateral, [i, _], _ in fragment("SELECT COUNT(*) FROM source_changes s WHERE s.image_id = ?", i.id))
|> preload([:tags, :deleter, user: [awards: :badge]])
|> select([i, t, s], {i, t.count, s.count})
|> join(:left, [i, _tc, _sc], _ in assoc(i, :user))
|> join(:left, [_i, _tc, _sc, u], _ in assoc(u, :awards))
|> join(:left, [_i, _tc, _sc, _u, a], _ in assoc(a, :badge))
|> join(:left, [i, _tc, _sc, _u, _a, _b], _ in assoc(i, :tags))
|> preload([_i, _tc, _sc, u, a, b, t], [:deleter, tags: t, user: {u, awards: {a, badge: b}}])
|> select([i, tc, sc, _u], {i, tc.count, sc.count})
|> Repo.one()
|> case do
nil ->

View file

@ -20,7 +20,7 @@ defmodule PhilomenaWeb.ImageLoader do
sort_queries = Keyword.get(options, :queries, [])
sort_sorts = Keyword.get(options, :sorts, [%{created_at: :desc}])
pagination = Keyword.get(options, :pagination, conn.assigns.image_pagination)
queryable = Keyword.get(options, :queryable, Image |> preload(:tags))
queryable = Keyword.get(options, :queryable, default_queryable())
user = conn.assigns.current_user
filter = conn.assigns.compiled_filter
@ -126,4 +126,10 @@ defmodule PhilomenaWeb.ImageLoader do
[{tag, description, dnp_entries}]
end
defp render_bodies(tags, _conn), do: tags
defp default_queryable do
Image
|> join(:left, [i], _ in assoc(i, :tags))
|> preload([_i, t], tags: t)
end
end