Techniques for improving stored procedure performance

  • 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/

  • 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[/url]

  • 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
  • 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/

  • 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
  • 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/

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply