使用一个连接执行多个sql命令或每次重新连接是否更好?

use*_*667 42 .net c# sql-server performance sqlconnection

这是我的测试代码,它似乎表明连接多次而不是仅连接一次更好.

难道我做错了什么?

int numIts = 100;
Stopwatch sw = new Stopwatch();
sw.Start();
using (SqlConnection connection = new SqlConnection(connectionParameters))
{   
            connection.Open();
    for(int i = 0; i < numIts; i++)
    {
        SqlCommand command = new SqlCommand(sqlCommandName, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue(par1Name, par1Val);
                command.Parameters.AddWithValue(par2Name, par2Val);
        using(SqlDataReader reader = command.ExecuteReader())
        {
        }
    }
}
sw.Stop();
TimeSpan durationOfOneConnectionManyCommands = sw.Elapsed;
Console.WriteLine(durationOfOneConnectionManyCommands);

sw.Reset();

sw.Start();
for(int i = 0; i < numIts; i++)
{
    using (SqlConnection connection = new SqlConnection(connectionParameters))
    {   
                connection.Open();
        SqlCommand command = new SqlCommand(sqlCommandName, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue(par1Name, par1Val);
                command.Parameters.AddWithValue(par2Name, par2Val);
        using(SqlDataReader reader = command.ExecuteReader())
        {
        }
    }                               
}
sw.Stop();
TimeSpan durationOfManyConnections = sw.Elapsed;
Console.WriteLine(durationOfManyConnections);
Run Code Online (Sandbox Code Playgroud)

输出:

//output:
//00:00:24.3898218   // only one connection established
//00:00:23.4585797   // many connections established.
//
//output after varying parameters (expected much shorter):
//00:00:03.8995448
//00:00:03.4539567
Run Code Online (Sandbox Code Playgroud)

更新:

好的,所以那些说它会更快与一个连接的人有它.(虽然差别很小,如果有的话.)这是修改后的代码和输出:

public void TimingTest()
{
    numIts = 1000;
    commandTxt = "select " + colNames + " from " + tableName;

    OneConnection();
    ManyConnections();
    OneConnection();
}
private void ManyConnections()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    for (int i = 0; i < numIts; i++)
    {
        using (SqlConnection connection = new SqlConnection(connectionParameters))
        {
            connection.Open();
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = commandTxt;

                using (SqlDataReader reader = command.ExecuteReader())
                {
                }
            }
        }
    }
    sw.Stop();
    TimeSpan durationOfManyConnections = sw.Elapsed;
    Console.WriteLine("many connections: " + durationOfManyConnections);
}
private void OneConnection()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    using (SqlConnection connection = new SqlConnection(connectionParameters))
    {
        connection.Open();
        for (int i = 0; i < numIts; i++)
        {
            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = commandTxt;
                using (SqlDataReader reader = command.ExecuteReader())
                {
                }
            }
        }
    }
    sw.Stop();
    TimeSpan durationOfOneConnectionManyCommands = sw.Elapsed;
    Console.WriteLine("one connection: " + durationOfOneConnectionManyCommands);
}
Run Code Online (Sandbox Code Playgroud)

输出:

one connection: 00:00:08.0410024
many connections: 00:00:08.7278090
one connection: 00:00:08.6368853

one connection: 00:00:10.7965324
many connections: 00:00:10.8674326
one connection: 00:00:08.6346272
Run Code Online (Sandbox Code Playgroud)

更新:

如果我SQLConnection.ClearAllPools()在每个功能之后使用,差异会更加显着:

输出:

one connection: 00:00:09.8544728
many connections: 00:00:11.4967753
one connection: 00:00:09.7775865
Run Code Online (Sandbox Code Playgroud)

Ben*_*ehn 33

默认情况下,SqlConnection将使用连接池.因此,在任何一种情况下,您的代码很可能不会实际打开许多连接.

您可以通过在连接字符串中启用或禁用池来控制SqlConnection是否将使用池,具体取决于连接字符串的DB,语法会有所不同.

如果您使用MSSQLServer,请参阅此处获取一些信息.尝试在连接字符串中设置Pooling = false,看看它是否有所不同.


Adr*_*iro 10

确切地说,最好有一个连接.也许您正在使用少量数据运行基准测试.尝试将数字增加到1,000或10,000.

另一点是,根据您的应用程序配置,您可能认为您正在运行多个连接,但.NET正在为您汇集连接,因此您基本上使用相同的连接运行.


Gro*_*roo 7

由于.NET重用连接("连接池"),因此DbConnection连续多次创建新实例的开销并不大.ADO.NET将重用引擎盖下的连接.这就是为什么SqlConnection每次处理对象都很好,告诉.NET它可以将它返回到池中.

但是,您可以通过使用ADO.NET批处理来提高多个插入的性能.在这种情况下,您每秒可以轻松地拥有数千个插入.如果性能至关重要,您甚至可以考虑使用SQLBulkCopy.

另外,你的第一对结果很奇怪:100个插入30秒?


n8w*_*wrl 5

一般来说,.NET 的连接池应该让它变得“无关紧要”,因为它在回收连接方面做得很好。但我的做法是使用单个连接来处理我知道将一起发生的一堆事务。我认为你的计时表明连接池正在完成其工作,并且只是运行中的简单变化。