Using Temp Tables Vs. Table Valued Functions

  • Can anyone offer an educated opinion and/or actual experience on the behavior of these two methods, especially concerning speed?  Do you believe a Table Valued function will outperform a temporary table used for the same basic purpose, or would there be any difference in performance? If so, would SQL Server 2005 perform any differently than SQL Server 2000 using the same senario?

     

     

  • >>Do you believe a Table Valued function will outperform a temporary table used for the same basic purpose,

    Unfortunately the answer is "it depends".

    Depends on table size and how it is being used. eg If you have a large temp table and are joining it to other tables, you may find that an index on the temp table really improves the performance. This is a scenario where the temp table would perform better because you can't index a table variable but you can index a temp table.

     

  • Thanks PW, good info!

  • More good info:

    http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=157332&p=2

    There is no "i" in team, but idiot has two.
  • Thank you Dave, that helpful link answered the rest of my questions.  I browse this forum all the time but that was one I happened to miss.

  • Contrary to a statement above, you can declare a PK or Unique constraint - which creates an index - on a table variable.

    Table vars can be as good or slower than temp tables.

    IMHO: Table valued functions: if you're only going to call it once, and it returns a relatively small set, they're OK. If you're going to use the results more than once, dump them into a @table of #table.

    P

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply