Session.Get 加载 N+1,即使 lazy=false
本文关键字:lazy false 即使 Get 加载 N+1 Session | 更新日期: 2023-09-27 17:56:10
我有这样的实体:
public class User
{
public virtual int Id { get; set;}
public virtual Iesi.Collections.Generic.ISet<Character> Characters { get; set; }
}
public class Character
{
public virtual int Id { get; set;}
public virtual User User { get; set;}
public virtual Iesi.Collections.Generic.ISet<UserCharacterSmartChallengeTracker> SmartChallengeTrackers { get; set; }
}
public class UserCharacterSmartChallengeTracker
{
public virtual int Id { get; set; }
public virtual int? CharacterId { get; set; }
}
他们都不是懒惰加载的。
当我这样做session.Get<User>()
时,我看到这样的查询:
SELECT smartchall0_.character_id as character3_1_,
smartchall0_.id as id1_,
smartchall0_.id as id51_0_,
smartchall0_.character_id as character3_51_0_,
FROM public.user_character_smart_challenge_trackers smartchall0_
WHERE smartchall0_.character_id = 48176 /* :p0 */
SELECT smartchall0_.character_id as character3_1_,
smartchall0_.id as id1_,
smartchall0_.id as id51_0_,
smartchall0_.character_id as character3_51_0_,
FROM public.user_character_smart_challenge_trackers smartchall0_
WHERE smartchall0_.character_id = 48175 /* :p0 */
-- and others
我尝试将它们全部预加载到会话缓存中:
var ids = session.Query<Character>().Where(x => x.User.Id == id)
.Select(x => x.Id)
.ToArray();
session.Query<UserCharacterSmartChallengeTracker>().Where(x => ids.Contains(x.Id)).ToArray();
带查询
select character0_.id as col_0_0_
from public.characters character0_
where character0_.user_id = 9602 /* :p0 */
select usercharac0_.id as id51_,
usercharac0_.character_id as character3_5
from public.user_character_smart_challenge_trackers usercharac0_
where usercharac0_.id in (48176 /* :p0 */, 48175 /* :p1 */, 48174 /* :p2 */, 48173 /* :p3 */,
48172 /* :p4 */, 48171 /* :p5 */, 48170 /* :p6 */, 48169 /* :p7 */)
但是 NHibernate 忽略了它们都已加载到会话缓存中并生成相同的 N+1 查询的事实!如何解决这个问题?
更新:预加载
session.QueryOver<Character>().Where(x => x.User.Id == id)
.Fetch(x => x.User).Lazy
.Fetch(x=>x.SmartChallengeTrackers).Eager
.List();
删除 N+1,但在我做我想避免session.Get<User>
时第二次加载 NHibernate 字符!
我使用了期货:
var q = session.Query<User>().Where(x => x.Id == id);
var lst = new List<IEnumerable>
{
q.FetchMany(x => x.Characters).ToFuture(),
q.Fetch(x=>x.UpdateableData).ToFuture(),
session.QueryOver<User>().Where(x => x.Id == id)
.Fetch(x=>x.Characters).Eager
.Fetch(x => x.Characters.First().SmartChallengeTrackers).Eager
.Future()
};
var r = session.QueryOver<User>().Where(x => x.Id == id)
.TransformUsing(Transformers.DistinctRootEntity)
.Future();
foreach (IEnumerable el in lst)
{
foreach (object o in el)
{
}
}
return r.ToArray();