【发布时间】:2014-09-28 00:03:18
【问题描述】:
我发现级联删除正在发送带有许多单独删除的 SQL,即 N+1 问题。
我有三个表:User、UserAttribute 和 Attribute。每个 User 有多个 UserAttribute,每个 UserAttribute 有一个 Attribute - 由以下 Maps 表示:
public UserMap() {
Id(x => x.Id);
Map(x => x.Description);
HasMany(x => x.Attributes)
.Inverse()
.Cascade.AllDeleteOrphan();
}
public UserAttributeMap() {
Id(x => x.Id);
References(x => x.User)
.Not.Nullable();
References(x => x.Attribute)
.Not.Nullable()
.Cascade.All();
}
public AttributeMap() {
Id(x => x.Id);
Map(x => x.Name)
.Unique();
}
在上面的测试场景中,我的架构是使用 FluentNH 生成的。
我遇到的问题是,当我删除一个用户实体时,级联会为每个用户属性和每个属性生成一个单独的删除;这是一个潜在的大性能问题(一个用户通常可能有数百个属性)。
映射是否有明显的问题会在这里触发 N+1 问题?我能做些什么来阻止它吗?这是疯话吗?
附带说明一下,我的 NHibernate 配置已经包含大批量大小,但据我所知,批处理不会像这样结束?当我对它们进行分析时,它们肯定不会被批量处理。
NHibernate v3.3.1.4000
FluentNHibernate v 1.4.0.0
UserTable:
Id
Description
UserAttributeTable:
Id
User_id (FK)
Attribute_id (FK)
AttributeTable:
Id
Name
【问题讨论】:
标签: nhibernate fluent-nhibernate cascade fluent fluent-nhibernate-mapping