• For each temporary tables currently allocated, the following script will return the table name, index type(s) (heap, clustered, non-clustered), reserved mb, and row counts. Using this, confirm the allocation of temp table for both code samples are equivalent.

    Also take a look at the execution plan to confirm it looks equivalent for both.

    use tempdb;

    select o.type_desc

    , substring(o.name,1,charindex('__',o.name)+1)+right(o.name,12) table_shortname

    , si.name index_name

    , case si.index_id when 0 then 'HEAP' when 1 then 'CLUSTERED' else 'NONCLUSTERED' end index_type

    , row_count

    , ((reserved_page_count * 8024) / 1024 / 1024)reserved_mb

    , create_date

    from sys.dm_db_partition_stats ps

    left join sys.objects o on ps.object_id = o.object_id

    left join sys.indexes si on si.object_id = o.object_id and si.index_id = ps.index_id

    where is_ms_shipped = 0

    order by reserved_page_count desc;

    One more thing to do when comparing runtime of two variations of scripts, you need to flush the page buffer cache between each execution to insure they both start on equal footing.

    DBCC DROPCLEANBUFFERS;

    DBCC FREEPROCCACHE;

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho