Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include a preload joins vs. no joins discussion in the preload docs #4517

Merged
merged 3 commits into from
Oct 7, 2024

Conversation

PragTob
Copy link
Contributor

@PragTob PragTob commented Oct 2, 2024

I'm not sure if Ecto wants to provide this level of guidance in the docs, so I understand if this PR won't be merged.

I've just seen many well-meaning people do this wrong (imo) by defaulting to joining all associations just to preload them, resulting in a lot more code and potentially worse performance.

I also vaguely remember a tweet from José calling this out, but couldn't find it again.

Here I tried to summarize what I think I know about the topic, hoping that it's accurate and helpful.

As usual, thank you all very much for your work! 💚

@@ -2667,6 +2667,34 @@ defmodule Ecto.Query do
where: l.inserted_at > c.updated_at,
preload: [:author, comments: {c, likes: l}]

## Choosing Between Preloading with Joins vs. Separate Queries
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure whether to make it a separate section with a heading or not, but I ended up doing it because it got too big to just be part of the normal text.

I also put it "first" as it seemed most relevant to the preceeding setion flow-wise. Happy to re-evaluate and change!

lib/ecto/query.ex Outdated Show resolved Hide resolved
I'm not sure if Ecto wants to provide this level of guidance in
the docs, so I understand if this PR won't be merged.

I've just seen many well-meaning people do this wrong (imo) by
defaulting to joining all associations just to preload them,
resulting in a lot more code and potentially worse performance.

I also vaguely remember a tweet from José calling this out,
but couldn't find it again.

Here I tried to summarize what I think I know about the topic,
hoping that it's accurate and helpful.
@PragTob PragTob force-pushed the preload-join-vs-no-join branch from 1175eac to a38584c Compare October 7, 2024 11:32
@PragTob
Copy link
Contributor Author

PragTob commented Oct 7, 2024

Updated, removed section about database load. Thanks for the feedback everyone!

lib/ecto/query.ex Outdated Show resolved Hide resolved
@josevalim josevalim merged commit 23da400 into elixir-ecto:master Oct 7, 2024
6 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@PragTob PragTob deleted the preload-join-vs-no-join branch October 7, 2024 20:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants