SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 why it behaves like this?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

249 Posts

Posted - 04/08/2013 :  12:24:47  Show Profile  Reply with Quote
Hi there,

I have a store procedure with 5 parameters. It runs slow. If I declare and init these 5 parameters inside the sp and highlight the scripts to run it, it runs fast, about 1 sec. But if I run it with statement as:

exec mysp p1, p2, p3 ..

it runs but takes 2 min to finish. not showing any error. What could be the reasons to cause this? Thanks in advance.

James K
Flowing Fount of Yak Knowledge

3704 Posts

Posted - 04/08/2013 :  12:29:50  Show Profile  Reply with Quote
Most likely the stored proc ends up using a stored execution plan that is not the most appropriate for the given set of parameters. People refer to this as "parameter-sniffing" problem - you will find references if you google for it. Examine the query plan and adding statement level recompilations at the approriate places may help.
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 04/08/2013 :  17:28:08  Show Profile  Reply with Quote
Thanks James, it's very helpful. I ran the query plan and found warnings showing MISSING INDEX ... then I did research on Google got a very good source at http://sqlfool.com/2009/04/a-look-at-missing-indexes/
... then problem solved, it runs FAST now.
THANKS again.

Edited by - allan8964 on 04/08/2013 17:28:57
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 04/09/2013 :  00:02:21  Show Profile  Reply with Quote
https://www.simple-talk.com/sql/t-sql-programming/parameter-sniffing/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000