Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a simple code segment that runs in about 1 second when run alone, but more than 5 minutes when deployed as a stored procedure. I'm using sql server 2008.

Unfortunately, I can't post the code due to company policy. Is there a general explanation of this behavior?

share|improve this question

closed as unclear what you're asking by Mark Storey-Smith, Phil, ypercubeᵀᴹ, Max Vernon, Paul White Jun 20 '14 at 0:13

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Hang on - I'll just deploy the MS SQL Server Crystal Ball module (TM) and your problem will disappear! Sorry about the sarcasm, but your question is a bit like saying "I'm going somewhere but I can't tell you where, can you help me to get there?". Change field names - sanitise data - only include key fields, but with no information, you can't expect to receive much assistance. – Vérace Jun 18 '14 at 14:39
Can't really be certain without the code and execution plan (could you post the plan for both?). This may also be worth a read: – swasheck Jun 18 '14 at 14:40
It was a parameter sniffing issue. Thanks zlatan! – Thomas Cleberg Jun 18 '14 at 14:48
@Vérace I understand, that is why I specified a general solution, which zlatan was able to help with. Thanks for the lesson! – Thomas Cleberg Jun 18 '14 at 14:49
It's probably what you already found out, but don't take it that parameter sniffing is bad, cause it isn't. Please read Kendra's article Why You’re Tuning Stored Procedures Wrong (the Problem with Local Variables), too. – Marian Jun 19 '14 at 7:31