Join on preloads to avoid N+1 queries
This commit is contained in:
parent
6383fa3a5d
commit
8018ae7ae5
|
@ -35,7 +35,8 @@ def for_user(user, opts \\ %{}) do
|
||||||
n in Notification,
|
n in Notification,
|
||||||
where: n.user_id == ^user.id,
|
where: n.user_id == ^user.id,
|
||||||
order_by: [desc: n.id],
|
order_by: [desc: n.id],
|
||||||
preload: [:activity],
|
join: activity in assoc(n, :activity),
|
||||||
|
preload: [activity: activity],
|
||||||
limit: 20
|
limit: 20
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -66,7 +67,8 @@ def get(%{id: user_id} = _user, id) do
|
||||||
from(
|
from(
|
||||||
n in Notification,
|
n in Notification,
|
||||||
where: n.id == ^id,
|
where: n.id == ^id,
|
||||||
preload: [:activity]
|
join: activity in assoc(n, :activity),
|
||||||
|
preload: [activity: activity]
|
||||||
)
|
)
|
||||||
|
|
||||||
notification = Repo.one(query)
|
notification = Repo.one(query)
|
||||||
|
|
|
@ -33,7 +33,12 @@ def call(conn, _) do
|
||||||
#
|
#
|
||||||
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
|
@spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil
|
||||||
defp fetch_user_and_token(token) do
|
defp fetch_user_and_token(token) do
|
||||||
query = from(q in Token, where: q.token == ^token, preload: [:user])
|
query =
|
||||||
|
from(t in Token,
|
||||||
|
where: t.token == ^token,
|
||||||
|
join: user in assoc(t, :user),
|
||||||
|
preload: [user: user]
|
||||||
|
)
|
||||||
|
|
||||||
with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
|
with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do
|
||||||
{:ok, user, token_record}
|
{:ok, user, token_record}
|
||||||
|
|
Loading…
Reference in New Issue