views:

231

answers:

3

I have a sproc that was taking far more time than I expected.

I pulled out the SQL and ran it with just DECLARED variables for the parameters. It ran nearly instantaneously (versus a reliable 8 seconds with the sproc). This is the same SQL on same machine, returning the same data.

How can I figure out and fix what is causing the sproc to be slow?

This is on SQL Server Express.

Thanks much.

+1  A: 

This is parameter sniffing. Just change your procedure to do the same thing: copy all of you parameters into local variables and than have the body of your procedure use the loal variables instead.

RBarryYoung
Thanks RBarryYoung and rexem! Making this change made the sproc perform as expected.
David Montgomery
A: 

Is an app running the stored procedure, or are you running the stored procedure via Query Analyzer?

mrdenny
+1  A: 

In the definition of the stored procedure, check for ANSI_NULLS. Make sure that this is turned on. (SET ANSI_NULLS ON)

By default in the Query Window in SSMS it is turned on. We found a problem where ANSI_NULLS was explicitly set to off and it drastically slowed down the sproc.

IIRC this comes from indexes that the sproc wants to use being built with a different setting (and thus incompatible)
RBarryYoung
SETing certain options (including ANSI_NULLS) within a stored proc can cause SP recompilations - most certainly the cause of your performance issues. See http://support.microsoft.com/kb/243586 for more info
Kev Riley