In the query below is it better for performance to move the case statement to a UDF? If so why?
SELECT id,
name,
case
when v1 = 'Y' then 'something'
when v2 = 'K' then 'something else'
when v3 is null then 'dont know'
else 'default'
end
from table
No I would leave it just like it is. Using UDFs can (in general) have some wierd side effects and limitations and this query simply doesn’t justify using them. UDF performance should be no better than what you get from this direct query.
Here’s a relevant blog entry: http://www.bennadel.com/blog/964-SQL-User-Defined-Functions-Are-Slower-Than-Inline-Logic.htm