Skip to main content

Loading 50 million records in 50 minutes in SQL Server - Super fast data loading

 Greetings to all the readers!!

 Yes, it is possible to load 1 million of records in 1 minute time from one SQL server to another.

  •  Create an SSIS package with a Script task.
  •  Add logging for ease of debugging and make all the connections configurable to make it re-usable.
  •  In the script task, we have to use .net script given below. We can define the batch size and   timeout.
  •  By Default, Identity in the destination is reset, in case you have to preserve it, amend the code      accordingly.

In my task, I used SQL Server 2017, SSIS 2017 and the source system is SQL 2008 R2

 C#

using System.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();


            // Open the destination connection. In the real world you would

            // not use SqlBulkCopy to move data from one table to the other

            // in the same database. This is for demonstration purposes only.

            using (SqlConnection destinationConnection =

                       new SqlConnection(connectionString))

            {

                destinationConnection.Open();


                // Set up the bulk copy object.

                // Note that the column positions in the source

                // data reader match the column positions in

                // the destination table so there is no need to

                // map columns.

                using (SqlBulkCopy bulkCopy =

                           new SqlBulkCopy(destinationConnection))

                {

                    bulkCopy.DestinationTableName =

                        "dbo.BulkCopyDemoMatchingColumns";


                    try

                    {

                        // Write from the source to the destination.

                        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;";

    }

}




Reference

SqlBulkCopy Class (System.Data.SqlClient) | Microsoft Learn

Comments

Popular posts from this blog

What makes Life 100 percent

In our life, there are many qualities we have such as patience, talent, knowledge, etc. Though we have all these, do you know which one makes your life 100 percent. Let’s find it using a mathematical calculation. Firstly, Knowledge –   Sum up the place value of each character.                                         –       11 + 14 + 15 + 23 + 12 + 5 + 4 + 7 + 5 = 96 Then, Health – 8 + 5 + 1 + 12 + 20 + 8 = 54 Money – 13 + 15 + 14 + 5 + 25 = 72. Peace – 16 + 5 + 1 + 3 + 4 = 24 Oh, No. none of these 100%. All of our favorite things have gone. What else does it. Let’s try a new one, Attitude – 1 + 20 + 20 + 9 + 20 + 21 + 4 + 5 = 100, wow. Finally, we got it. It is our Attitude make the life 100%. Always have a positive, healthy, and good attitude. That’s more than enough. Our Thiruvalluvar has said the same in the below one...

Leo Movie - Individual Opinion

  Leo Movie – Individual Opinion After Vikram, Expectation on Director Lokesh has gone sky high. In that way, Leo movie is not satisfying us fully. Likes -         Making is fantastic -         Comedy has helped a bit -         Location is a great attraction   Dislikes -         Music -         Story track change from serial killer to drug dealing -         Hyena fails to catch attention, selection of animal could be better -         He is crying and shouting a lot for doubting his character, at last he admits the same One lady does a promise to take revenge for the killing of his family, she has never appeared on the screen after that. Movie is setting up an expectation initially that this is going to be a...

How to get patta in Tamilnadu in less than 100 Rs

Good wishes to all the readers!! When it comes to patta, there are two types of patta as far as i know. Type 1 If the piece of land you own is fresh, recently divided from a parent property, below method will apply. 1. Go to the nearest e-service center with Aadhar card and Registration documents. 2. They will fill up the online form for you with the details you have shared in the documents.     once application is submitted, they will give you print out or acknowledgement of your application.     Charge for this service is less than 100 Rs when i applied 3 years ago. 3. You have to get the surveyor of your land contact details. you need to check with them       for their availability to submit this acknowledgement original copy along with all the registration              history of your land.       I submitted a book of records that show registration details of the land from t...