The differences and similarities between table variables and #temp
tables are looked at in depth in my answer here.
Regarding the two queries you have shown (unindexed table variable vs unindexed temp table) three possibilities spring to mind.
INSERT ... SELECT
to table variables is always serial. TheSELECT
can be parallelised for temp tables.- Temp tables can have column statistics histograms auto created for them.
- Usually the cardinality of table variables is assumed to be
0
(when they are compiled when the table is empty)
From the code you have shown (3) seems the most likely explanation.
This can be resolved by using OPTION (RECOMPILE)
to recompile the statement after the table variable has been populated.