views:

4288

answers:

6

Why would a stored procedure that returns a table with 9 columns, 89 rows using this code take 60 seconds to execute (.NET 1.1) when it takes < 1 second to run in SQL Server Management Studio? It's being run on the local machine so little/no network latency, fast dev machine

Dim command As SqlCommand = New SqlCommand(procName, CreateConnection())
command.CommandType = CommandType.StoredProcedure
command.CommandTimeout = _commandTimeOut
Try
   Dim adapter As new SqlDataAdapter(command)
   Dim i as Integer
   For i=0 to parameters.Length-1
      command.Parameters.Add(parameters(i))
   Next
   adapter.Fill(tableToFill)
   adapter.Dispose()
Finally
   command.Dispose()
End Try

my paramter array is typed (for this SQL it's only a single parameter)

parameters(0) = New SqlParameter("@UserID", SqlDbType.BigInt, 0, ParameterDirection.Input, True, 19, 0, "", DataRowVersion.Current, userID)

The Stored procedure is only a select statement like so:

ALTER PROC [dbo].[web_GetMyStuffFool]
   (@UserID BIGINT)
AS
SELECT Col1, Col2, Col3, Col3, Col3, Col3, Col3, Col3, Col3
FROM [Table]
A: 

Why not make it a DataReader instead of DataAdapter, it looks like you have a singel result set and if you aren't going to be pushing changes back in the DB and don't need constraints applied in .NET code you shouldn't use the Adapter.

EDIT:

If you need it to be a DataTable you can still pull the data from the DB via a DataReader and then in .NET code use the DataReader to populate a DataTable. That should still be faster than relying on the DataSet and DataAdapter

Marcus King
The code I'm working with can't return anything other than a datatable for this sproc.
Steve Wright
A: 

I don't know "Why" it's so slow per se - but as Marcus is pointing out - comparing Mgmt Studio to filling a dataset is apples to oranges. Datasets contain a LOT of overhead. I hate them and NEVER use them if I can help it.

You may be having issues with mismatches of old versions of the SQL stack or some such (esp given you are obviously stuck in .NET 1.1 as well) The Framework is likely trying to do database equivilant of "Reflection" to infer schema etc etc etc

One thing to consider try with your unfortunate constraint is to access the database with a datareader and build your own dataset in code. You should be able to find samples easily via google.

fuzzbone
and please use some using statements since your connection implements IDisposable
Ray Booysen
+13  A: 

First, make sure you are profiling the performance properly. For example, run the query twice from ADO.NET and see if the second time is much faster than the first time. This removes the overhead of waiting for the app to compile and the debugging infrastructure to ramp up.

Next, check the default settings in ADO.NET and SSMS. For example, if you run SET ARITHABORT OFF in SSMS, you might find that it now runs as slow as when using ADO.NET.

What I found once was that SET ARITHABORT OFF in SSMS caused the stored proc to be recompiled and/or different statistics to be used. And suddenly both SSMS and ADO.NET were reporting roughly the same execution time.

To check this, look at the execution plans for each run, specifically the syscacheobjects table. They will probably be different.

Finally, you can try cleaning out procedure cache and memory buffers using SSMS:

DBCC DROPCLEANBUFFERS
DBCC FREEPROCCACHE

Doing so before you test your query prevents usage of cached execution plans and previous results cache.

RoadWarrior
DBCC DROPCLEANBUFFERSDBCC FREEPROCCACHE solved my problem. Does this need to be ran on a recurring basis, or only when changes are made to the stored procedure?
Merritt
It's on a recurring basis, as these are cached results and plans that will gradually accumulate.
RoadWarrior
Great answer, my SP execution time went from 117s to 3s! +1
Carl
The "SET ARITHABORT OFF" was a great answer! Now I get a consistent experience between ADO.NET as SSMS. In my case I just needed to rebuild one index which I did from the SSMS IDE.
Carter
A: 

RoadWarrior is correct. The other answers are ok, but when I ran the dropcleanbuffers and freeproccache, our speed issues in or WEB using ADO.Net evaporated. I've been pulling my hair out for the last 3 or four weeks with this issue. We are talking about 3-4 mintues for something that takes 4 or 5 seconds usually. People were really complaining. Thanks, very much, for the help.

Peter Grigg
You're allowed to up-vote me then :-)
RoadWarrior
yes, RoadWarrior's answer was correct and ultimately the cause of the problem.
Steve Wright
+1  A: 

Here is what I ended up doing:

I executed the following SQL statement to rebuild the indexes on all tables in the database:

EXEC <databasename>..sp_MSforeachtable @command1='DBCC DBREINDEX (''*'')', @replacechar='*'
-- Replace <databasename> with the name of your database

If I wanted to see the same behavior in SSMS, I ran the proc like this:

SET ARITHABORT OFF
EXEC [dbo].[web_GetMyStuffFool] @UserID=1
SET ARITHABORT ON

Another way to bypass this is to add this to your code:

MyConnection.Execute "SET ARITHABORT ON"
Steve Wright
A: 

I had the same issue and got it fixed using this article: http://www.databasejournal.com/features/mssql/article.php/3841271/T-SQL-Best-Practices--Parameter-Sniffing.htm

sujeewa