SQLBulkCopy比预期慢

时间:2014-04-14 08:52:12

标签: c# sql sql-server-2012 sqlbulkcopy

它目前每百万条记录大约需要5分钟,我只是想知道这是否是预期的速度。

我想知道我的批量是否很小,即500?

我目前的代码看起来像这样...

using (var destinationConnection = new SqlConnection(Settings.Default.BatchOutputConnectionString))
{
    var tableName = mergedFileName.Substring(0, mergedFileName.IndexOf('.'));
    destinationConnection.Open();

    try
    {
        using (var createAndDropTableCommand = new SqlCommand("sp_DropAndCreateTable", destinationConnection))
        {
            createAndDropTableCommand.CommandType = CommandType.StoredProcedure;
            createAndDropTableCommand.Parameters.Add("@TableToCreate", SqlDbType.VarChar).Value = tableName;
            createAndDropTableCommand.ExecuteNonQuery();
        }

        foreach (var file in txtFiles)
        {
            using (var lineIterator = File.ReadLines(file).GetEnumerator())
            {
                while (lineIterator.MoveNext())
                {
                    var line = lineIterator.Current;
                    var dataColumns = line.Split('\t');
                    var dr = dt.NewRow();

                    for (var i = 0; i < dataColumns.Length; i++)
                    {
                        dr[i] = dataColumns[i].Trim().Length == 0 ? null : dataColumns[i];
                    }

                    dt.Rows.Add(dr);
                    recordCounter++;
                }
                using (var bulkCopy = new SqlBulkCopy(destinationConnection))
                {
                    bulkCopy.DestinationTableName = "dbo." + tableName;
                    bulkCopy.BatchSize = 500;
                    bulkCopy.ColumnMappings.Clear();
                    foreach (DataColumn col in dt.Columns)
                    {
                        bulkCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(col.ColumnName, col.ColumnName));
                    }

                    bulkCopy.WriteToServer(dt);
                }
                dt.Clear();
            }
        }
    }
    finally
    {
        destinationConnection.Close();
    }
}

0 个答案:

没有答案