473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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é
Jun 27 '08 #1
5 4407
On Thu, 17 Apr 2008 02:29:07 -0700 (PDT), René
<re**********@googlemail.comwrote:

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é
Jun 27 '08 #2
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é
Jun 27 '08 #3
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

Jun 27 '08 #4
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.

Jun 27 '08 #5
On Thu, 17 Apr 2008 07:38:16 -0700 (PDT), René
<re**********@googlemail.comwrote:

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é
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Michael Rybak | last post by:
hi, everyone. I'm writing a 2-players game that should support network mode. I'm now testing it on 1 PC since I don't have 2. I directly use sockets, and both client and server do...
9
by: Gibby Koldenhof | last post by:
Hiya, Terrible subject but I haven't got a better term at the moment. I've been building up my own library of functionality (all nice conforming ISO C) for over 6 years and decided to adopt a...
6
by: sathyashrayan | last post by:
Following are the selected thread from the date:30-jan-2005 to 31-jan-2005. I did not use any name because of the subject is important. You can get the original thread by typing the subject...
22
by: TC | last post by:
I have an Access database application with a lot of custom row functions written in VBA. In other words, a lot of queries contain calculated fields which use functions defined in the modules. I...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.