如何回收我的 SqliteCommand 以加速此 Sqlite 批量插入 (iOS)? [英] How can I recycle my SqliteCommand to speed up this Sqlite bulk insert (iOS)?

查看:21
本文介绍了如何回收我的 SqliteCommand 以加速此 Sqlite 批量插入 (iOS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码批量插入 30000 行(一次 1000 行).尽管如此,它仍然没有想象的那么快.在这个例子中 提高 SQLite 每秒插入的性能? 我可以看到他们只创建了一次 SqliteCommand,然后重新设置它并清除绑定.但是,我在 iOS/Monotouch 上找不到合适的方法.没有 Reset()ClearBindings() 或其他类似的东西.

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 (  );
}

推荐答案

尝试将您的代码更改为以下内容:

Try to change your code to the following:

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 (  );

    oCmd = new SqliteCommand ( "INSERT INTO LocalObjects ( intID, intParentID, intObjectType, strName, dtModified VALUES (@intID, @intParentID, @intObjectType, @strName, @dtModified)", oConn );

    // <do this for all of your parameters>.
    var id = oCmd.CreateParameter();
    id.ParameterName = "@intID";
    oCmd.Parameters.Add(id);
    // </do this for all of your parameters>.

    foreach ( MyObj oObj in aMyObjects )
    {
        // <do this for all of your parameters>.
        id.Value = oMyObj.ID;
        // </do this for all of your parameters>.

        oCmd.ExecuteNonQuery (  );
    }

    oCmd.Dispose();

    oCmd = new SqliteCommand ( "END", oConn );
    oCmd.ExecuteNonQuery (  );
    oCmd.Dispose (  );

    oConn.Close (  );
    oConn.Dispose (  );
}

基本上,在每个循环中,您现在只需更改参数的值,而不是构建一个全新的查询.但是,我不确定您的表现是否真的会从中受益.你需要尝试一下.

Basically, in each loop now you just change the values of the parameters, instead of constructing a whole new query. However, I am not sure, whether your performance will really benefit from it. You need to try that.

这篇关于如何回收我的 SqliteCommand 以加速此 Sqlite 批量插入 (iOS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆