我正在将大约 900000 行的数据表插入 MySQL 数据库,现在 13 小时后,我的程序已插入 185000 行。我觉得那太慢了^^
这是我如何将 DataTable 插入 MYSQL DB 的代码
//data is typeof DataTable and filled with my data.
using (System.Data.SqlClient.SqlConnection srcConnection = new SqlConnection(Settings.Default.SourceConnection))
using (MySql.Data.MySqlClient.MySqlConnection dstConnection = new MySqlConnection(Settings.Default.TargetConnection))
{
if (dstConnection.State != ConnectionState.Open)
dstConnection.Open();
if (srcConnection.State != ConnectionState.Open)
srcConnection.Open();
var builder = SqlBuilder(tableName, data)
foreach (DataRow toCopy in data)
{
using (var cmdUpdate = builder.GenerateUpdateCommand(dstConnection, toCopy))
{
if (cmdUpdate.ExecuteNonQuery() == 0)
{
using (var cmdInsert = builder.GenerateInsertCommand(dstConnection, toCopy))
{
cmdInsert.ExecuteNonQuery;
}
}
}
}
}
builder 是我自己的班级:
public class SqlBuilder
{
private readonly string _tableName;
private readonly DataColumnCollection _columns;
private readonly List<string> _columnStrings = new List<string>();
private DataColumn _primaryDataColumn;
private readonly Logger _logger = LogManager.GetLogger(typeof(SqlBuilder).FullName);
public SqlBuilder(string tableName, DataTable table)
{
if (tableName == null) throw new ArgumentNullException("tableName");
if (table == null) throw new ArgumentNullException("table");
if (table.PrimaryKey.Count() != 1) throw new ArgumentException("Only one primary key supported", "table");
_tableName = tableName;
_columns = table.Columns;
_primaryDataColumn = table.PrimaryKey[0];
foreach (DataColumn column in _columns)
_columnStrings.Add(column.ColumnName);
}
public SqlBuilder()
{
}
public string TableName
{
get { return _tableName; }
}
public String[] ColumnStrings
{
get { return _columnStrings.ToArray(); }
}
public IDbCommand GenerateCountCommand(IDbConnection con)
{
IDbCommand result = con.CreateCommand();
result.CommandText = String.Format("SELECT COUNT(*) FROM {0}", TableName);
return result;
}
public IDbCommand GenerateDeleteTableCommand(IDbConnection con)
{
IDbCommand result = con.CreateCommand();
result.CommandText = String.Format("DELETE FROM {0}", TableName);
return result;
}
public IDbCommand GenerateDeleteChangeLogTableCommand(IDbConnection con)
{
IDbCommand result = con.CreateCommand();
result.CommandText = String.Format("DELETE FROM ChangeLog");
return result;
}
public String GenerateInsertCommandText()
{
String result;
result = String.Format(
"INSERT INTO {0} ({1}) VALUES ({2})",
TableName,
String.Join(",", _columnStrings.ToArray()),
String.Join(",", _columnStrings.Select(s => "@" + s).ToArray()));
return result;
}
public string GenerateUpdateCommandText()
{
String result;
result = String.Format(
"UPDATE {0} SET {1} WHERE {2}",
TableName,
String.Join(", ", _columnStrings.Where((s =>!_primaryDataColumn.ColumnName.Equals(s))).Select(s => s+"=@" + s).ToArray()),
_primaryDataColumn.ColumnName+"=@"+_primaryDataColumn.ColumnName
);
return result;
}
public string GenerateDeleteCommandText(int id)
{
string result = String.Format("DELETE FROM {0} WHERE {1} = {2}", _tableName , _columnStrings[0], id);
return result;
}
public IDbCommand GenerateInsertCommand(IDbConnection con, DataRow row)
{
IDbCommand result = con.CreateCommand();
result.CommandText = GenerateInsertCommandText();
result = FillParameters(row, result);
if (_logger.IsTraceEnabled) _logger.Trace(result.CommandText);
return result;
}
public IDbCommand GenerateUpdateCommand(IDbConnection connection, DataRow dataRow)
{
IDbCommand result = connection.CreateCommand();
result.CommandText = GenerateUpdateCommandText();
result = FillParameters(dataRow, result);
if (_logger.IsTraceEnabled) _logger.Trace(result.CommandText);
return result;
}
public IDbCommand GenerateDeleteCommand(IDbConnection con, int id)
{
IDbCommand result = con.CreateCommand();
result.CommandText = GenerateDeleteCommandText(id);
return result;
}
private IDbCommand FillParameters(DataRow row, IDbCommand result)
{
foreach (var curColumn in _columnStrings)
{
IDbDataParameter parameter = result.CreateParameter();
parameter.ParameterName = curColumn;
parameter.Value = row[curColumn];
result.Parameters.Add(parameter);
}
return result;
}
}
任何人都知道如何使这个更快???