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

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14403 124
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement