RudManusachi

RudManusachi

Aggregation in application vs using DB aggregate operations

TL;DR would you prefer to aggregate result_set in DB or application level?

Recently I learned about array aggregate operators such as array_agg in Postgres and json_arrayagg in MySQL.
So in dummy example, suppose we have a schema posts where each post has author_id as a foreign key. I want to get a map with author_ids as keys and the list of related post ids as a value:

from(p in Post, select: {p.author_id, p.id})
|> Repo.all()
|> Enum.reduce(%{}, fn {author_id, post_id}, acc -> 
  Map.update(acc, author_id, [post_id], fn post_ids -> [post_id | post_ids] end) 
end)

or we could

from(p in Post, group_by: p.author_id, select: {p.author_id, fragment("array_agg(?)", p.id)})
|> Repo.all()
|> Enum.into(%{})

Most Liked

hauleth

hauleth

In this case? 100% on the DB side. It probably will be much faster that way as you will not need to send repeated author_id over the wire. Also it should utilise indices better (of course if you have one in your DB).

Where Next?

Popular in Discussions Top

joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New
acrolink
How does the two languages compare when it comes to server side application development? Any experiences or ideas? Thank you.
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
thojanssens1
It would be nice to be able to define a redirect from one route to another from the router.ex file. E.g.: redirect "/", UserController, ...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement