简体   繁体   English

SqlBulkCopy Azure DataTable比Streaming更快

[英]SqlBulkCopy Azure DataTable faster than Streaming

I need a worker role that takes a txt file from a Blob and bulk it into an Azure Sql table. 我需要一个worker角色,它从Blob获取一个txt文件并将其批量转换为Azure Sql表。

I'm using the SqlBulkCopy provided by LumenWorks.Framework.IO, I've created 2 version of the worker role: 1) read the entire file, load it into a DataTable, execute the SqlBulkCopy 2) StreamRead the file and pass the Stream to the SqlBulkCopy 我正在使用LumenWorks.Framework.IO提供的SqlBulkCopy,我创建了2个版本的worker角色:1)读取整个文件,将其加载到DataTable中,执行SqlBulkCopy 2)StreamRead文件并传递Stream到SqlBulkCopy

The problem is that the second version has like half of the performance of the first one. 问题是第二个版本具有第一个版本的一半性能。

As an example with a 10MB txt file, with 90'000 records: -first version: half a second to load file, 2 seconds to convert to a DataTable, 20 seconds for the SqlBulkCopy 作为一个10MB txt文件的例子,有90'000条记录: - 第一版:半秒加载文件,2秒转换为DataTable,20秒为SqlBulkCopy

-second version: 50 seconds total (more than double!) - 第二版:总共50秒(超过一倍!)

I've tried to change the BatchSize but it doesn't seem to make much difference and I don't know what im doing wrong, here is the 2' version code: 我试图更改BatchSize但它似乎没有太大的区别,我不知道我做错了什么,这里是2'版本代码:

using (var stream = await blockBlob.OpenReadAsync(cancellationToken))
using (var reader = new StreamReader(stream))
using (var csv = new CsvReader(reader, true, ';'))
using (var conn = new SqlConnection(CloudConfigurationManager.GetSetting("TestDbConn")))
{
      await conn.OpenAsync(cancellationToken);
      connAperta = true;
      using (var transaction = conn.BeginTransaction())
      using (var bulkCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.KeepIdentity | SqlBulkCopyOptions.TableLock, transaction))
      {
             bulkCopy.BulkCopyTimeout = 300;
             bulkCopy.DestinationTableName = "[3sc_base_1]";
             await bulkCopy.WriteToServerAsync(csv, cancellationToken);
             transaction.Commit();
      }
}

What i'm doing wrong?? 我做错了什么?

Have a look at the new Azure SQL Database capability to bulk upload directly from an Azure Storage account . 查看新的Azure SQL数据库功能,以直接从Azure存储帐户批量上载

This should be the fastest and easiest way to achieve what you want unless you are not streaming directly but doing transformation as well. 这应该是实现您想要的最快速,最简单的方法,除非您不是直接流式传输,而是进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM