SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Techniques for improving stored procedure performance


Techniques for improving stored procedure performance

Author
Message
Ray K
Ray K
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3805 Visits: 4645
Hi, SQL land . . .

I realize that this is a general and vague question; for this, I apologize in advance. I'm looking to educate myself, and would appreciate any advice, tips, tricks, and helpful links.

Here's my scenario: I have SQL code that I'm writing (that is getting increasingly complex).

When I run it in SSMS as a standalone query, it flies. I get my results in just a few seconds.

However, when I put the exact same query into a stored procedure (no changes to the code AT ALL, other than putting it into a stored proc and testing it in another query window), it slows to a crawl -- 30+ seconds.

I've already specified the WITH RECOMPILE option. Is there anything else I should look into?

+--------------------------------------------------------------------------------------+
‌Check out my blog at https://pianorayk.wordpress.com/
Eugene Elutin
Eugene Elutin
SSCertifiable
SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)SSCertifiable (5K reputation)

Group: General Forum Members
Points: 5030 Visits: 5478
Does your proc have input parameters which then used directly in WHERE clauses or JOINS?
If so, try to declare local variables at the top of proc, assign them to input param values and use them instead. You may find it will help you better than using WITH RECOMPILE.
Do you run both ways in SSMS?

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88172 Visits: 45277
Parameter sniffing, or possibly lack thereof would be my first guess, but really need to see the procedure to say for sure.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Ray K
Ray K
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3805 Visits: 4645
Hey Gail -- unfortunately, I'm unable to post the code for security reasons. I suppose I could do a "protect the innocent" rewrite, but alas, I don't have time for that right now.

Eugene Elutin (8/23/2013)
Does your proc have input parameters which then used directly in WHERE clauses or JOINS?
If so, try to declare local variables at the top of proc, assign them to input param values and use them instead. You may find it will help you better than using WITH RECOMPILE.
Do you run both ways in SSMS?


I have input parameters for @startdate and @enddate, but I default them both to null in the stored proc. In my code, I set them to 1/1/1900 and getdate(), respectively. To be honest, I'm not even sure that they're needed, and I might actually take them out completely.

+--------------------------------------------------------------------------------------+
‌Check out my blog at https://pianorayk.wordpress.com/
GilaMonster
GilaMonster
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88172 Visits: 45277
Ray K (8/23/2013)
I default them both to null in the stored proc. In my code, I set them to 1/1/1900 and getdate(), respectively.


And that's your problem right there.
http://sqlinthewild.co.za/index.php/2008/05/22/parameter-sniffing-pt-3/

If you're going to change the values of parameters, you need to use variables instead. Otherwise the plan is compiled for parameter values of NULL and when it runs with something non-null you have a really, really inappropriate execution plan

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Ray K
Ray K
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3805 Visits: 4645
GilaMonster (8/23/2013)
Ray K (8/23/2013)
I default them both to null in the stored proc. In my code, I set them to 1/1/1900 and getdate(), respectively.


And that's your problem right there.
http://sqlinthewild.co.za/index.php/2008/05/22/parameter-sniffing-pt-3/

If you're going to change the values of parameters, you need to use variables instead. Otherwise the plan is compiled for parameter values of NULL and when it runs with something non-null you have a really, really inappropriate execution plan

And lo and behold, that did it.

Thanks for your help, Gail, as always! :-)

+--------------------------------------------------------------------------------------+
‌Check out my blog at https://pianorayk.wordpress.com/
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search