简体   繁体   English

插入SqlLite需要太长时间

[英]inserting to SqlLite takes too long

I insert data like this but it takes too long 我插入这样的数据,但它需要太长时间

For 34,000 records it took 20 minutes !! 对于34,000条记录,花了20分钟! (for example if I insert into SQL Server CE it took only 3 minutes) (例如,如果我插入SQL Server CE只花了3分钟)

conn_sql = new SQLiteConnection(conn_str2);
conn_sql.Open();
cmd_sql = conn_sql.CreateCommand();

for (int i = 0; i < iTotalRows; i++)
{
    try 
    { 
        Makat = dsView.Tables["Items"].Rows[i]["Makat"].ToString().Trim(); 
    }
    catch { Makat = ""; }

    try 
    { 
        Barcode = dsView.Tables["Items"].Rows[i]["Barcode"].ToString().Trim(); 
    }
    catch { Barcode = ""; }

    try 
    { 
        Des = dsView.Tables["Items"].Rows[i]["Des"].ToString().Trim(); 
    }
    catch { Des = ""; }

    try 
    { 
         Price = dsView.Tables["Items"].Rows[i]["Price"].ToString().Trim(); 
    }
    catch { Price = ""; }

    SQL = "INSERT INTO Catalog(Makat,Barcode,Des,Price)VALUES('" + Makat + "','" + Barcode + "','" + Des + "','" + Price + "')";
    cmd_sql.CommandText = SQL;
    cmd_sql.CommandType = CommandType.Text;

    cmd_sql.ExecuteNonQuery();

    //cmd_sql.Dispose();
}

How to insert faster ? 如何插入更快?

SQLite implicitly wraps queries within a transaction. SQLite隐式地在事务中包装查询。 Beginning and committing transactions in a loop could be slowing things down. 在循环中开始和提交事务可能会减慢速度。 I think you should get a significant boost of speed if you start a transaction and commit it once the loop is completed: 我认为如果你启动一个事务并在循环完成后提交它,你应该大大提高速度:

conn_sql.Open();
using(var tran = conn_sql.BeginTransaction()) // <--- create a transaction
{
     cmd_sql = conn_sql.CreateCommand();
     cmd_sql.Transaction = tran;   // <--- assign the transaction to the command
              
     for (int i = 0; i < iTotalRows; i++)
     {
          // ...
          cmd_sql.CommandText = SQL;
          cmd_sql.CommandType = CommandType.Text;
          cmd_sql.ExecuteNonQuery();
          //cmd_sql.Dispose();

     }
     tran.Commit(); // <--- commit the transaction
} // <--- transaction will rollback if not committed already

At first, try to use StringBuilder for the string concatenation. 首先,尝试使用StringBuilder进行字符串连接。 Secondly, you are making 34k requests and this is slow, try to make less amount of requests. 其次,您正在发出34k请求,这很慢,尝试减少请求量。 Let say using StringBuilder concatenate 5k insert statements in one and fire it in transaction, do this until all your data will not be saved. 假设使用StringBuilder将5k插入语句连接在一起并在事务中触发,执行此操作直到所有数据都不会被保存。

If you do it in a single transaction it should be faster: 如果你在一次交易中这样做,它应该更快:

SqlTransaction transaction;
try
{
    conn_sql = new SQLiteConnection(conn_str2);
    conn_sql.Open();
    cmd_sql = conn_sql.CreateCommand();

    transaction = conn_sql.BeginTransaction();

    for (int i = 0; i < iTotalRows; i++)
    {
        // create SQL string
        cmd_sql.CommandText = SQL;
        cmd_sql.CommandType = CommandType.Text;
        cmd_sql.ExecuteNonQuery();
    }

    transaction.Commit();
}
catch
{
    transaction.Rollback();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM