What’s New in SQL Server 2019: Faster Functions

A while back, we talked you through a public whitepaper about how Microsoft was working on making user-defined functions go faster. Now that the preview of SQL Server 2019 is out, you can start getting your hands on Froid, the performance-boosting feature. Here’s the documentation on it – let’s see how it works.

Using the StackOverflow2010 database, say our company has a scalar user-defined function that we use to calculate how many badges a user has earned:

In this past, performance for this has really sucked. If I grab 1,000 users, and call the function to get their badge counts:

The execution plan looks simple:

2017 plan that hides the scalar’s work

But it’s actually terrible:

  • It doesn’t show all the stuff the scalar function does
  • It wildly underestimates the work involved (scalars have fixed tiny costs regardless of the work they do)
  • It doesn’t show the logical reads performed by the function
  • And of course, it calls that function 1,000 times – once for each user we return


  • Runtime: 17 seconds
  • CPU time: 56 seconds
  • Logical reads: 6,643,089

Now let’s try in SQL Server 2019.

My database has to be in 2019 compat mode to enable Froid, the function-inlining magic. Run the same query again, and the metrics are wildly different:

  • Runtime: 4 seconds
  • CPU time: 4 seconds
  • Logical reads: 3,247,991 (which still sounds bad, but bear with me)

The execution plan looks worse:

2019 plan showing the inlined function’s terribility

Your first thought is probably, “Mother Nature, that plan looks like more work,” but the point is that it’s now showing the work involved in the function. Before, you had to use tools like sp_BlitzCache to figure out which functions were getting called, and how often.

Now, you can see that SQL Server is building a spool, or as Erik likes to say, passively-aggressively building its own missing index on the fly without bothering to tell you that it needs one (note that there’s no missing index request on the plan):

Spoolin’ up

That’s great! That means I can easily fix that just by doing index tuning myself. Granted, I have to know how to do that level of index tuning – but that’s a piece of cake when suddenly the plan makes it more obvious as to what it’s really doing.

How to find out if your functions will go faster

The documentation lists a lot of T-SQL constructs that will or won’t go inline:

But reading that would require you to open the code in your user-defined functions, too, and that’s one of the top causes for data professional suicide. I need all the readers I can get.

So instead, just go download the SQL Server 2019 preview, install it on a test VM, restore your database into it, and run:

There’s a new is_inlineable column that tells you which of your functions can be inlined:


Then to find out which of your functions are actually getting called the most often in production today, use sp_BlitzCache:

Make an inventory of those, and while you’re in there looking at functions, try CPU, Reads, or Memory Grant as different sort orders. Keep an eye out for table variables, high memory grants, or low grants that lead to spills, because SQL Server 2019 improves those too:

That’ll help you build a business case to management. You’ll be able to explain which of your queries are going to magically run faster as soon as you upgrade, and how much of an impact that’ll have on your SQL Server overall – all without changing code. That’s especially powerful for third party apps where you can’t change the code, and you need a performance boost.

Wanna see examples of code that actually gets slower in SQL Server 2019?

Previous Post
Picking #PASSsummit Sessions to Attend
Next Post
PASS Summit 2018 Keynote Day 1: Live Blog

9 Comments. Leave new

  • Daniel Santos
    November 7, 2018 9:24 am

    Great article!

  • Emanuele Meazzo
    November 7, 2018 10:07 am

    Damn, this new release is getting really interesting really fast!

  • You can even do it faster for a milisecond using window function, no need to write a function and bug the serve with it

    • Koen Verbeeck
      November 8, 2018 4:01 am

      The problem with SQL Server was that everything was faster by just writing it in the query itself instead of using a function. Great for performance. Bad for code reuse. It kind of sucks that you had to write every piece of logic multiple times (once for every query that needs it). Fixing a bug could really take some time.

      • Andrej Kuklin
        November 8, 2018 9:39 am

        If you can change the code, then code reuse is easy: Don’t use scalar functions, use inline table-valued functions (which return 1 row in this case). They are transparent to SQL Server and will be integrated in the execution plan.

        • Agree. Refactored all my scalar fns to table valued direct cte return functions instead 10 years ago. No reason to go back to scalar garbages.

  • Michael Sosis
    November 7, 2018 6:53 pm

    Looks like we be skipping 2017 and go straight to the 2019.

  • […] Brent Ozar looks at improvements the SQL Server team has made to scalar functions in 2019: […]

  • […] What’s New in SQL Server 2019 CTP 2.1: Faster Functions A small piece of code where Brent [T] shows how a scalar function works under 2019. […]