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
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2667,6 +2667,30 @@ 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!

josevalim marked this conversation as resolved.
Show resolved Hide resolved

Deciding between preloading associations via joins, a single large
query, (`preload: [comments: c]`) or separate smaller queries
(`preload: [:comments]`) depends on the specific use case.
Here are some factors to guide your decision:

* **Joins reduce database round trips:** By fetching data in a single
query, joins can minimize database round trips, potentially reducing
overall latency.
* **Potential for data duplication:** Joins may lead to duplicated
data in the result set, which requires more processing by Ecto
and consumes more bandwidth when transmitting the results.
* **Parallelism with separate queries:** When using separate queries
outside of a transaction, Ecto can parallelize the preload queries,
which can speed up the overall operation.

In general, a good default is to only use joins in preloads if you're
already joining the associations in the main query. For example,
in the last query in the section above, comments and likes are already
joined, so they are included in the preload.
However, the author is not joined in the main query, so it is preloaded
via a separate query.
greg-rychlewski marked this conversation as resolved.
Show resolved Hide resolved

## Preload queries

Preload also allows queries to be given, allowing you to filter or
Expand Down
Loading