nightfury17200
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?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
kokolegorille
What was the error message?
nightfury17200
no error message the code just did nothing.
al2o3cr
Re: your specific question:
Stream.eachdoesn’t run any computation, you have to call a function likeEnum.to_listorStream.runto makeStreamdo things.A more general thought: this code returns all the rows from
querywhich neutralizes a lot of the benefit from streaming;Stream.runwill do the computation but ignore the result, if that’s your intent.nightfury17200
I read about what stream.run do and what the documentation mentioned was-
to be honest I was not able to make any sense of it hence I tried using stream.each and thought it would be similar to enum.each and would call a function for each value. Can you please explain how stream.run works?
nightfury17200
I tried using stream.run assuming that it makes all the stream command inputted in it work. the code I tried is-
but it does not work. can you point out where I am making the mistake?
al2o3cr
Stream.eachreturns a new stream that calls the supplied function as each element is generated. It doesn’t iterate by itself.Example:
Note that in the second line,
streamis bound as aStreamstruct but nothing has beenIO.inspect-ed yet.Stream.runcauses the stream to compute values but discards them and returns:ok, whileEnum.to_listaccumulates and returns the values.nightfury17200
then shouldn’t the code above your reply run here I have added
Stream.run. so first I will make chunks of data withStream.chunk_every(@query_batch)and then pass it to Stream.each for going to each chunk and then for each chunk I will pass it toStream.eachso that I can iterate over the records and then call the required function for each record, and lastly all of this is passed tostream.run()to implement this. Sorry If my understanding is incorrect.benwilson512
Your overall stream does, but you’re doing another
Stream.eachinside aStream.eachand that inner one isn’t doing anything:The inner
Stream.eachshould probably be anEnum.eachsince thexlist is already loaded into memory, streaming here doesn’t accomplish anything.nightfury17200
ok, understood. thanks