Share via


대량 복사 작업을 위한 순서 힌트

적용 대상: .NET Framework .NET .NET Standard

ADO.NET 다운로드

대량 복사 작업은 SQL Server 테이블에 데이터를 로드하는 다른 방법에 비해 상당한 성능 이점을 제공합니다. 주문 힌트를 사용하여 성능을 더욱 향상시킬 수 있습니다. 사용자의 대량 복사 작업에 대한 순서 힌트를 지정하면 정렬된 데이터의 삽입 시간을 클러스터형 인덱스가 있는 테이블로 줄일 수 있습니다.

기본적으로 대량 삽입 작업은 들어오는 데이터가 정렬되지 않았음을 전제로 합니다. SQL Server는 이 데이터를 대량으로 로드하기 전에 강제로 데이터를 중간 정렬합니다. 수신 데이터가 이미 정렬되어 있는 경우 순서 힌트를 사용하여 클러스터형 인덱스의 일부인 대상 열의 정렬 순서에 대해 대량 복사 작업을 알릴 수 있습니다.

대량 복사 작업에 순서 힌트 추가

다음 예제에서는 AdventureWorks 샘플 데이터베이스의 원본 테이블에서 동일한 데이터베이스의 대상 테이블로 데이터를 대량 복사합니다. 대상 테이블의 ProductNumber 열에 대한 정렬 순서를 정의하기 위해 SqlBulkCopyColumnOrderHint 개체가 만들어집니다. 그런 다음 SqlBulkCopy 인스턴스에 순서 힌트가 추가됩니다. 그러면 결과 INSERT BULK 쿼리에 적절한 순서 힌트 인수가 추가됩니다.

using System;
using System.Data;
using Microsoft.Data.SqlClient;

class Program
{
    static void Main()
    {
        string connectionString = GetConnectionString();
        // Open a sourceConnection to the AdventureWorks database.
        using (SqlConnection sourceConnection =
                   new SqlConnection(connectionString))
        {
            sourceConnection.Open();

            // Perform an initial count on the destination table.
            SqlCommand commandRowCount = new SqlCommand(
                "SELECT COUNT(*) FROM " +
                "dbo.BulkCopyDemoMatchingColumns;",
                sourceConnection);
            long countStart = System.Convert.ToInt32(
                commandRowCount.ExecuteScalar());
            Console.WriteLine("Starting row count = {0}", countStart);

            // Get data from the source table as a SqlDataReader.
            SqlCommand commandSourceData = new SqlCommand(
                "SELECT ProductID, Name, " +
                "ProductNumber " +
                "FROM Production.Product;", sourceConnection);
            SqlDataReader reader =
                commandSourceData.ExecuteReader();

            // Set up the bulk copy object.
            using (SqlBulkCopy bulkCopy =
                       new SqlBulkCopy(connectionString))
            {
                bulkCopy.DestinationTableName =
                    "dbo.BulkCopyDemoMatchingColumns";

                // Setup an order hint for the ProductNumber column.
                SqlBulkCopyColumnOrderHint hintNumber =
                    new SqlBulkCopyColumnOrderHint("ProductNumber", SortOrder.Ascending);
                bulkCopy.ColumnOrderHints.Add(hintNumber);

                // Write from the source to the destination.
                try
                {
                    bulkCopy.WriteToServer(reader);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    // Close the SqlDataReader. The SqlBulkCopy
                    // object is automatically closed at the end
                    // of the using block.
                    reader.Close();
                }
            }

            // Perform a final count on the destination 
            // table to see how many rows were added.
            long countEnd = System.Convert.ToInt32(
                commandRowCount.ExecuteScalar());
            Console.WriteLine("Ending row count = {0}", countEnd);
            Console.WriteLine("{0} rows were added.", countEnd - countStart);
            Console.WriteLine("Press Enter to finish.");
            Console.ReadLine();
        }
    }

    private static string GetConnectionString()
    // To avoid storing the sourceConnection string in your code, 
    // you can retrieve it from a configuration file. 
    {
        return "Data Source=(local); " +
            " Integrated Security=true;" +
            "Initial Catalog=AdventureWorks;";
    }
}

다음 단계