Avoiding UDF pitfalls

Scalar User-Defined Functions (UDFs) are a very useful T-SQL programming artifact because they allow a specific routine to be reused very easily. However, these seemingly harmless constructs can be detrimental to performance, because the Query Optimizer does not account for any T-SQL logic inside a UDF, and UDFs are executed for every row in the result set, just like a cursor. When using scalar UDFs, there are specific recommendations that apply to UDFs that access system or user data, and recommendations that apply to all UDFs.

An example of a scalar UDF that does not access data was referenced in Chapter 4, Exploring Query Execution Plans, under the Query plan properties of interest section, as shown in the following ...

Get Learn T-SQL Querying now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.