Unix Technical Forum

SQL Server 2005: functions become slow after a while

This is a discussion on SQL Server 2005: functions become slow after a while within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I'm having serious issues with our user defined functions. They get very slow after a while. The functions ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 05:08 PM
=?ISO-8859-1?Q?Ren=E9?=
 
Posts: n/a
Default SQL Server 2005: functions become slow after a while

Hi,

I'm having serious issues with our user defined functions. They get
very slow after a while. The functions I use have several IF-branches
which check input parameters. Always exactly one branch will be
executed.
Normally, the execution time of my functions is something like 300ms.
After a while though, it climbs up to 20 seconds (!!).
The interesting thing about it is now: if I just alter the function by
adding a blank somewhere it will be fast again. What the hell is going
on here?

To me it seems that 2005 is optimizing the function incorrectly. It is
executed on very different result sets (some are extremly small, some
are extensivly large). I can imagine that for one case hashed indexing
is better and for the other one nested loops. So maybe it sticks to
one ooptimization after a while? The problem is that I cannot really
prove it because I cannot force the behavior. It occurs suddenly it
won't go away until I perform the "add blank" action.

The server is a 8 processor DB cluster with 12GB of RAM, all SPs
installed (SP2 for 2005).

Any hints what to do?

René
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 05:08 PM
Tom van Stiphout
 
Posts: n/a
Default Re: SQL Server 2005: functions become slow after a while

On Thu, 17 Apr 2008 02:29:07 -0700 (PDT), René
<rene.ruppert@googlemail.com> wrote:

sp_recompile is typically used to get a new execution plan. On very
dynamic systems it may have to be run daily. On others still not a bad
idea to run it once every week.

-Tom.


>Hi,
>
>I'm having serious issues with our user defined functions. They get
>very slow after a while. The functions I use have several IF-branches
>which check input parameters. Always exactly one branch will be
>executed.
>Normally, the execution time of my functions is something like 300ms.
>After a while though, it climbs up to 20 seconds (!!).
>The interesting thing about it is now: if I just alter the function by
>adding a blank somewhere it will be fast again. What the hell is going
>on here?
>
>To me it seems that 2005 is optimizing the function incorrectly. It is
>executed on very different result sets (some are extremly small, some
>are extensivly large). I can imagine that for one case hashed indexing
>is better and for the other one nested loops. So maybe it sticks to
>one ooptimization after a while? The problem is that I cannot really
>prove it because I cannot force the behavior. It occurs suddenly it
>won't go away until I perform the "add blank" action.
>
>The server is a 8 processor DB cluster with 12GB of RAM, all SPs
>installed (SP2 for 2005).
>
>Any hints what to do?
>
>René

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 05:08 PM
=?ISO-8859-1?Q?Ren=E9?=
 
Posts: n/a
Default Re: SQL Server 2005: functions become slow after a while

> sp_recompile is typically used to get a new execution plan. On very
> dynamic systems it may have to be run daily. On others still not a bad
> idea to run it once every week.



The problem is that in my case the SP/function is fast at 8AM and slow
at 9AM.Then I add the blank (=recompile) and then it may work for a
day or maybe only for 30 minutes...I really cannot do a recompile
every 30 minutes, can I?

René
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 05:08 PM
Plamen Ratchev
 
Posts: n/a
Default Re: SQL Server 2005: functions become slow after a while

This looks like parameter sniffing. Read the following articles on possible
solutions:
http://blogs.msdn.com/khen1234/archi...02/424228.aspx
http://www.sqlmag.com/Article/Articl...ver_94369.html

Using OPTION(RECOMPILE) on the particular query will cause only that query
to be recompiled instead of all queries in the function.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-18-2008, 09:30 AM
--CELKO--
 
Posts: n/a
Default Re: SQL Server 2005: functions become slow after a while

Look at the WITH RECOMPILE option for stored procedures. In T-SQL,
the first time a procedure is compiled, it looks at the parameters and
assumes that they are gospel; it optimizes for those values. DB2 and
other SQL products you might have worked with can store many different
execution plans and the pick the best one for each call to the
procedure.

Another piece of advice is to avoid user defined functions
altogether. This is proprietary syntax, a return to procedural
coding, and you can get the same things with pure SQL most of the
time.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-18-2008, 09:30 AM
Tom van Stiphout
 
Posts: n/a
Default Re: SQL Server 2005: functions become slow after a while

On Thu, 17 Apr 2008 07:38:16 -0700 (PDT), René
<rene.ruppert@googlemail.com> wrote:

You left out that gem. In one hour you go from msecs to dozens of
seconds? That's just wrong.
Look at an execution plan when it's fast, and another one when it's
slow.
Is your db very dynamic? For example if you have a big data load at
08:30, indeed the execution plan wouldn't be much good at 9:00.

-Tom.



>> sp_recompile is typically used to get a new execution plan. On very
>> dynamic systems it may have to be run daily. On others still not a bad
>> idea to run it once every week.

>
>
>The problem is that in my case the SP/function is fast at 8AM and slow
>at 9AM.Then I add the blank (=recompile) and then it may work for a
>day or maybe only for 30 minutes...I really cannot do a recompile
>every 30 minutes, can I?
>
>René

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:22 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com