I have a stored procedure that is being executed from a legacy ASP.NET WebForms application that is timing out. The CommandTimeout
property has been set to 6 minutes. The stored procedure queries a table with 15 million rows in it and joins to other tables.... so it is slow. However, it only returns 20 rows, so network performance is not the issue here.
var data = new DataSet();
using (var connection = GetConnection())
using(var command = connection.CreateCommand())
using(var dataAdapter = new SqlDataAdapter(command))
{
command.CommandTimeout = 360;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "praGetTradesForProductIdReassignment";
command.Parameters.AddWithValue("@int4_TradeId", (object)tradeId ?? DBNull.Value);
command.Parameters.AddWithValue("@int4_FeedId", (object)feedId ?? DBNull.Value);
command.Parameters.AddWithValue("@int4_SystemId", (object)systemId ?? DBNull.Value);
command.Parameters.AddWithValue("@int4_TradeCode", (object)tradeCode ?? DBNull.Value);
command.Parameters.AddWithValue("@int4_ProductID", (object)productId ?? DBNull.Value);
command.Parameters.AddWithValue("@sortColumn", sortColumn);
command.Parameters.AddWithValue("@sortDirection", sortDirection);
command.Parameters.AddWithValue("@pageIndex", pageIndex);
command.Parameters.AddWithValue("@pageSize", pageSize);
dataAdapter.Fill(data);
}
return data;
When I execute the same stored procedure, with the same parameters from within SQL Server Management Studio, it completes successfully after about 50 seconds.
I've run a SQL Trace using Profiler which shows that the ADO.NET execution is taking way longer than the SQL Server Management Studio one (50 vs 356 seconds).
<Events>
<Event id="13" name="SQL:BatchStarting">
<Column id="1" name="TextData">exec praGetTradesForProductIdReassignment @int4_TradeId=NULL,@int4_FeedId=NULL,@int4_SystemId=NULL,@int4_TradeCode=NULL,@int4_ProductID=NULL,@sortColumn=N'',@sortDirection=0,@pageIndex=0,@pageSize=20</Column>
<Column id="10" name="ApplicationName">Microsoft SQL Server Management Studio - Query</Column>
</Event>
<Event id="12" name="SQL:BatchCompleted">
<Column id="10" name="ApplicationName">Microsoft SQL Server Management Studio - Query</Column>
<Column id="1" name="TextData">exec praGetTradesForProductIdReassignment @int4_TradeId=NULL,@int4_FeedId=NULL,@int4_SystemId=NULL,@int4_TradeCode=NULL,@int4_ProductID=NULL,@sortColumn=N'',@sortDirection=0,@pageIndex=0,@pageSize=20</Column>
<Column id="13" name="Duration">49946289</Column>
</Event>
<Event id="10" name="RPC:Completed">
<Column id="10" name="ApplicationName">.Net SqlClient Data Provider</Column>
<Column id="1" name="TextData">exec praGetTradesForProductIdReassignment @int4_TradeId=NULL,@int4_FeedId=NULL,@int4_SystemId=NULL,@int4_TradeCode=NULL,@int4_ProductID=NULL,@sortColumn=N'',@sortDirection=0,@pageIndex=0,@pageSize=20</Column>
<Column id="13" name="Duration">356352721</Column>
</Event>
</Events>
Why does the ADO.NET execution take so much longer than the SSMS execution?
The purpose of this question is not to improve the performance of the stored procedure, but rather to determine the reason for the difference in performance between an invocation via ADO.NET vs SSMS.