-
Notifications
You must be signed in to change notification settings - Fork 934
One to One constrained relation with PropertyRef causes N+1 #3292
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
Comments
I think this issue is similar to #2716 To auto join one-to-one associations, we need some logic that checks all mapping associations for loading entities and adds missing required joins. Such logic is implemented for Criteria/QueryOver and EntityLoader(session.Get/lazy loading)). But not implemented for LINQ. So there is a big chance that's QueryOver version |
I have checked QueryOver too. It behaves exactly the same: [Test]
public void QueryOver()
{
using var session = sessionFactory.OpenSession(new LoggingInterceptor());
var entities = session.QueryOver<EntityOne>().List();
} Query does: select entityone0_.Id as id1_1_, entityone0_.Name as name2_1_ from [EntityOne] entityone0_
SELECT entitytwo0_.Id as id1_2_0_, entitytwo0_.Name as name2_2_0_, entitytwo0_.EntityOne_id as entityone3_2_0_ FROM [EntityTwo] entitytwo0_ WHERE entitytwo0_.EntityOne_id=?
SELECT entitytwo0_.Id as id1_2_0_, entitytwo0_.Name as name2_2_0_, entitytwo0_.EntityOne_id as entityone3_2_0_ FROM [EntityTwo] entitytwo0_ WHERE entitytwo0_.EntityOne_id=?
SELECT entitytwo0_.Id as id1_2_0_, entitytwo0_.Name as name2_2_0_, entitytwo0_.EntityOne_id as entityone3_2_0_ FROM [EntityTwo] entitytwo0_ WHERE entitytwo0_.EntityOne_id=? And query over: SELECT this_.Id as id1_1_0_, this_.Name as name2_1_0_ FROM [EntityOne] this_
SELECT entitytwo0_.Id as id1_2_0_, entitytwo0_.Name as name2_2_0_, entitytwo0_.EntityOne_id as entityone3_2_0_ FROM [EntityTwo] entitytwo0_ WHERE entitytwo0_.EntityOne_id=?
SELECT entitytwo0_.Id as id1_2_0_, entitytwo0_.Name as name2_2_0_, entitytwo0_.EntityOne_id as entityone3_2_0_ FROM [EntityTwo] entitytwo0_ WHERE entitytwo0_.EntityOne_id=?
SELECT entitytwo0_.Id as id1_2_0_, entitytwo0_.Name as name2_2_0_, entitytwo0_.EntityOne_id as entityone3_2_0_ FROM [EntityTwo] entitytwo0_ WHERE entitytwo0_.EntityOne_id=? Double checked with SQL Server Profiler. |
Ok. Then nhibernate is not smart enough and only explicit |
@bahusoid you are right. Adding |
There is no roadmap. You need it you do it and contribute it. That's how it works now. I can work on it but only if someone is sponsoring this job. But it's not a trivial task. Another way to solve n+1 issue is to enable batch loading for your one-to-one entity (like adding |
Thanks for your insights. |
Consider code bellow:
With schema and data:
When test is executed, four queries can be found in test output: one to retrieve records from "EntityOne" table, and three queries for each "EntityTwo".
If
.PropertyRef(x => x.EntityOne)
is removed, only one query is made. And now, if.Fetch(e => e.EntityTwo)
is added, query with join clause is made, however joins are happening on id fields, not EntityOne.Id = EntityTwo.EntityOne_id.Is this how
.Constrined()
with.PropertyRef()
supposed to work? If so, can limitation not be able join on specified field be explained?The text was updated successfully, but these errors were encountered: