I have a multi statement table value UDF. That builds a temp table then returns it as the result.
The first time its executed it takes 5 seconds, subsequent executions with the same parameters take 1 second. If I change the parameters it then takes 5 seconds the first time then it goes back to 1 second. If I then change the parameters back to the first ones it still takes one second.
I cant work out what's causing it to take longer the first time a new parameter is used. Do UDF's compile a separate query plan each time different parameters are used? Can I monitor this in Profiler? I can see the sp_Recompile event but I'm not sure what event is fired when a UDF compiles.
Unfortunately I cant post the code but am hoping there is enough information for someone to give me some sort of explanation.
DBCC DROPCLEANBUFFERS
(on a development instance). Back to 5 seconds again?