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


Stored procedure runs faster than the code it contains...


Stored procedure runs faster than the code it contains...

Author
Message
Erin Ramsay
Erin Ramsay
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1969 Visits: 1119
I have a stored procedure which runs in about 1 second. If I take the code out of the stored procedure and run it by itself it takes about 5 minutes. The execution plan for the stored procedure saves the paralellism to the very end and the plan for the code itself runs the paralellism at various points throughout the plan.

Why would the plans be different, first off? Does SQL treat the stored procedure with different performance enhancements than it does regular sql?
LutzM
LutzM
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22273 Visits: 13559
Do you use exactly the same code as used in the sproc?
Or did you replace a few variables in the query with hardcoded values?

It seems like the compiled version of the sproc is based on a different sample data distribution as the ad-hoc query leading to different execution plans...



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Erin Ramsay
Erin Ramsay
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1969 Visits: 1119
It's exactly the same code, variables included. Part of what's missing is that there is a cartesian product in the code. Basically one of those..."let's look at a million rows where the ID is in (select another million rows)..." so the code, when executed outside the procedure produces an 80 million row select..

That part of the code execution plan is NOT in the procedure execution plan and I suspect that the optimizer might be rewriting that particular part when it's compiling.
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