Ecto alternative of find_in_batches?

Hi,

What’s the simplest way of going over an extremely large set of records in a database? Is there something like Rails’ find_in_batches in Ecto?

3 Likes

Ecto.Repo.stream/2. You can see in the docs it takes a :max_rows of 500, which is the same as the batch size in find_in_batches.

5 Likes
result = Repo.transaction(fn() ->
  your_ecto_query_here
  |> Repo.stream
  |> Enum.each(fn(one_record) ->
    # do something with one record here.
  end)
end)

case result do
  {:ok, success} -> # ...
  {:error, reason} -> # ...
end
4 Likes