开发者

SqlServer better to batch statements or foreach?

Hypothetically, is it better to send N statements to Sql Server (2008), or is it better to send 1 command comprising N statements to Sql Server? In either case, I am running the same statement over a list of objects, and in both cases I would be using named parameters. Suppose my use case is dumping a cache of log items every few hours.

foreach example

var sql = "update blah blah blah where id = @id";
using(var conn = GetConnection())
{
    foreach(var obj in myList)
    {
         var cmd = new SqlCommand() 
         {CommandText = sql, Connection = conn};
         //add params from obj
         cmd.ExecuteNonQuery();
    }
}

batch example

var sql = @"
   update blah blah blah where id = @id1
   update blah blah blah where id = @id2
   update blah blah blah where id = @id3
   -etc";
 using (var conn = GetConnection())
 {
     var cmd = new SqlCommand
     { CommandText = sql, Connection = conn};

     for(int i=0; i<myList.Count; i++)
     {
         //add params: "id" + i  from myList[i]
     }
     cmd.ExecuteNonQuery();
 }

In time tests, the batch version took 15% longer than the foreach version for large inputs. I figure the batch version takes longer to execute because the server has to parse a huge statement and bind up to 2000 parameters. Supposing Sql Server is on the LAN, is there any advanta开发者_开发问答ge to using the batch method?


Your tests would seem to have given you the answer however let me add another. It is preferrable to encapsulate the update into a separate function and call that using a foreach:

private function UpdateFoo( int id )
{
    const sql = "Update Foo Where Id = @Id";
    using ( var conn = GetConnection() )
    {
        using ( var cmd = new SqlCommand( sql, conn ) )
        {
            cmd.AddParameterWithValue( "@Id", id )
            cmd.ExecuteNonQuery();
        }
    }
}

private function UpdateLotsOfFoo()
{
    foreach( var foo in myList )
    {
        UpdateFoo( foo.Id );
    }
}

In this setup you are leveraging connection pooling which mitgates the cost of opening and closing connections.


@Thomas - this design can increase overhead of opening / closing connections in a loop. This is not a preferred practice and should be avoided. The code below allows the iteration of the statements while using one connection and will be easier on resources (both client and server side).

    private void UpdateFoo(int id)
    {
        const string sql  = "Update Foo Where Id = @Id";
        using (var conn = GetConnection())
        {
            conn.Open();
            foreach (var foo in myList)
            {
                UpdateFoo(foo.Id);
                using (var cmd = new SqlCommand(sql, conn))
                {
                    cmd.AddParameterWithValue("@Id", id);
                    cmd.ExecuteNonQuery();
                }
            }
            conn.Close();

        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜