Brian Hansen takes a look at the scalar UDF performance improvements in SQL Server 2019:
In one of my sessions, Set Me Up: How to Think in Sets, I discuss a variety of performance-inhibiting query constructs, including scalar UDFs. I thought it would be interesting to take the simple scalar function that I use in the demo and see what kind of difference that scalar inlining might make.
First, I restored the CorpDB database that I use in the session to my SQL Server 2019 CTP 2.1 instance and initially set the compatibility level to 140. I also ran script 001 from the demo to create the needed database tables (no need to create the CLR objects for this test). I then ran script 030 to execute the scalar UDF test. In a nutshell, this script
-
creates a UDF
-
runs a query that calls the UDF about 13,000 times, capturing the time required to do so
-
repeated this test five times
-
discards the fastest and slowest tests
-
reports the average time for the remaining three tests
If I’m reading Brian’s notes right, it’s still slower than writing the set-based solution yourself, but a huge improvement over the prior scalar function performance.