我正在以特定的批量大小(尝试过 100,500、1000 等)插入 90,000 个实体,但无法获得速度。 这么多实体需要我大约 50 分钟。
我尝试了很多批量大小
int batchSize = 500;
i = 1;
var batches = nichoInvoices.Batch(batchSize);
foreach (var batch in batches)
{
Logger.LogInformation($"Seeding Invoices {i * batchSize}");
await _invManager.InvoiceRepository.InsertManyAsync(batch, true);
i++;
}
Logger.LogInformation($"End Seeding Invoices");
要么使用非事务性工作单元:
using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false)) // +
{ // +
foreach (var batch in batches)
{
Logger.LogInformation($"Seeding Invoices {i * batchSize}");
await _invManager.InvoiceRepository.InsertManyAsync(batch, true);
i++;
}
await uow.CompleteAsync(); // +
} // +
或者对每个批次使用单独的工作单元:
foreach (var batch in batches)
{
Logger.LogInformation($"Seeding Invoices {i * batchSize}");
using (var uow = _unitOfWorkManager.Begin(requiresNew: true)) // +
{ // +
await _invManager.InvoiceRepository.InsertManyAsync(batch, true);
await uow.CompleteAsync(); // +
} // +
i++;
}