views:

323

answers:

2

In my application I would like to clear/empty a table (which is the only one) in my SQLite-DB. I am programming in C#. _session is of type NHibernate.ISession. Here is my code:

string queryFmt = "FROM {0}"; string query = String.Format(queryFmt, typeName); _session.Delete(query);
_session.Flush();

My example-DB consists of over 5000 entries (the s3db-file is about 750KB big). The Flush()-Method needs more than 6 minutes. (When I execute the delete-operation in the SQLite Administrator it takes less than a second.)

How can I empty the table faster?

A: 

If you set nhib to show_sql=true in the config, what sql is output?


After your response, you can see that nhib is taking so long because it's not bulk deleting the rows.

Ayende has a post explaining it here.

Chad Ruppert
for _session.Delete(...): NHibernate: select logentry0_.Id as Id22_, logentry0_.Message as Message22_, logentry0_.Type as Type22_, logentry0_.Category as Category22_, logentry0_.DateCreated as DateCrea5_22_, logentry0_.Level as Level22_ from Log logentry0_ ============ for _session.Flush(): NHibernate: DELETE FROM Log WHERE Id = @p0;@p0 = 1 NHibernate: DELETE FROM Log WHERE Id = @p0;@p0 = 2 ... NHibernate: DELETE FROM Log WHERE Id = @p0;@p0 = 99 NHibernate: DELETE FROM Log WHERE Id = @p0;@p0 = 100
toni
+2  A: 

Use an ExecuteUpdate on a hql query Here's an example:

using(var session = sessionFactory.OpenSession())
{

    String hqlDelete = string.Format("delete {0} t",typename);
    int deletedEntities = session.CreateQuery( hqlDelete ).ExecuteUpdate();
    session.Close();
}
Beatles1692
one minor change: it's "session.CreateQuery..." and not "hqlDelete.CreateQuery..."
toni
That's right I edited that . Thanks :)
Beatles1692