最近处理一个将csv内容导入到sqlite数据库中,使用了事务(BEGIN TRANSACTION; sql语句; COMMIT;)
测试发现一次1000条导入,反而比10000条导入要快,总共6万多条数据,整下下来竟然快有6分钟,真是奇怪
var iCount = 0;
var dDataTime = DateTime.Now;
var mCsvHelp = new CsvHelper();
var mDataTable = mCsvHelp.GetCsvData(@"E:\", "scrm_friend");
var mSql = new StringBuilder();
var conn = new SQLiteConnection(@"Data Source=E:\mydatabase.db");
SQLiteCommand command;
conn.Open();
for (var i = 0; i < mDataTable.Rows.Count; i++)
{
var mRows = mDataTable.Rows[i];
mSql.Append("Insert Into scrm_friend (friendid,userid,friendname,phone) values(" + mRows[0] + "," +
mRows[1] + ",'" + mRows[2] + "','" + mRows[3] + "');");
iCount = iCount + 1;
if (iCount > 1000)
{
command = new SQLiteCommand
{
Connection = conn,
CommandText = "BEGIN TRANSACTION;" + mSql + "COMMIT;"
};
command.ExecuteNonQuery();
mSql.Remove(0, mSql.Length);
iCount = 0;
}
}
command = new SQLiteCommand
{
Connection = conn,
CommandText = "BEGIN TRANSACTION;" + mSql + "COMMIT;"
};
command.ExecuteNonQuery();
MessageBox.Show((DateTime.Now - dDataTime).ToString());