gpt4 book ai didi

c# - 一个连接执行多条sql命令好,还是每次都重新连接好?

转载 作者:IT王子 更新时间:2023-10-29 03:55:36 26 4
gpt4 key购买 nike

这是我的测试代码,这似乎表明最好连接多次而不是只连接一次。

我做错了什么吗?

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);

输出:

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

更新:

好的,所以那些说使用一个连接会更快的人已经做到了。 (尽管差异很小,如果有的话。)这是修改后的代码和输出:

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);
}

输出:

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

更新:

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

输出:

one connection: 00:00:09.8544728
many connections: 00:00:11.4967753
one connection: 00:00:09.7775865

最佳答案

默认情况下,SqlConnection 将使用连接池。因此,在这两种情况下,您的代码很可能实际上不会打开很多连接。

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

参见 here如果您使用 MSSQLServer,请获取一些信息。尝试在连接字符串中设置 Pooling=false,看看它是否有所作为。

关于c# - 一个连接执行多条sql命令好,还是每次都重新连接好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5981376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com