PC Review


Reply
Thread Tools Rate Thread

ADO.NET query execution much slower than SQL Management Studio

 
 
dustbort
Guest
Posts: n/a
 
      22nd Feb 2008
I have a stored procedure that when called from SQL 2005 Management Studio
takes less than one second to execute. The same SP, when called from .NET
code takes about 13 seconds. I am using a SqlCommand object with
CommandType set to StoredProcedure and I am passing arguments through the
parameters collection. I have tried using a SqlDataReader and a DataAdapter
to retrieve the data, but both are equally slow. From stepping thru the
debugger, I know that the specific statement that takes a long time to
execute is either reader.ExecuteReader() or dataAdapter.Fill(dataTable),
equivalently depending on the method I tried. I did a trace in the
Profiler, and got nearly identical result for either method of .NET
SqlClient Data Provider. Here is an example:

EventClass: RPC:Completed
CPU: 13390
Reads: 559475
Writes: 0
Duration: 13496
Binary Data: (a long hex value)

When I copied the TextData (SQL Statement being executed) from Profiler into
SQL Management Studio, I get the following trace:

EventClass: SQL:BatchCompleted
CPU: 437
Reads: 9998
Writes: 0
Duration: 440
BinaryData: (empty)

(Immediately prior to this there is a corresponding SQL:BatchStarting trace,
with empty CPU, Reads, Writes, and Duration columns.)

What could explain the orders of magnitude difference in reads and duration?
Is the problem due to RPC? What about the binary data? I have tried using
the overload of ExecuteReader(CommandBehavior.SingleResult) with no
improvement. What can I try to improve it?

Thanks,
Dustin


 
Reply With Quote
 
 
 
 
Peter Bromberg [C# MVP]
Guest
Posts: n/a
 
      22nd Feb 2008
Hate to say this, but it doesn't compute. Look carefully for a bug in your
code, connections being closed properly, etc.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: htp://petesbloggerama.blogspot.com
Short Urls & more: http://ittyurl.net


"dustbort" wrote:

> I have a stored procedure that when called from SQL 2005 Management Studio
> takes less than one second to execute. The same SP, when called from .NET
> code takes about 13 seconds. I am using a SqlCommand object with
> CommandType set to StoredProcedure and I am passing arguments through the
> parameters collection. I have tried using a SqlDataReader and a DataAdapter
> to retrieve the data, but both are equally slow. From stepping thru the
> debugger, I know that the specific statement that takes a long time to
> execute is either reader.ExecuteReader() or dataAdapter.Fill(dataTable),
> equivalently depending on the method I tried. I did a trace in the
> Profiler, and got nearly identical result for either method of .NET
> SqlClient Data Provider. Here is an example:
>
> EventClass: RPC:Completed
> CPU: 13390
> Reads: 559475
> Writes: 0
> Duration: 13496
> Binary Data: (a long hex value)
>
> When I copied the TextData (SQL Statement being executed) from Profiler into
> SQL Management Studio, I get the following trace:
>
> EventClass: SQL:BatchCompleted
> CPU: 437
> Reads: 9998
> Writes: 0
> Duration: 440
> BinaryData: (empty)
>
> (Immediately prior to this there is a corresponding SQL:BatchStarting trace,
> with empty CPU, Reads, Writes, and Duration columns.)
>
> What could explain the orders of magnitude difference in reads and duration?
> Is the problem due to RPC? What about the binary data? I have tried using
> the overload of ExecuteReader(CommandBehavior.SingleResult) with no
> improvement. What can I try to improve it?
>
> Thanks,
> Dustin
>
>
>

 
Reply With Quote
 
Paul Shapiro
Guest
Posts: n/a
 
      23rd Feb 2008
If you first ran the procedure from your .net code, and then ran it from SQL
Management Studio, it's possible the data and/or stored procedure execution
plan were cached from the first run in .net. Try clearing the caches before
running in SQL Management, or try executing the same procedure twice in a
row from .net.
SQL commands to clear procedure and data caches:
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS

If you still have the issue, make sure sql profiler is showing enough detail
so you can see how the data is being moved in the two cases.

"Peter Bromberg [C# MVP]" <(E-Mail Removed)> wrote in message
news:A6ABED66-2158-401A-98DA-(E-Mail Removed)...
> Hate to say this, but it doesn't compute. Look carefully for a bug in your
> code, connections being closed properly, etc.
> -- Peter
> Site: http://www.eggheadcafe.com
> UnBlog: htp://petesbloggerama.blogspot.com
> Short Urls & more: http://ittyurl.net
>
>
> "dustbort" wrote:
>
>> I have a stored procedure that when called from SQL 2005 Management
>> Studio
>> takes less than one second to execute. The same SP, when called from
>> .NET
>> code takes about 13 seconds. I am using a SqlCommand object with
>> CommandType set to StoredProcedure and I am passing arguments through the
>> parameters collection. I have tried using a SqlDataReader and a
>> DataAdapter
>> to retrieve the data, but both are equally slow. From stepping thru the
>> debugger, I know that the specific statement that takes a long time to
>> execute is either reader.ExecuteReader() or dataAdapter.Fill(dataTable),
>> equivalently depending on the method I tried. I did a trace in the
>> Profiler, and got nearly identical result for either method of .NET
>> SqlClient Data Provider. Here is an example:
>>
>> EventClass: RPC:Completed
>> CPU: 13390
>> Reads: 559475
>> Writes: 0
>> Duration: 13496
>> Binary Data: (a long hex value)
>>
>> When I copied the TextData (SQL Statement being executed) from Profiler
>> into
>> SQL Management Studio, I get the following trace:
>>
>> EventClass: SQL:BatchCompleted
>> CPU: 437
>> Reads: 9998
>> Writes: 0
>> Duration: 440
>> BinaryData: (empty)
>>
>> (Immediately prior to this there is a corresponding SQL:BatchStarting
>> trace,
>> with empty CPU, Reads, Writes, and Duration columns.)
>>
>> What could explain the orders of magnitude difference in reads and
>> duration?
>> Is the problem due to RPC? What about the binary data? I have tried
>> using
>> the overload of ExecuteReader(CommandBehavior.SingleResult) with no
>> improvement. What can I try to improve it?


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO.NET RPC Slower than Management Studio Dereck Microsoft ADO .NET 2 24th Apr 2009 10:20 PM
ADO.NET query execution much slower than SQL Management Studio dustbort Microsoft ADO .NET 5 11th Mar 2008 02:17 PM
Query runs slow from web app, but not from management studio Jeremy Chapman Microsoft ADO .NET 8 26th Jun 2006 04:59 PM
Execution in Visual Studio vs Native Execution =?Utf-8?B?SmFjbyBSYXViZW5oZWltZXI=?= Microsoft Dot NET Framework 0 1st Jun 2005 09:46 AM
ADO.NET Query Execution time is 10 times slower than via Query Analyzer Oleg Microsoft ADO .NET 11 11th Feb 2005 08:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:34 AM.