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
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).
1
Popular in Discussions
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
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
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
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
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...
New
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
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
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...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
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
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
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









