Here are my notes from this session of SQL Saturday held Saturday in Vancouver, BC
Perform an estimated execution plan to get the query plan for a scalar function
Track statement completion events in profiler to see what a function does. It will produce a lot of noise but you can dig through it and figure it out
A TVF query plan estimate is based on the TVF only returning 1 row. This can lead to problems with the optimization of the query plan.
Solutions to function problems
- Don't use functions
- In profiler add an object type filter for 18004, 20038, and 21321 to see what is happening and tune
- Use query hints MERGE and HASH to get better query plans
Join order with index hints affects performance
Use a temp table to populate from a TVF and then join to the temp table.
Use an inline TVF since it will be optimized along with the outer query
sp_refreshsplmodule fixes metadata associated with select * and compilation.
Using a TVF in a subquery, cross apply, or outer apply gives better performance than a scalar function.
SQLCLR functions can be fast for simple things. Be wary of using lots of memory.
Data access in SQLCLR can potentially be bad.
Each time you reference a function it wll be evaluated so a join back to a CTE with a function can be bad.