NHibernate fetch 创建正确的查询然后执行 N+1
NHibernate fetch creates right query and then performs N+1
我有三个实体,配置为
Contact
HasOne(e => e.User).PropertyRef(e => e.Contact).Cascade.All();
User
HasMany(e => e.Requests);
References(e => e.Contact);
Request
References(e => e.User);
然后我这样查询:
CurrentSession.Query<Request>()
.Fetch(x => x.User)
.ThenFetch(x => x.Contact)
.ToList();
看起来 Fetch 正在运行,因为首先 select 我看到所有表都已连接(为简洁起见省略了其他字段):
SELECT request0_.Id as Id128_0_,
user1_.Id as Id131_1_,
contact2_.Id as Id77_2_,
FROM requests request0_
left outer join users user1_
on request0_.user_id = user1_.Id
left outer join contacts contact2_
on user1_.contact_id = contact2_.Id
然后由于某种原因关注 N+1 selects,我无法理解也不知道如何解决问题:
SELECT user0_.Id as Id131_0_,
FROM users user0_
WHERE user0_.contact_id = 200 /* @p0 - contact_id */
目前它是 NHibernate 中的 bug。
我有三个实体,配置为
Contact
HasOne(e => e.User).PropertyRef(e => e.Contact).Cascade.All();
User
HasMany(e => e.Requests);
References(e => e.Contact);
Request
References(e => e.User);
然后我这样查询:
CurrentSession.Query<Request>()
.Fetch(x => x.User)
.ThenFetch(x => x.Contact)
.ToList();
看起来 Fetch 正在运行,因为首先 select 我看到所有表都已连接(为简洁起见省略了其他字段):
SELECT request0_.Id as Id128_0_,
user1_.Id as Id131_1_,
contact2_.Id as Id77_2_,
FROM requests request0_
left outer join users user1_
on request0_.user_id = user1_.Id
left outer join contacts contact2_
on user1_.contact_id = contact2_.Id
然后由于某种原因关注 N+1 selects,我无法理解也不知道如何解决问题:
SELECT user0_.Id as Id131_0_,
FROM users user0_
WHERE user0_.contact_id = 200 /* @p0 - contact_id */
目前它是 NHibernate 中的 bug。