roganjoshua
Scenario
Players may belong to many games and each game may have multiple players.
When I go to my games I want to see all my games and who else is in those games.
Ideally in a single query.
I have a PlayersGames many to many junction table.
I have this which seems to work:
subquery =
from ug in UsersGames,
where: ug.user_id == ^user_id,
select: ug.game_id
query =
from user in User,
join: g in assoc(user, :games),
where: g.id in subquery(subquery),
distinct: true,
select: g
Repo.all(query) |> Repo.preload(:users)
I don’t like the distinct and I wonder if this is optimal.
Otherwise is this a fairly common scenario? and is this the solution?
I am just a beginner.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Other Trending Topics
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
krasenyp
I’d keep the
subquerybut thequerywould be:In the end, you want a list of games so in order to avoid
distinct, select a list of games, not users. By preloading in the query expression, you avoid an additional query (Repo.preload).I used the more verbose version of the
QueryDSL in order to be more explicit about the bindings.Asd
No need for subquery, I think, but I’d check the execution plans to pick the best query.
jdiago
Someone correct me if I’m wrong but I’m pretty sure using a subquery in your query technically counts as 2 queries in postgres.
Either way, I wouldn’t worry so much if ecto spits out 1 query or multiple if I was a beginner. Just do the simple:
Repo.all(Game) |> Repo.preload(:users)assuming theGameschema hasmany_to_many :users, User, join_through: UsersGames.The 1 query version would be:
krasenyp
Your examples don’t filter the games in any way. OP wants to list the games a particular user has played and all the other player is those games.
krasenyp
I believe this query will return only one user for every game. Putting
wherein between the joins won’t make any difference as SQL will order the directives the right way.I might be wrong, of course.
Asd
No, it won’t. Database will return a table where
gamesanduserstables are joined, and then Ecto will group them up bygames.idand users will be accumulated intousersfield (thats how preload works in Ecto)I’ve tested it out in sample project and something like this was returned. Users 1 to 5 were playing in games from 1 to 5 and users 6, 7 and 99 joined the game 5 too.
Exactly right, I did it for readability.
jdiago
True. I misunderstood.
To filter the games by user_id, this seems to work:
Ecto comes out with 1 query. No sub query:
edit:
This can refactor down to:
Almost the same as the solution from @Asd but it doesn’t need an explicit
:users_gamesassoc in the Game schema.roganjoshua
Excellent, I knew it is was sub-optimal which is as much an SQL as an ecto thing.
Reading that now actually looks lovely.
sodapopcan
I know you know this but for those who don’t, you do have to be careful using this if you are doing it with a lot more data or more joins. It can end up in a massive table that needs to be sent over the wire then reduced in Elixir which gets slower and slower. One query isn’t always better!