entc/gen: use join for loading m2m relationship #2417
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rewrite the way Ent eager-loads M2M edges, by replacing two queries with a single JOIN query. The main reasons are:
entgql
in order to use SQLWINDOW
functions and limit the number of edges returned for each entity.With<E>
configured withLimit
,Order
orWhere
).JOIN
to the destination table which makes it irrelevant.Test plan
Added 10K users, and connected each one of them to a unique group with attribute
active=false
. Then, created another 10 groups (withactive=true
) and connected them to all users.The results are as follows:
When querying
users->groups
, the new JOIN ofusers.id
+groups
columns generate 99990 duplicate rows, unlike the previous solution, that used 2 queries. One for getting all edges (without filtering) from join-table, and another one for querying the destination table (the one requested onWith
).However, in cases like
groups(active:false)
when there are no duplicate rows at all, the numbers are much better.Future work
If we'll feel it is necessary, we can provide users an option to configure the way relationships are queried using "runtime annotation/hints". Some rough ideas I have in mind: