Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mass Insert Into Sql Server

I am attempting to insert a mass of records into SQL Server 2005 from Vb.Net. Although the insertion is working fine, I am doing my best to try to make it as fast as possible. Currently, it takes ~ 11 mins for 100,000 records. What would be the suggested approach to inserting a large number of records into SQL Server from an Application?

My current apporach is basically opening the connection, iterating through my list of information and firing off individual sql insert statments, and then closing the connection. Anyone have a better suggestion on how to do this?

Current Function:

Public Sub BatchInsert(ByVal ParamCollections As List(Of SqlParameter()))

    Dim Conn As SqlConnection = New SqlConnection(DBHelper.DatabaseConnection)
    Using scope As TransactionScope = New TransactionScope()
    Using Conn

        Dim cmd As SqlCommand = New SqlCommand("sproc_name", Conn)

        Conn.Open()
        cmd.CommandType = CommandType.StoredProcedure

        For i = 0 To ParamCollections.Count - 1

            cmd.Parameters.Clear()
            cmd.Parameters.AddRange(ParamCollections(i))
            cmd.ExecuteNonQuery()

        Next

        Conn.Close()
        scope.Complete()
    End Using
    End Using

End Sub
like image 247
Nathan Avatar asked Dec 02 '08 18:12

Nathan


People also ask

How do I use the BULK INSERT statement in SQL Server?

SQL Server provides the BULK INSERT statement to perform large imports of data into SQL Server using T-SQL. Let's first understand the syntax and options of the BULK INSERT statement before we start using this command. The first argument to BULK INSERT should be a table name or a view name.

How do you use insert into in SQL?

The SQL INSERT INTO Statement. The INSERT INTO statement is used to insert new records in a table. INSERT INTO Syntax. It is possible to write the INSERT INTO statement in two ways. The first way specifies both the column names and the values to be inserted:

How to insert multiple rows into a table in SQL?

Normal insert statements will only insert one row at a time into the database. But if you want to multiple rows into the database table, then we use the SQL bulk insert. Bulk insert allows us to import the CSV file and insert all the data from the file. The Bulk insert also has the advantage of loading the data “BATCHSIZE” wise.

How to perform large imports of data into SQL Server using T-SQL?

SQL Server provides the BULK INSERT statement to perform large imports of data into SQL Server using T-SQL. Let's first understand the syntax and options of the BULK INSERT statement before we start using this command. The first argument to BULK INSERT should be a table name or a view name. By default, it expects that the schema of the file ...


3 Answers

Use the SqlBulkCopy class, it will be able to run through those 100K rows much faster than individual inserts.

Oh, and if you can, I would urge you to implement a IDataReader capable class, to feed the SqlBulkCopy.WriteToServer(IDataReader) method, this will allow you to produce data sequentially, one row at a time. If you are importing from a text file, as an example, building some IEnumerable<T> methods that uses yield return and converting it to a IDataReader object will allow you to feed data to the server very naturally.

To counter the loss of rollback ability with BCP, you can transfer the data into a temporary table, and then execute normal INSERT INTO statements on the server afterwards, bulk-transferring the data from the temporary table into the production table, this will allow you to use a transaction for the last transfer part, and will still run a lot faster than your original individual insert statements.

EDIT: and Here's an example (C#, but should be easy to convert to VB.Net) of the usage of the bulk load API.

like image 71
Lasse V. Karlsen Avatar answered Sep 24 '22 15:09

Lasse V. Karlsen


Thanks to everyone's help, I was able to complete my task. The SQLBulkCopy fit my needs perfectly (although there were some other excellent suggestions). Using SqlBulkcopy,the time went from 11 mins to 45 seconds. I can't believe the difference!

For future reference, here are a few bits of information:

  • To use SQL Bulk Copy, your data has to be in the form of a DataSet, DataReader, or DataTable. Some XML is allowed as well.

Basic Implementation code:

    Public Sub PerformBulkCopy(ByVal dt As DataTable)

    Using Conn As SqlConnection = New SqlConnection(DBHelper.DatabaseConnection)
        Conn.Open()

        Using s As SqlBulkCopy = New SqlBulkCopy(Conn)

            s.DestinationTableName = "TableName"
            s.WriteToServer(dt)
            s.Close()

        End Using

        Conn.Close()
    End Using
End Sub

Very informative link that I found:

Using Sql Bulk Copy

Thanks to all for the help! I sincerely appreciate it.

like image 41
Nathan Avatar answered Sep 25 '22 15:09

Nathan


Put your data to be imported into a csv file and run the bcp utility on the data. You can't get any faster with sequential calls inserting single rows at a time, you certainly need a bulk utility if you want performance.

The SQLBulkCopy class will allow you to transmit all the data in a collection so the server can process everything at once, eliminating the back and forth. So if you want to avoid creating temporary files (which I would), then look to that class.

Just having the connection remain open is a good start, but you still have the overhead of sending a row, having SQL store it, return a result, and then you must iterate to the next row.

like image 20
Adam Avatar answered Sep 23 '22 15:09

Adam