I'm finding that cascading deletes is sending SQL with many individual deletes, i.e. the N+1 problem.
I have three tables: User, UserAttribute and Attribute. Each User has many UserAttributes, each UserAttribute has one Attribute - represented by the following 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();
}
In the above test scenario my schema is then generated using FluentNH.
The problem I'm experiencing is that when I delete a User entity, the cascade generates an individual delete for each UserAttribute and each Attribute; which is a potentially large performance problem (A User could typically have hundreds of Atributes).
Is there something obviously wrong with the mapping that would be triggering the N+1 problem here? Is there anything I can do to stop it? Is this crazy talk?
As a side note, my NHibernate configuration already includes a large batch size, but as far as I'm aware batching won't wrap up something like this? They certainly don't appear to be batched when I profile them.
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