Hoho there.
I was just trying to enhance the performance of my application (.NET 3.5, C#) with the use of stored procedures.
So I wrote a little test-app to see how much faster these are in comparison to normal queries which looks like this:
private static long _StartStored;
private static long _EndStored;
private static long _StartNormal;
private static long _EndNormal;
private static TimeSpan _StoredTime;
private static TimeSpan _NormalTime;
private static string[] _Stored = new string[102000];
private static string[] _Normal = new string[102000];
static void Main(string[] args)
{
Console.WriteLine("Querying 2000 normal queries");
_SQLConnection = new SqlConnection(/*my_connection*/);
_SQLConnection.Open();
_StartNormal = DateTime.Now.Ticks;
for (int i = 100000; i <= 102000; i++)
{
DataTable _ResultDataTable = new DataTable();
SqlDataAdapter _SQLAdapter = new SqlDataAdapter(/*my_query*/, _SQLConnection);
_SQLAdapter.Fill(_ResultDataTable);
if (_ResultDataTable.Rows.Count > 0)
_Normal[i] = _ResultDataTable.Rows[0]["row"].ToString();
}
_EndNormal = DateTime.Now.Ticks;
_NormalTime = TimeSpan.FromTicks(_EndNormal - _StartNormal);
Console.WriteLine("Total execution time: " + _NormalTime.ToString());
//-----------------------------------------------------------------------------
Console.WriteLine("Querying 2000 stored procedures");
_StartStored = DateTime.Now.Ticks;
SqlCommand _Cmd = new SqlCommand(/*my_sp*/, _SQLConnection);
_Cmd.CommandType = CommandType.StoredProcedure;
SqlParameter _Param = new SqlParameter("@param1", 0);
_Cmd.Parameters.Add(_Param);
for (int i = 100000; i <= 102000; i++)
{
_Cmd.Parameters["@param1"].Value = i;
SqlDataReader _Reader = _Cmd.ExecuteReader();
while (_Reader.Read())
{
_Stored[i] = _Reader["StartWork"].ToString();
}
_Reader.Close();
}
_EndStored = DateTime.Now.Ticks;
_StoredTime = TimeSpan.FromTicks(_EndStored - _StartStored);
Console.WriteLine("Total execution time: " + _StoredTime.ToString());
I'd love to shorten that code, but well... doesn't work :D
TL;DR - 2000 stored procedures of the same query are only about 4 seconds faster, which seems quite low to me?
Am I using the stored procedures wrong?