Ecto alternative of find_in_batches?

ecto
Tags: #<Tag:0x00007fbcac24a2a0>

#1

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?


#2

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.


#3
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