I'm trying to delete all rows in a table using Castle ActiveRecord. Normally, I would do this:
DB.ParseError.DeleteAll();
However, this table happens to have somewhere around 1.9 million rows in it. What the above command will do is issue a separate SELECT
and DELETE
for each row in the table, looking something like this:
SELECT page0_.Id as Id29_0_, ... FROM Indexer.Pages page0_ WHERE page0_.Id=:p0;:p0 = b03665aa-37d0-4a2d-a04c-c232ebd94dbc [Type: Guid (0)]
SELECT page0_.Id as Id29_0_, ... FROM Indexer.Pages page0_ WHERE page0_.Id=:p0;:p0 = 11cb69e3-1c6a-4ac1-908b-084dfe859639 [Type: Guid (0)]
--- 1.9 million more of these
DELETE FROM Indexer.ParseErrors WHERE Id = :p0;:p0 = b03665aa-37d0-4a2d-a04c-c232ebd94dbc [Type: Guid (0)]
DELETE FROM Indexer.ParseErrors WHERE Id = :p0;:p0 = 11cb69e3-1c6a-4ac1-908b-084dfe859639 [Type: Guid (0)]
--- 1.9 million more of these
This takes... well I don't know how long it takes because I don't have the patience to actually let it finish.
What I want it to do is issue one SQL statement; this one:
DELETE FROM Indexer.ParseErrors;
Here's what I've tried so far:
ISessionFactoryHolder holder = ActiveRecordMediator.GetSessionFactoryHolder();
ISession session = holder.CreateSession(typeof(DB.ParseError));
IDbCommand cmd = session.Connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "DELETE FROM Indexer.ParseErrors;";
cmd.CommandTimeout = 600;
cmd.ExecuteNonQuery();
holder.ReleaseSession(session);
This appears to work, however it seems like a hack to me as it completely avoids the model and ActiveRecord framework. Plus, this probably doesn't play well with any caching NHibernate has.
Is there a more official way to delete a table, without doing 2 SQL statements per row?