Ecto.Preloader v0.1.2 Ecto.Preloader View Source
Ecto.Preloader
is a module for preloading associations using joins.
By default, Ecto preloads associations using a separate query for each association, which can degrade performance.
You could make it run faster by using a combination of join/preload, but that requires a bit of boilerplate (see example below).
With Ecto.Preloader
, you can accomplish this with just one line of code.
Example using just Ecto
It requires calling Query.join/4
, Query.assoc/3
and Query.preload/2
import Ecto.Query
Invoice
|> join(:left, [i], assoc(i, :customer), as: :customer)
|> join(:left, [i], assoc(i, :lines), as: :lines)
|> preload([lines: l, customers: c], lines: l, customer: c)
|> Repo.all()
Example using Ecto.Preloader
Just one method call:
import Ecto.Query
import Ecto.Preloader
Invoice
|> preload_join(:customer)
|> preload_join(:lines)
|> Repo.all()