Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Very slow SP, but query is very quick

Author  Topic 

cedubose
Starting Member

22 Posts

Posted - 2009-12-08 : 15:39:08
Does anyone have any idea why this is happening:

We have a stored procedure that is taking several minutes to execute (lots of decryptions of encrypted data; many records). In investigating it, I copied the script for the stored procedure to a new query window, substituted local variables for the parameters, and gave them the same values I was using for the SP.

I then ran it, and it ran in 4 seconds. But if I right-click on the stored procedure (in Management Studio) and select "run", it takes over 5 minutes to run!

EXACTLY the same code -- not one character changed.

Why would this be happening, any ideas??


Cynthia

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-12-08 : 16:08:20
There are a couple of reasons why this could happen:
1. Parameter sniffing (do a search for info on that)
2. Bad execution plan on the stored procedure

For #2, compare the execution plan of the stored procedure to what you are getting in your adhoc query.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-12-08 : 16:45:02
Do an sp_recompile...maye it will flush out a bad plan and use a better one

How big is the database?

And 4 seconds isn't really that fast in any case

How much data is coming back?

Why not post the Sproc so we can have a look see





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

cedubose
Starting Member

22 Posts

Posted - 2009-12-08 : 17:12:58
It turns out it's the "parameter sniffing" thing. Strange -- I never knew there was such a thing. When I implement the fix whereby you copy the parameters to local variables, the execution time of the stored procedure drops to a few seconds.


Cynthia
Go to Top of Page
   

- Advertisement -