/ / C#ODBC接続の破棄パフォーマンス-C#、ODBC、データベースパフォーマンス

C#ODBC接続破棄パフォーマンス - c#、odbc、データベースパフォーマンス

私は私のパフォーマンスを向上させようとしています応用。データベースクエリのパフォーマンスを調べると、クエリの計算時間のほとんどが接続のクローズと破棄に費やされていることに気付きました。
合計クエリ時間(平均)76ミリ秒では、接続を開いてクエリを実行するために約10ミリ秒が使用されます。他の66 msは、終了後に接続を破棄するために使用されます using ステートメント。計算時間の85%以上が接続の破棄に使用されるのが正常かどうか疑問に思っていました。これを減らす方法はありますか?これにより、パフォーマンスが大幅に向上しますか?

私のコード:

public String executeScalarCommandString(String conString, OdbcCommand command)
{
//starting stopwatch here
String result = "";
using (OdbcConnection connection = new OdbcConnection(conString))
{
try
{
command.Connection = connection;
command.CommandType = CommandType.Text;
connection.Open();
object obj = command.ExecuteScalar();
if (obj != null && DBNull.Value != obj)
result = obj.ToString();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
//stop and restart stopwatch here. Avg elapsed time: 10 ms
}
//stop stopwatch here. Avg elapsed time: 76 ms
return result;
}

参考:いくつかの理由で、古いAccess Jetエンジンを使用しています。
可能であれば、SQL Serverへの移行を計画しています。これは私のパフォーマンスにどのように影響しますか?

回答:

回答№1は1

パフォーマンスを向上させるには、以前の接続を使用してタスクを実行するのが最善です。そのため、接続を開いたり閉じたりする必要はありません。 コマンドを実行するたびに

public String executeScalarCommandString(String conString, OdbcCommand command)
{
//starting stopwatch here
String result = "";
// remember we have already a connection which already open
// so we dont have to create new connection
/* we dont need this :: using (OdbcConnection connection = new OdbcConnection(conString))*/
{
try
{
// i"ll assume the connection already on command object
//command.Connection = connection;
command.CommandType = CommandType.Text;
//connection.Open();
object obj = command.ExecuteScalar();
if (obj != null && DBNull.Value != obj)
result = obj.ToString();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
//stop and restart stopwatch here. Avg elapsed time: 10 ms
}
//stop stopwatch here. Avg elapsed time: 76 ms
return result;
}

この助けを願って