Pat
Pat

Reputation: 649

SQL Bulk Query Inserts

So I have a list of id's stored in a JsonArray. Currently, I am looping through the id's like so:

'Insert ids into table
For Each userId As Int32 In idList
    command.CommandText = "INSERT INTO user_ids (id) VALUES (" & userId & ");"
    command.ExecuteNonQuery()
Next

When that runs, it executes about 100 queries give or take, depending on the size of the id list. What I am trying to do, is use a bulk query so that rather than doing 100 queries to the database, I can do them all in one query. I have been looking at using DataSets and BulkCopy but I am not sure how I would implement it.

Is there a better solution?

Any help would be appreciated. Thanks.

Upvotes: 1

Views: 118

Answers (1)

Joel Coehoorn
Joel Coehoorn

Reputation: 415690

Setting up to do a BULK INSERT or SqlBulkCopy would likely be more efficient, but with just about 100 rows it's questionable whether you'd see a noticeable improvement, or even any improvement at all... the time spent massaging the data to a BULK INSERT-friendly format could easily outweigh the database throughput gain here. You also need to weigh whether the increased code complexity and introduction of a new technique would justify any improvement that you did see.

Instead, I'd just go for keeping the the same query and connection objects in your loop, like this:

Using cn As New SqlConnection("connection string here"), _
      cmd As New SqlCommand("INSERT INTO user_ids (id) VALUES ( @UserID )" )

    cmd.Parameters.Add("@UserID", SqlDbType.Integer)
    cn.Open()

    'Insert ids into table
    For Each userId As Int32 In idList
        cmd.Parameters("@UserID").Value = userId
        cmd.ExecuteNonQuery()
    Next
End Using

In addition to the nice side benefit of keeping your code safe from sql injection attacks, this code should be faster than what you have, because the database will be able to cache that execution plan during your loop, and thus skip the compile and generate plan steps as it executes individual query.

If this insert happens at regular intervals (such as once per day), you might also take a look at this KB article about using minimal logging:

http://technet.microsoft.com/en-us/library/ms191244(v=sql.105).aspx

Upvotes: 1

Related Questions