UDF is slow

Today I discovered that a query that uses an udf to calculate the age of an employee ( see code below) is much slower than when the same TSQL is called directly (without using an UDF). Probably because the query optimiser sees the UDF as a black box and generates a very stupid plan.

Slow Example  (  took more than one hour in our environment when we decided to kill it ). 

Fast example ( < 3 minutes). 


Leave a Reply

Your email address will not be published. Required fields are marked *