PragTob
Preload association count/if there are associations records
![]()
Relatively common setup:
I have a Thing which has_many SubThing. In the index view (showing many Things) I want to display a link or a text if there are any “sub things”. Otherwise not.
Of course I could just Repo.preload(:sub_things) but I don’t need them here so I wanna be light on the db/memory etc.
I know that I can write some query that returns me its own data structure telling me which Thing record has how many SubThings (or id any at all) and I could use that second data structure to display/not display them. I don’t have that query but I’m confident that I can write it with relative ease ![]()
I think this is a relatively common problem, so the question is: “Is there a better/standard way to do this?” (my googling came up empty)
If I could wish for something then my Thing struct would have an additional attribute like:
%Thing{
sub_things_exist?: true/false
# or / and
sub_things_count: 5
}
And I could get there with Repo.preload(:sub_things_exist?) or something similar.
Just to be clear I don’t want to introduce a counter cache (yet) it’s fine if it’s still queried from the database dynamically but ideally in one big query (this is on an index page with a lot of elements).
Thanks and cheers as always! ![]()
Marked As Solved
hauleth
You do not need that subquery, if this is just association then:
query = from t in Thing,
join: s in assoc(s, :sub_thing),
group_by: t.id
select_merge: %{sub_thing_count: count(s.id)}
Will work as expected (DB optimisers are good enough to not fetch whole entries.
Also Liked
LostKobrakai
Add field :sub_thing_count, :integer, virtual: true to your schema of Thing and instead of Thing use the following for querying:
sub_query = from s in SubThing, select: map(s, [:id, :thing_id])
query = from t in Thing,
join: s in subquery(sub_query), on: s.thing_id == t.id,
group_by: t.id
select_merge: %{sub_thing_count: count(s.id)}
You could skip the subquery and then join the whole table. What is faster probably depends on the column size of the sub_thing table.
PragTob
Thanks a bunch everyone, haven’t tried it yet but looks like exactly what I need ![]()
LostKobrakai
I’m not sure how it would work in ecto, but lateral joins would basically do what you’re thinking of:
SELECT thing.id, sub_thing.available IS NOT NULL AS sub_thing_available
FROM thing AS thing
LEFT JOIN LATERAL (
SELECT true AS available
FROM sub_thing
WHERE thing_id = thing.id
LIMIT 1
) AS sub_thing ON true
Popular in Questions
Other popular topics
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









