【发布时间】:2011-05-12 16:36:12
【问题描述】:
这是我的测试代码,这似乎表明最好连接多次而不是只连接一次。
我做错了吗?
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
【问题讨论】:
-
这些数字太接近了,无法成为一个明确的基准!
-
+1 这个增量很小。尝试运行它几千次,看看你得到了什么。此外,重新排序这两个测试,因为一些启动时间无疑会影响您的结果。
-
不仅时间几乎相等,而且您是在一个接一个地执行它们,由于代码已经加载和潜在的缓存,第二轮可能会更快。连接池是关键,在这里。
-
@n8wrl, @user420667:
DbCommand也实现了IDisposable(SqlCommand也是如此)。 -
嗯...我会尝试重新排序和更多。但我认为我应该将我的命令更改为更快的命令,因为大部分时间都花在了命令的执行上。
标签: c# .net sql-server performance sqlconnection