Why are temporary tables faster than table variables for joins?

Guillermo Gutiérrez picture Guillermo Gutiérrez · Jul 12, 2013 · Viewed 10.4k times · Source

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.

Answer

Adam Haines picture Adam Haines · Jul 12, 2013

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.