Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2005
    Posts
    29

    Unanswered: In-line code Vs Sproc. Very different runtime.

    Hi Guys,

    I'm looking into why a particular report is running very slowly.
    I call the stored proc which populates the report from query analyzer and request get an execution plan. Runtime is ~12mins

    When I take the code inside the stored proc and convert it to inline code (change the parameter declarations to declare statements and procedure call parameters to set statements) and request an execution plan, the runtime is 10 seconds. The results returned are identical.

    Comparing the execution plans shows the difference to lie in the SP using one index (returns 100m rows) and the inline code using another (2.5k rows). Apart from this index and the cost for some elements changing by a couple of percentage points the execution plans look very similar.

    I've held off pasting the table schema and stored proc in here due to size.

    One thing I'd hoped would help, but which didn't make an appreciable change, was to call the stored proc 'with recompile' in case the issue was an old execution plan.

    Any suggestions how to stop the query planner making this 'bad' choice? Even some clue as to the source of the difference between the two functionally identical pieces of code might help.

  2. #2
    Join Date
    May 2004
    Location
    Seattle
    Posts
    1,313
    maybe it's because of parameter sniffing? Have a look here: http://blogs.msdn.com/khen1234/archi...02/424228.aspx

  3. #3
    Join Date
    Jan 2005
    Posts
    29
    Thanks for the reply jezemine. The article was an enlightening read.

    Quote Originally Posted by jezemine
    maybe it's because of parameter sniffing?
    While this may be the problem I don't think it is since when I ran the sp with the 'with recompile' option the behaviour didn't change.
    If I've understood the article correctly the execution plan produced when the value passed into the sp got sniffed would be the same as the inline one, and it's not. Or am I missing something?

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    have you tried using an index hint with the stored procedure?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Jan 2005
    Posts
    29
    Quote Originally Posted by Thrasymachus
    have you tried using an index hint with the stored procedure?
    I hadn't, but after reading up on their usage and finding the problem join (it was in a function called by the stored proc) an index hint dropped the report execution time to the couple of seconds I was hoping for.

    Thanks Thrasymachus!

  6. #6
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Go ahead and post the code for the sproc. I'm guessing there are a lot of conditionan predicates....
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  7. #7
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Quote Originally Posted by Thrasymachus
    have you tried using an index hint with the stored procedure?
    ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

    EDIT: I've only once had to do this, and it was in 6.5
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  8. #8
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    not a perfect solution, and it is rarely the right one, but on a rare occasion i pull it out of the hat.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  9. #9
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    Quote Originally Posted by Thrasymachus
    not a perfect solution, and it is rarely the right one, but on a rare occasion i pull it out of the hat.

    You a betting man?

    Wanna bet the OP's problem is self inflicted
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  10. #10
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    i did not say he wasn't dorking something up in this case, but he got his combo meal. thank you, drive through.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  11. #11
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    ....would you like fries with that?

    would you like to super-size that?
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

  12. #12
    Join Date
    Jun 2004
    Location
    Long Island
    Posts
    696
    Wouldja ? - Jaime Gumb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •