Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why a simple T-SQL UDF function makes the code execution 3 times slower

I'm rewriting some old stored procedure and I've come across an unexpected performance issue when using a function instead of inline code.

The function is very simple as follow:

ALTER FUNCTION [dbo].[GetDateDifferenceInDays] 
(       
@first_date SMALLDATETIME, 
@second_date SMALLDATETIME
)
RETURNS INT 
AS
BEGIN 

RETURN ABS(DATEDIFF(DAY, @first_date, @second_date))

END

So I've got two identical queries, but one uses the function and the other does the calculation in the query itself:

ABS(DATEDIFF(DAY, [mytable].first_date, [mytable].second_date))

Now the query with the inline code runs 3 times faster than the one using the function.

like image 422
Giuseppe Romagnuolo Avatar asked Aug 07 '09 17:08

Giuseppe Romagnuolo


People also ask

Which values Cannot be returned by a scalar UDF?

For example, a UDF cannot return a value of any of these datatypes: text, ntext, image, cursor, or timestamp.

What is option recompile in SQL Server?

What is the RECOMPILE option? The compilation is the process when a query execution plan of a stored procedure is optimized based on the current database objects state. This query execution plan is often stored in the cache to be quickly accessed. Recompilation is the same process as a compilation, just executed again.


2 Answers

What you have is a scalar UDF ( takes 0 to n parameters and returns a scalar value ). Such UDFs typically cause a row-by-row operation of your query, unless called with constant parameters, with exactly the kind of performance degradation that you're experiencing with your query.

See here, here and here for detailed explanations of the peformance pitfalls of using UDFs.

like image 151
nagul Avatar answered Nov 15 '22 05:11

nagul


Depending on the usage context, the query optimizer may be able to analyze the inline code and figure out a great index-using query plan, while it doesn't "inline the function" for similarly detailed analysis and so ends up with an inferior query plan when the function is involved. Look at the two query plans, side by side, and you should be able to confirm (or disprove) this hypothesis pretty easily!

like image 5
Alex Martelli Avatar answered Nov 15 '22 04:11

Alex Martelli