Product articles SQL Prompt SQL Code Analysis
Misuse of the scalar user-defined…

15 February 2018

4 Comments

Guest post

This is a guest post from Phil Factor. Phil Factor (real name withheld to protect the guilty), aka Database Mole, has 30 years of experience with database-intensive applications.

Despite having once been shouted at by a furious Bill Gates at an exhibition in the early 1980s, he has remained resolutely anonymous throughout his career.

He is a regular contributor to Simple Talk and SQLServerCentral.

15 February 2018

4 Comments

Misuse of the scalar user-defined function as a constant (PE017)

The incorrect use of a scalar UDF as a global database constant is a major performance problem and should be investigated whenever SQL Prompt spots this in any production code. Unless you need to use these global constants in computed columns or constraints, it is generally safer and more convenient to store the value in an inline table-valued function, or to use a view.

Guest post

This is a guest post from Phil Factor. Phil Factor (real name withheld to protect the guilty), aka Database Mole, has 30 years of experience with database-intensive applications.

Despite having once been shouted at by a furious Bill Gates at an exhibition in the early 1980s, he has remained resolutely anonymous throughout his career.

He is a regular contributor to Simple Talk and SQLServerCentral.

Developers tend to expect to be able to set global values in a database to provide constants such as the value of Pi, or variables such as the tax rate, language, file URNs or URLs. User-defined scalar functions return a single value, and so seem to provide the ideal way of doing so. This is fine for functions that are executed infrequently, and process relatively small data sets, but in other cases it can cause dramatic query performance problems. The problem arises because SQL Server doesn’t trust non-schema verified scalar functions as being precise and deterministic, and so chooses the safest, though slowest, option when executing them.

There is an overhead in calling any SQL Server function that has a BEGIN…END block because, unless we allow SQL Server to verify its output, by creating the function using schema binding, it will re-execute the function for every row, before data can be filtered, even though it is obvious to you that it will return the same value every time. It’s a slightly insidious problem because it doesn’t really show its full significance in the execution plan, though an Extended Events session will reveal what is really going on.

In short, do not use a scalar user-defined function (UDF) in a JOIN condition, WHERE search condition, or in a SELECT list, unless the function is schema-bound. SQL Prompt implements static code analysis rule, PE017, designed precisely to help you detect and rectify this problem. Unless you are confident with schema binding, and its consequences when making database changes, it is better to use either transfer the value to a variable, or use a module such as a view or inline Table-value function.

Tackling the Problem

If SQL Prompt detects that your code flouts PE017, what should you do?

We’ll set up all of the possible options, run some performance tests, and make some recommendations.

Schema-qualifying UDFs

A scalar function can be used correctly by adding schema binding to ensure that it is system-verified. Listing 1 creates two versions of the same, simple Wordcount function, first without and then with schema binding, both of which simple return a constant. In each case, we check the IsDeterministic, IsPrecise and IsSystemVerified property values for each object.

Finally, it creates a third version that simply returns its parameter value, just to check to see if this is a factor in SQL Server’s verification process.

Listing 1

If you run Listing 1, you’ll see that only the second version of the function, WordCountSchemaBound, returns true for the three properties. We’ll see a little later just how much this affects the performance of any queries that call these functions.

Although schema-binding has many advantages, it means, in this case, that you’ll be explicitly prevented from treating the constant as a variable, which is no bad thing. If you alter the ‘constant’ function, which is being you’ve used in constraints or computed columns within tables, it will prove to be complicated. Also, if you try to change the constant when the database is working, the plans that use the function that are being executed will place schema stability locks on the function and this will prevent you changing the value of the constant, as they require schema modification lock.

Alternatives to scalar UDFs

Listing 2 shows a couple of alternatives to scalar UDFs to hold database-wide values, in cases where you can’t or don’t want to schema-bind them; first a view, and then a table-valued function.

Listing 2

The objects referred to in the view definition can’t be altered in a way that would make the view definition illegal, or force SQL Server to recreate the index on the view.

I’ve left out the alternative of using a table for holding your constants despite the extra protection of CHECK constraints. Tables just aren’t designed to be immutable! As a spoiler, I’ll tell you that they perform as well as a view.

The Performance tests

Having lined up all the candidate solutions, let’s see how they perform. I’m going to test how quickly each of the options can find out how many five-letter words there are in common usage in the English language. The tests require us to create a simple Commonwords table consisting of a single column (primary key) of all the common words. To populate it, you’ll need to download the commonwords file, then run Listing 3, with the correct path to the file.

Listing 3

For the timings, I’m going to use the simple test harness described in my article, How to record T-SQL execution times using a SQL Prompt snippet.

Listing 4

When we run this, we verify that all the forms of using a constant in a query produce the same results. The times show very clearly what the problem is, and the dramatic extent of it

A graph is scarcely necessary to emphasize the horror of PE017 – Incorrect usage of const UDF. The way SQL Server executes a scalar UDF that isn’t schema-bound, and therefore unverified, is so cautious (for every row, it asks “is it still returning 5?“) that it is fifty times slower.

Apart from avoiding use of non-schema bound scalar UDFs, the test showed that on average there is no real difference in performance between any of the other ways of getting a constant value to a query. The query execution plan is the same in each case.

Now, we end by using a tear-down section to leave everything tidy in our test database.

Listing 5

Recommendations

If you use an unverified scalar function, then the query will be grindingly slow because you will be executing it on every row whether it has a parameter or not.

If you are faced with a vast tangle of inherited code that is using scalar UDFs as global constants, then redo them with schema binding. However, if these are global variables, which change infrequently in a live system, then I wouldn’t think of that alternative because you can’t alter a schema-bound function without temporarily altering every table that uses it in a constraint or computed column, to remove them, alter the function, and then replace the constraints and computed column.

Views or TVFs are more versatile, and so I’d me more inclined to use them to hold ‘global’ values. If these are changed, the change is logged because they require a DDL change. The only problem is that only scalar functions can be used in constraints or computed columns. If you use a table, then fine, but remember that changing a constant isn’t a DDL change and so you must set up access permissions to deny anyone the right to change, for example, the tax rate!

You may also like

  • Article

    Removing the Square Bracket Decorations with SQL Prompt

    If you avoid illegal characters and reserved words in your identifiers, you'll rarely need delimiters. Sadly, SSMS applies square bracket delimiters indiscriminately, as a precaution, when generating build scripts. Phil Factor provides a handy function that adds quoted delimiters only where they are really needed and then sits back and lets SQL Prompt strip out any extraneous square brackets, in a flash.

  • Article

    Quickly rename variables all at once with SQL Prompt

    You’re going to run into situations where someone has made poor choices in naming objects, or maybe the business logic has changed and the object name no longer makes sense. Either way, easily and quickly taking care of renaming the objects within scripts can be a pain. Let’s take this code as an example: The

  • Article

    SQL Prompt to the rescue with tab coloring

    When you’re hard at work, it’s all too easy to make a mistake as you rush to get a job done. Say, for example, you have a QA database you need to get rid of. You run a query to drop it and then… oh no! You’ve accidentally run it in Production. Your day just

  • Article

    Quick SQL Prompt tip – script objects as ALTER in two clicks

    Working in a large database can be difficult at times. While many of us might learn the meanings and definitions of most objects, it’s easy to forget the exact ways in which some objects work, or what the behavior is in certain calls. This is one place where having tools that assist you like SQL

  • Forums

    SQL Prompt Forum

    Write, format, and refactor SQL effortlessly