SQL Server for Developers

SQH14 T-SQL User-Defined Functions, or: Bad Performance Made Easy

12/08/2016

3:00pm - 4:15pm

Level: Intermediate

Hugo Kornelis

MVP

Database Consultant

PerFact B.V.

User-defined functions in SQL Server are very much like custom methods and properties in .NET languages. At first sight, they seem to be the perfect tool to introduce code encapsulation and reuse in T-SQL. So why is this feature mostly avoided by all T-SQL gurus? The reason is performance. In this session, you will learn how user-defined functions feed the optimizer with misleading and insufficient information, how the optimizer fails to use even what little information it has, and how this can lead to shocking query performance. You will also see that there is a way to avoid the problems. With just a little extra effort, you can reap the benefits of code encapsulation and reuse, and still get good performance.

You will learn:

  • How T-SQL user-defined functions can hurt query performance
  • Which T-SQL performance metrics are and which are not trustworthy when user-defined functions are involved
  • How to use inline table-valued functions to avoid the performance hit of other types of user-defined functions