我有将数据插入 Oracle 数据库的 ac# 过程。我尝试使用批量插入,但性能非常糟糕,我不知道为什么。插入超过 100'000 行大约需要 1h30 !
这是我的插入过程:
internal void InsertPublications(DateTime aDate, List<Model.Publication> aPublications, Action<int> aProgress, Action<int> aCallBack)
{
List<List<Model.Publication>> _Publications = Split(aPublications, 128);
int _InsertedCount = 0;
var _Worker = new BackgroundWorker() { WorkerReportsProgress = true };
_Worker.DoWork += (_Sender, _Args) =>
{
var _TableName = string.Format("SRC_PUBLICATION_{0}", aDate.ToString("yyyyMMdd"));
OracleCommand _Command;
this.Connect();
try
{
this.CreateOrReplaceTable(_TableName, SQL_CREATE_PULICATIONS_TABLE);
foreach (var _PublicationSub in _Publications)
{
if (!DlgParamPublication.alive)
{
break;
}
_Command = this.Connection.CreateCommand();
_Command.CommandText = string.Format(SQL_INSERT_PULICATIONS, _TableName);
_Command.CommandType = CommandType.Text;
_Command.BindByName = true;
_Command.ArrayBindCount = _PublicationSub.Count;
_Command.Parameters.Add(":ANNEE", OracleDbType.NVarchar2, _PublicationSub.Select(_Item => _Item.Year).ToArray(), ParameterDirection.Input);
_Command.Parameters.Add(":IDE_PUBLICATION", OracleDbType.NVarchar2, _PublicationSub.Select(_Item => _Item.Pid).ToArray(), ParameterDirection.Input);
_Command.Parameters.Add(":IDE_TYPE_PUBLICATION", OracleDbType.NVarchar2, _PublicationSub.Select(_Item => _Item.IdePublicationType.ToString().ToLower()).ToArray(), ParameterDirection.Input);
_Command.Parameters.Add(":IDE_TYPE_TRAVAIL", OracleDbType.NVarchar2, _PublicationSub.Select(_Item => _Item.IdeTypeOfWork.ToString().ToLower()).ToArray(), ParameterDirection.Input);
_Command.Parameters.Add(":IDE_NIVEAU_ELEMENT_ORG", OracleDbType.NVarchar2, _PublicationSub.Select(_Item => _Item.UserRefType.ToString().ToLower()).ToArray(), ParameterDirection.Input);
_Command.Parameters.Add(":IDE_ELEMENT_ORG", OracleDbType.NVarchar2, _PublicationSub.Select(_Item => _Item.UserRefValue).ToArray(), ParameterDirection.Input);
_Command.Parameters.Add(":AUTEUR", OracleDbType.NClob, _PublicationSub.Select(_Item => _Item.Author).ToArray(), ParameterDirection.Input);
_InsertedCount += _Command.ExecuteNonQuery();
_Worker.ReportProgress(0, _InsertedCount);
}
catch (Exception _Exc)
{
ViewModel.DlgParamPublication.Logger.LogException("INSERT PUBLICATIONS", _Exc, false);
}
finally
{
this.Disconnect();
}
};
_Worker.ProgressChanged += (_Sender, _Args) => { aProgress((int)_Args.UserState); };
_Worker.RunWorkerCompleted += (_Sender, _Args) => { aCallBack(_InsertedCount); };
// Start background thread
if (DlgParamPublication.alive)
{
_Worker.RunWorkerAsync();
}
}
知道为什么要花这么长时间吗?
更新
有人有其他建议吗?我无法解决问题。