Why are temporary tables faster than table variables for joins?

10,526

Solution 1

Both are persisted in Tempdb; however, performance problems come into play because the optimizer does not maintain statistics on table variables. This is problematic because the optimizer is ALWAYS going to assume there is 1 row in your table variable. Obviously this can really screw up a query plan, especially when there are lot of rows in your table variable. I wouldn't use a table variable to store anything more than a 1000 or so rows; otherwise, the performance could be unpredictable.

Solution 2

Temp tables are similar to tables but they are store in tempdb when created, which means that optimizer can create statistics on them,while table varaibles as similar to variables and there are no statistics on them.Usualy when comparing tmp tables vs table variables,the temp tables come out on top. The reason is that the query optimizer will sometimes generate poor plans for table vars. This can mostly be seen when there is a lot of data. The biggest point I can make is that table variables are more likely to cause unpredictable execution plans when compared to the plans generated for temp tables. Temp tables on the other hand, will cause more recompilation.

Share:
10,526

Related videos on Youtube

Guillermo Gutiérrez
Author by

Guillermo Gutiérrez

Currently Javascript Senior Developer. I have experience primarily with C# and SQL Server, which I used the most in last 2 jobs. Also I have been using Developer Express components for Windows Forms and ASP.NET MVC Framework. Aditionally I have knowledges in relational database design. Currently I am also learning Elixir, Erlang and Clojure (I love FP!) In web development (which I like the most and is what influenced me to choose this career :), I have experience with: HTML, CSS and Javascript. JQuery and some AngularJS. Bootstrap framework. Symfony PHP Framework 1.4 and 2, combined with Doctrine 1 and 2 ORM. Experience on ASP.NET MVC Framework, Web API, NHibernate, FluentNHibernate, FluentValidation and StructureMap. Basic knowledges of ServiceStack. Mobile applications with Phonegap/Cordova, using Intel's AppFramework, and now starting with Onsen UI. Scala, with Play Framework, Akka and Slick (still learning). Other programming languages I have used: C, Visual Basic 6 and .NET, DrScheme, Prolog.

Updated on June 05, 2022

Comments

  • Guillermo Gutiérrez
    Guillermo Gutiérrez about 2 years

    Why are temporary tables faster than table variables for joins in SQL Server?

    NOTE: In both scenarios the tables have PK, and the tables are joined with other "physical" tables through the PK.

  • Martin Smith
    Martin Smith almost 11 years
    There are no statistics maintained for table variables but row count is. The optimizer generally assumes the table variable cardinality is 0 rows because the statement is compiled before the table variable is populated. If the statement is subject to recompile it can use the actual row count.
  • Martin Smith
    Martin Smith almost 11 years
    Example of this in the cardinality section of my answer here