I'm using the code below to bulk insert a 30000 rows (1000 rows at a time). Still it is not as fast as it could be. In this example Improve INSERT-per-second performance of SQLite? I can see that they are creating the SqliteCommand
only once and then reycle it be resetting it and clearing the bindings. However, I cannot find the apropriate methods on iOS/Monotouch. There is no Reset()
or ClearBindings()
or anything else looking similar.
using ( var oConn = new SqliteConnection ( "Data Source=" + DB_NAME ) )
{
oConn.Open ( );
// Wrap the whole bulk insertion into one block to make it faster, otherwise one transaction per INSERT would be created.
// Note that this is differen from "BEGIN TRANSACTION" which would increase memory usage a lot!
SqliteCommand oCmd = new SqliteCommand ( "BEGIN", oConn );
oCmd.ExecuteNonQuery ( );
oCmd.Dispose ( );
foreach ( MyObj oObj in aMyObjects )
{
oCmd = new SqliteCommand ( "INSERT INTO LocalObjects ( intID, intParentID, intObjectType, strName, dtModified VALUES (@intID, @intParentID, @intObjectType, @strName, @dtModified)", oConn );
oCmd.Parameters.AddWithValue ( "@intID", oMyObj.ID );
oCmd.Parameters.AddWithValue ( "@intParentID", oMyObj.ParentID );
oCmd.Parameters.AddWithValue ( "@intObjectType", ( int ) oMyObj.Type );
oCmd.Parameters.AddWithValue ( "@strName", oMyObj.Name );
oCmd.Parameters.AddWithValue ( "@dtModified", oMyObj.Modified );
oCmd.ExecuteNonQuery ( );
oCmd.Dispose ( );
}
oCmd = new SqliteCommand ( "END", oConn );
oCmd.ExecuteNonQuery ( );
oCmd.Dispose ( );
oConn.Close ( );
oConn.Dispose ( );
}