Table Valued Functions

January 7, 2009 at 7:43 am (TSQL) (, , )

I’ve been blogging for a whole nine months now. I decided to look back and see what’s bringing people to the site. The number one search phrase is “sql server 2005 service pack 3” but the overwhelming topic that most people are using to get to the site are user defined functions, specifically multi-statement table valued user defined functions.

It’s completely understandable. Ever since I first saw these things in use back in SQL Server 2000, I thought they were slick. Unfortunately appearances can be deceiving. The reason so many people are searching out information on these things is because they just don’t work very well. SQL Server can’t create statistics on the tables generated through the multi-statement UDF. Because it has no statistics to work with, the query optimizer assumes that each of these tables has a single row. Creating execution plans for only a single row of data works well when there is only a single row, or just a few rows, of data. But when you have hundreds, thousands, or more, it breaks down very quickly.

Because of this rather severe short coming, I recommend avoiding the use of multi-statement table valued functions. There are any number of tests (look through Gail’s blog, just for starters) proving this for those who are skeptical and think that, if they just adjust the right little bit of the query, it’ll move millions of rows through 8 layers of functions lickety split. It won’t. It can’t. Stop trying.

The single statement functions, parameterized views as they’re sometimes referred to, work very well, so feel free to use them if they’ll help your code. They resolve out, like views, into actual execution plans based on the statistics within the database.

Permalink Leave a Comment

More on Table Valued Functions

August 15, 2008 at 7:29 am (TSQL) (, , , )

From the hits in the search results, this is a popular topic. That being so, I’d like to redirect you to another blog that has some more detailed tests available. These tests show more of the shortcomings of multi-statement table valued functions. Gail Shaw is an MVP and a regular at SQL Server Central. She’s worth tracking and this post shows why.

I want to be clear. I’m not suggesting that you never, ever, use multi-statement table valued functions. There may be places where their use is helpful. I’m saying that using them comes with a very heavy cost, so you better be sure that they are in fact needed in the situation, whatever it is.

There was a long discussion and debate over at SQL Server Central recently on “profane” methods in TSQL. These were defined as the methods that any reasonably experienced DBA or database developer will generally proscribe against, such as TSQL cursors, table valued user defined functions (of either variety), CLR, and a few others. The confusion seemed to arise between the statement “there are usually better ways to access the data than to use X” and the statement “THOU SHALT NEVER USE X.” Apparently saying the first somehow causes people to hear the second. I don’t know why and I kind of don’t care. I think there are perfectly valid uses for cursors and WHILE loops and CLR and triggers and UDF’s… But I also know these things have a cost and if you simply don’t use them, there may be the occasional place where you might have recognized a savings, but overall your database will be better off.

Permalink Leave a Comment