Though it's a quite subjective question but I feel it necessary to share on this forum.
I have personally experienced that when I create a UDF (even if that is not complex) and use it into my SQL it drastically decrease the performance. But when I use SQL inbuild function they happen to work pretty faster. Conversion , logical & string functions are clear example of that.
So, my question is "Why SQL in build functions are faster than UDF"? and it would be an advantage if someone can guide me how can I judge/manipulate function cost either mathematically or logically.
This is a well known issue with scalar UDFs in SQL Server.
They are not inlined into the plan and calling them adds overhead compared with having the same logic inline.
The following takes just under 2 seconds on my machine
Creating the simple scalar UDF
And changing the query to
MAX(dbo.F1(N))
instead ofMAX(N - N)
it takes around 26 seconds withSTATISTICS TIME OFF
and 37 with it on.An average increase of 2.6μs / 3.7μs for each of the 10 million function calls.
Running the Visual Studio profiler shows that the vast majority of time is taken under
UDFInvoke
. The names of the methods in the call stack gives some idea of what the additional overhead is doing (copying parameters, executing statements, setting up security context).Moving the logic into an inline table valued function
And rewriting the query as
executes in as fast as a time as the original query that does not use any functions.