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...

Additional Discount on VU TV

One of the leading smart TV brands in india VU is offering additional discounts across all it’s models. This is an online only brand, we can’t get this brand from retail outlets like Reliance digital, Croma, etc. If we buy it from online portals like flipkart, amazon, etc we get around 30% discount on MRP. When I was trying to purchase this model, I happened to see an online article which showed the own website of VU, that offered me additional 2000 rupees. So please make use of this opportunity to avail maximum discounts on VU TV. Vu Televisions - Official Website (vutvs.com)

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...