I know, this was discussed several times (most discussions are some years back...).
But - looking through these discussions - I get the feeling, that there is a broadly spread common sense which is simply wrong: TV-UDFs are bad in performance.
Many people prefer SPs to collect data, why?
When I say TV-UDF, I'm speaking about the single-statement-UDF ("Inline-UDF") only. The optimizer will handle this right as clever, as if this part was written directly into the query. Multi-statement-UDFs perform much worse...
From my point of view, why you ALMOST EVER should use an UDF to collect data, I see these points:
- Best performance (I did a lot of comparisons!)
- You can easily use them with JOIN or APPLY and reach very complex but still readable queries (I use one "univers" query consisting of more than 30 functions coming up with more than 1000 columns - in seconds!)
- You can call them from everywhere (e.g. to fill an EXCEL-sheet with one call via ODC)
- You can mix them with XML-calls perfectly
- If your select needs only a sub-set of the columns the optimizer will skip unneeded parts - not so with SP
- With inline-UDFs the optimizer can predict the row count and is able to use all its capabilities of indexes, statistics... not so with SP
- With inline-UDFs there's no need to write the whole table definition for an insert when you want to use the data in the latter
- You could "wrap" an UDF with
Select count(*) from(select * from dbo.MyFunc())
to predict the row count. The optimizer will execute only the parts needed to get the count... - And - last but not least - as the UDF never writes anything, it is much lighter in point of locking
I use multi-statement-UDFs when the result is one distinct row in any case and SPs when - which is VERY rare - a cursor or dynamic sql is needed.
So: Why are so many people using SPs to collect data? Why do so many people think, that UDFs are bad? Is there any good reason or is this just old common sense?
Thank you for your inputs!