出于某种疯狂的原因,我在将数据以合理的块发送到 SQL 时遇到了 OutOfMemoryException,并且几乎没有使用任何内存:
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at System.Data.DataTable.NewRowArray(Int32 size)
at System.Data.RecordManager.GrowRecordCapacity()
at System.Data.RecordManager.NewRecordBase()
at System.Data.DataTable.NewRecord(Int32 sourceRecord)
at Company.PA.Data.PADbContext.d__22`1.MoveNext() in D:\Agent_A\_work\7\s\Company.PA.DataLayer\Company.PA.Data\BulkInsert\StreamedSqlBulkCopy.cs:line 46
在下面的 while 循环中调用时发生错误dataTable.NewRow()
,一旦我超过第 30 百万行:
/// <summary>Helper to stream a large number of records into SQL without
/// ever having to materialize the entire enumerable into memory at once.</summary>
/// <param name="destinationTableName">The name of the table in the database to copy data to.</param>
/// <param name="dataTable">A new instance of the DataTable class that matches the schema of the table to insert to.
/// This should match exactly (same column names) what is in SQL, for automatic column mapping to work.</param>
/// <param name="sourceData">The enumerable of data that will be used to generate DataRows</param>
/// <param name="populateRow">A delegate function that populates and returns a new data row for a given record.</param>
/// <param name="memoryBatchSize">The number of DataRows to generate in memory before passing them to SqlBulkCopy</param>
/// <param name="insertBatchSize">The batch size of inserts performed by SqlBulkCopy utility.</param>
public async Task StreamedSqlBulkCopy<T>(
string destinationTableName, DataTable dataTable,
IEnumerable<T> sourceData, Func<T, DataRow, DataRow> populateRow,
int memoryBatchSize = 1000000, int insertBatchSize = 5000)
{
using (SqlConnection connection = new SqlConnection(Database.Connection.ConnectionString))
{
connection.Open();
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.TableLock, null))
using (IEnumerator<T> enumerator = sourceData.GetEnumerator())
{
// Configure the single SqlBulkCopy instance that will be used to copy all "batches"
bulkCopy.DestinationTableName = destinationTableName;
bulkCopy.BatchSize = insertBatchSize;
bulkCopy.BulkCopyTimeout = _bulkInsertTimeOut;
foreach (DataColumn column in dataTable.Columns)
bulkCopy.ColumnMappings.Add(column.ColumnName, column.ColumnName);
// Begin enumerating over all records, preparing batches no larger than "memoryBatchSize"
bool hasNext = true;
while (hasNext)
{
DataRow[] batch = new DataRow[memoryBatchSize];
int filled = 0;
while ((hasNext = enumerator.MoveNext()) && filled < memoryBatchSize)
batch[filled++] = populateRow(enumerator.Current, dataTable.NewRow());
// When we reach the end of the enumerable, we need to shrink the final buffer array
if (filled < memoryBatchSize)
Array.Resize(ref batch, filled);
await bulkCopy.WriteToServerAsync(batch);
}
}
}
}
希望很清楚,上述帮助程序的目的是IEnumerable<T>
使用读取器和委托将(非常大的)数据流式传输到 SQL 表SqlBulkCopy
,该委托将为给定元素填充一行。
示例用法是:
public async Task SaveExchangeRates(List<FxRate> fxRates)
{
var createDate = DateTimeOffset.UtcNow;
await StreamedSqlBulkCopy("RefData.ExchangeRate",
GetExchangeRateDataTable(), fxRates, (fx, newRow) =>
{
newRow["BaseCurrency"] = "USD";
newRow["TargetCurrency"] = fx.CurrencyCode;
newRow["ExchangeDate"] = fx.ExchangeRateDate;
newRow["DollarValue"] = fx.ValueInUsd;
return newRow;
});
}
private DataTable GetExchangeRateDataTable()
{
var dataTable = new DataTable();
dataTable.Columns.Add("ExchangeDate", typeof(DateTime));
dataTable.Columns.Add("BaseCurrency", typeof(string));
dataTable.Columns.Add("TargetCurrency", typeof(string));
dataTable.Columns.Add("DollarValue", typeof(double));
return dataTable;
}