Reputation: 45
Fetching + Deleting an object with NHibernate is extremely slow if it is a more complex object and the database has some data in it already.
Example:
- ObjectA (about 150.000 entries in database)
- ObjectB referring to ObjectA (about 200.000 entries in database)
- ObjectC (one to one relationship to ObjectA)
- ObjectD (many to one relationship to ObjectC, about 800.000 entries)
Deleting ObjectA also deletes all other Objects listed above. Deleting 100 ObjectA takes about 12 seconds. What could be the problem, that NHibernate is that slow?
If I delete the same amount of object on an empty database, it only takes about 1-2 seconds.
I already tried to Flush + Clear my Session after about 20 - 30 objects, but that does not make a huge difference.
Upvotes: 1
Views: 1339
Reputation: 123861
The deletion of 100 objects, would be slower with NHibernate, than executing direct execution of the DELETE statement (on empty database). There is alsways some overhead during objects conversion to the C# entities.
But NHibernate has nice solution for these scenarios, when we need to delete dozens of objects - without loading them into the Application: DML
An extract of the 13.3. DML-style operations
...hence manipulating (using the SQL Data Manipulation Language (DML) statements: INSERT, UPDATE, DELETE) data directly in the database will not affect in-memory state. However, NHibernate provides methods for bulk SQL-style DML statement execution which are performed through the Hibernate Query Language (HQL).
the code example from documentation:
ISession session = sessionFactory.OpenSession();
ITransaction tx = session.BeginTransaction();
string hqlDelete = "delete ObjectA a where a.property = :someValue";
int deleteddEntities = s.CreateQuery( hqlDelete )
.SetString( "someValue", someValue)
.ExecuteUpdate();
tx.Commit();
session.Close();
We have in the HQL statement full access to mapped properties. But the statement is executed directly on a DB Engine...
Upvotes: 2