EntityFramework-Extensions icon indicating copy to clipboard operation
EntityFramework-Extensions copied to clipboard

Feat: IncludeInner extension method to force Parent/Children inner joins

Open tsanton opened this issue 1 month ago • 5 comments

This is an ask for an explicit inner join extension to force inner joins between parent and child/children entities in a FK relationship. This feature request is a shameless plagiarization of this (closed) issue from EF-core. Here is a summary of the issue and arguments for from the linked issue:

The relational database model doesn't have a concept of mandatory children since ownership is defined by a foreign key in a child relation. As a consequence, in SQL, Include always has to translate to OUTER JOIN.

There are three reasons why this OUTER JOIN may not always be desired.

1) It's not uncommon for parents without children to be invalid or meaningless. Fetching them from the database by plain SQL would always involve an INNER JOIN because there is foreknowledge of the database content. (Which in such cases will be enforced by client-side validation).
2) Irrespective of database content, developers may want to Include children and at the same time filter only parents with children. Here, INNER JOIN is better than OUTER JOIN + a predicate. Again, in plain SQL that would be a no-brainer.
3) INNER JOINs may perform significantly better than OUTER JOINs, esp. in more complex queries, it would be useful for developers to have control over the type of join EF will execute.

As of now I haven't found anything indicating that the EFcore-team will do anything to implement this feature for EF9. Further they seem very set on not explicitly declaring expected behaviour, but rather having it inferred from configuration. That to me means that I should not be holding my breath for this kind of "performance optimisation easily gained from data model understanding" features from the EFcore-team.

This feature request might be more natural implemented in the EF Plus codebase as I see that's where all the IncludeX extension methods are located, but to be fair this is an optimisation that I:

  1. can't find any implementation for (and I have searched extensibly)
  2. can imagine being highly sought after
  3. would gladly pay for

I am therefore hoping this is an implementation/override of the query pipeline that you guys can easily implement, as it will massively benefit the community of "optimisation focused" query writer, and hopefully bring ZZZ Projects more customers :)

tsanton avatar May 27 '24 07:05 tsanton