nightfury17200

nightfury17200

Operating on large amount of data

hello all, I want to operate on large amount of data where I pass a query to get some records and then for each record I have to perform a costly operation (creating large number of redis keys(~1000) for each record data and then deleting it) so here I want to use a stream to make the strain less. My code till now is-

      Repo.transaction(
        fn ->
          query
          |> Repo.stream(max_rows: @query_batch)
          |> Stream.chunk_every(@query_batch)
          |> Stream.each(fn x ->
             Enum.to_list(x) |> Enum.each(fn y -> delete_from_redis(y) end)
          end)
          |> Enum.to_list()
        end,
        timeout: :infinity
      )

here in this code when I tried to do -

Stream.each(x, fn y -> delete_from_redis(y) end)

in place of-

Enum.to_list(x) |> Enum.each(fn y -> delete_from_redis(y) end)

my code did not worked, please explain the reason for that also please tell me what can I do to make my current code more efficient?

Marked As Solved

al2o3cr

al2o3cr

Stream.each returns a new stream that calls the supplied function as each element is generated. It doesn’t iterate by itself.

Example:

iex(1)> a = [1,2,3]
[1, 2, 3]

iex(2)> stream = Stream.each(a, &IO.inspect/1)
#Stream<[enum: [1, 2, 3], funs: [#Function<38.58486609/1 in Stream.each/2>]]>

iex(3)> Stream.run(stream)
1
2
3
:ok

iex(4)> Enum.to_list(stream)
1
2
3
[1, 2, 3]

Note that in the second line, stream is bound as a Stream struct but nothing has been IO.inspect-ed yet.

Stream.run causes the stream to compute values but discards them and returns :ok, while Enum.to_list accumulates and returns the values.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Your overall stream does, but you’re doing another Stream.each inside a Stream.each and that inner one isn’t doing anything:

          |> Stream.each(fn x ->
             Stream.each(x, fn y -> delete_from_redis(y) end)
          end)

The inner Stream.each should probably be an Enum.each since the x list is already loaded into memory, streaming here doesn’t accomplish anything.

al2o3cr

al2o3cr

Re: your specific question:

Stream.each doesn’t run any computation, you have to call a function like Enum.to_list or Stream.run to make Stream do things.

A more general thought: this code returns all the rows from query which neutralizes a lot of the benefit from streaming; Stream.run will do the computation but ignore the result, if that’s your intent.

Last Post!

nightfury17200

nightfury17200

ok, understood. thanks

Where Next?

Trending in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
tj0
I’ve been following the steps here for the upgrade from 1.6 to 1.7 and it has gone relatively smoothly all the way till the phoenix_view ...
New
cgraham
Hi! What is currently the best library/method for parsing text and tabular data out of PDF files in Elixir or Erlang?
New
stefanchrobot
Hi, I need a way to handle data migrations in my application. I found an article by @wojtekmach about manual migrations: Automatic and ma...
New
stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
webofbits
Squid Mesh is an open source workflow automation runtime for Elixir applications. It is aimed at Phoenix and OTP apps that want to defin...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
kip
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New

We're in Beta

About us Mission Statement