0

This article warns of the performance pitfalls of using parametrised queries rather than stored procedures. The basic idea is that the server has to guess the data type of your parameters and its guesses pollute the cache. My confusion comes from the article only mentioning stored procedures as a solution. As far as I know, user-defined table-valued function are just as strongly typed as stored procedures, so the server shouldn't have to guess when using them. However, I also know that ADO.NET only has a special ComamndType for stored procedures, so I don't know how it knows that it's talking to a user-defined table-valued function rather than passing around raw SQL.

My question is this: Given that I can't tell ADO.NET that what I'm telling it to call is a user-defined table-valued function rather than a stored procedure, am I at risk of it treating my call to a user-defined table-valued function as a parametrised query and therefore facing performance issues? Or is ADO.NET smart enough to know that user-defined table-valued functions have obvious parameters?

13
  • 2
    1) don't ignore that this article is from 2009. 2) whenever you're worried about performance: try it yourself - since performance is very dependent on your specific usecase. and noone external could give any qualified answer about your system. May 13 at 14:39
  • @FranzGleichmann Very true, but I imagine that how ADO.NET talks to table-valued functions will be well-known and independent of my system.
    – J. Mini
    May 13 at 14:47
  • yes. but what impact it has is very dependent on your system. and your source information could be very out of date and simply incorrect. so yeah; racing your horses is the best way to get reliable performance data. besides that, i guess we can only give opinions. May 13 at 14:52
  • 2
    @jdweng Why would an article that is uniquely about stored procedures have any relevance here?
    – J. Mini
    May 13 at 15:09
  • 1
    @jdweng Why would showing how to call something explain the difference between that and something else?
    – J. Mini
    May 13 at 19:05

1 Answer 1

2

No, they are not the same.

While both get their parameters coerced into the respective types, therefore no issues with sargeable queries, an ad-hoc batch against a TVF still gets a new query plan, so you have the overhead of a recompilation.

As far as I know, user-defined table-valued function are just as strongly typed as stored procedures, so the server shouldn't have to guess when using them.

No they are different things. Procedures have query plans already compiled, TVFs only get their plans compiled when they are used, so if you are using a new data type (or something else changed in the query) then you get a recompile.

You can see this by examining the query plans generated in this fiddle. Note how the query_hash values are the same in the procedure case, but not in the TVF cases.

5
  • I can't stress just how surprising I've found this answer. It's the only case I can think of where it's better to write a stored procedure than write the equivalent user-defined table-valued function. I feel like there's a major and terrifying gap in my education.
    – J. Mini
    May 13 at 23:31
  • They are two different things and hav different purposes. TVFs cannot contain multiple statements, or do anything other than a SELECT. It could be argued that TVFs are a better version of views, and perhaps you should create a procedure to select from the TVF, in other words use both at the same time. May 13 at 23:38
  • You're right. I'm just expressing my surprise that this is the only time that I've ever seen an argument for writing a procedure instead of a table-valued function (in a situation where either could do the job).
    – J. Mini
    May 13 at 23:41
  • "TVFs cannot contain multiple statements" - That's not entirely true. There are are two kinds of TVFs in SQL Server, inline and multi-statement. The former works the same as a view; the query in the function is inlined with the outer query by the optimizer. The latter works the same as a stored procedure, INSERTing the results into a table variable; the results are fully realized prior to being processed by the outer query, so if the outer query's predicate could reduce those results, the optmizer has no way of doing it. May 14 at 8:02
  • 1
    Sorry I took for granted we were talking iTVFs May 14 at 8:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.