Recently, I have been conducting many training sessions at a leading technology company in India. During the discussion of temp table and table variable, I quite commonly hear that Table Variables are stored in memory and Temp Tables are stored in TempDB. I would like to bust this misconception by suggesting following:
Temp Table and Table Variable — both are created in TempDB and not in memory.
Let us prove this concept by running the following T-SQL script.
/* Check the difference between Temp Table and Memory Tables */
-- Get Current Session ID
SELECT @@SPID AS Current_SessionID
-- Check the space usage in page files
SELECT user_objects_alloc_page_count
FROM sys.dm_db_session_space_usage
WHERE session_id = (SELECT @@SPID )
GO
-- Create Temp Table and insert three thousand rows
CREATE TABLE #TempTable (Col1 INT)
INSERT INTO #TempTable (Col1)
SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
GO
-- Check the space usage in page files
SELECT user_objects_alloc_page_count
FROM sys.dm_db_session_space_usage
WHERE session_id = (SELECT @@SPID )
GO
-- Create Table Variable and insert three thousand rows
DECLARE @temp TABLE(Col1 INT)
INSERT INTO @temp (Col1)
SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
GO
-- Check the space usage in page files
SELECT user_objects_alloc_page_count
FROM sys.dm_db_session_space_usage
WHERE session_id = (SELECT @@SPID )
GO
-- Clean up
DROP TABLE #TempTable
GO
Let us see the resultset. It is very clear that the size of the table variable and temp table is the same and created in TempDb.

Have you ever heard of this misconception? Do you know any other method to prove that both Temp Table and TableVariable are created in TempDB.
Update : Ken Simmons have written excellent another article proving without inserting data that temp table and table variable both are created in TempDB please read here.
Reference: Pinal Dave (http://blog.SQLAuthority.com)










Good Explanation
About Temp Table & Table Variable
really busted the misconception of many!!!! :)
Hi Pianl,
I want to know the temp table name max length in temp db.
What is the naming convention will be used to store the temp table name?
Please explain the procees which is happening inside the SQL Server while creating temp table?
Thanks in Advance.
Regards
Prasad
Thanks for the explanation; my question now is WHICH ONE IS BETTER?
Regards,
Terry Amusa
Hi,
That is true, but when we used Temp table in place of table variable, performance much faster.
We didn’t try to prove it. But there should be some difference in storage, that’s why we got better performance.
is it when releasing memory after used?
as usual Great Explanation..
Thanks,
Tejas
Hi Tejas,
1. Yes as far as performance goes table variable does not outperform temp tables necessarily. Here is a quote from sqlmag (SQL Server Magazine)
Table Variables vs. Temporary Tables
By: Brian Moran
Quote:
A senior member of the SQL Server development team told me that table variables use internal metadata in a way that prevents the engine from using a table variable within a parallel query. He also said that SQL Server maintains statistics for queries that use temporary tables but not for queries that use table variables. Without statistics, SQL Server might choose a poor processing plan for a query that contains a table variable. The development team member added that you should limit your use of SQL Server 2000 table variables to reasonably small queries and data sets and use temporary tables for larger data sets.
2. There is a situation using a table variable is preferred.
In a user defined function if you want to access a temp table it is not possible, where you can access a table variable. For example: the following will work: the second example will not work, there will be compilation erors:
CREATE FUNCTION dbo.example1
(
)
RETURNS INT
AS
BEGIN
DECLARE @t1 TABLE (i INT)
INSERT @t1 VALUES(1)
INSERT @t1 VALUES(2)
UPDATE @t1 SET i = i + 5
DELETE @t1 WHERE i < 7
DECLARE @max INT
SELECT @max = MAX(i) FROM @t1
RETURN @max
END
GO
Example 2:
CREATE FUNCTION dbo.example2
(
)
RETURNS INT
AS
BEGIN
CREATE TABLE #t1 (i INT)
INSERT #t1 VALUES(1)
INSERT #t1 VALUES(2)
UPDATE #t1 SET i = i + 5
DELETE #t1 WHERE i < 7
DECLARE @max INT
SELECT @max = MAX(i) FROM #t1
RETURN @max
END
GO
This Q/A on stackoverflow: http://stackoverflow.com/questions/27894/whats-the-difference-between-a-temp-table-and-table-variable-in-mssql leads to a few other articles (note the accepted answer), the MS KB article seems to be down right now, but the MSDN blog article above that says that “First, the table variable is NOT necessarily memory resident. Under memory pressure, the pages belonging to a table variable can be pushed out to tempdb.”, which sounds to me like a much smaller test would need to be performed to determine if a table variable, when it is afforded plenty of RAM, will stay in memory…
Hi Pinal,
Good clarification,(there are still articles out there which claim table variables are created in memory).
Thanks for the sql…
Ramdas
Thank you very much Ramdas for your very excellent note.
Hello All,
If you attempt this experiment with very simple and small number of rows, it will still give you same result – which means both of them are created in TempDB and no in memory.
Kind Regards,
Pinal
I did not understand how the page count 6 and 12 for temp table and table variable resp shows that both uses tempdb. Can anyone explain this? Thanks.
Please note that number 6 and 12 really does not matter.
What matters is from 0 to 6 and 6 to 12 so in fact I am talking about they have same difference.
Okay, got you. Many Thanks.
Hi Pinal,
Thanks for the explanation. Have couple of questions haunting me now:
1. If both reside in tempDB, how are table variables different from temp tables? What is the advantage of using table variables in place of temp tables?
2. Why is it adviced to go for temporary tables when there is huge amount of data being stored in table variables?
Request you to throw some light on this to make things more clear which would help us in better understanding on each of these at concept and usability level.
Thanks for all your knowledge sharing.
Regards,
Phani.
Hi Phani,
In the comments section i have attempted to throw some light on the questions you have, look at the reply for tejas shah in the comments section.
Thank you
Hello Phani,
In case of large amount of data temp table is advised because sql server creates, maintains and uses the statistics of temp table while generating the execution plan. Besides this we can create index in tenp tables if that is needed.
Regards,
Pinal Dave
I have tried the sequences and it worked ,but when i drop temp #TempTable the user_objects_alloc_page_count did not clear to 0 ,WHY?
Hello sir,
Great Topic to Discuss on blog.
good explanation with example and very useful too.
Ramdas has also give very good comment and very good explanation.
Used temp (#) in this procedure. Check it out…
alter procedure sp_Insert
(
@vStr varchar(max)
)
as
Begin
Declare @GetLine varchar(max), @GetCol varchar(max)
Declare @p int, @col1 varchar(30), @col2 varchar(30)
– Select @maxid = max(InvID) from tblMaster1
– Delete from tblDetail1 where lid = @maxid
create table #temp1 (col1 varchar(20), col2 varchar(20))
While (len(@vstr) !=0)
Begin
if (len(@vstr) !=0 and charindex(‘^^’,@vstr,0) = 0)
Begin
set @getline = @vstr
set @vstr = ”
End
else
Begin
Select @getline = substring(@vstr,0,charindex(‘^^’,@vstr,0))
End
set @p = 0
While (len(@getline)!=0)
Begin
if (len(@getline) !=0 and charindex(‘^’,@getline,0) = 0)
Begin
set @getcol=@getline
set @getline=”
End
Else
Begin
print @getline
Select @getCol = substring(@getline,0,charindex(‘^’,@getline,0))
print @getcol
End
set @P=@P+1
IF(@p=1)
begin
set @col1= @getcol
end
if (@p=2)
begin
set @col2= @getcol
end
Set @getline = substring(@getline , charindex(‘^’ ,@getline,0)+1, len(@getline))
print @getline
End
– Select @maxid= isnull(max(lid) from tbldet
insert into #temp1 values(@col1,@col2)
Set @vstr = substring(@vstr,charindex(‘^^’,@vstr,0)+2 , len(@vstr))
End
Select * from #temp1
End
[...] December 28, 2009 by pinaldave Few days ago I wrote an article on the myth of table variable stored in the memory—it was very well received by the community. Read complete article here: SQL SERVER – Difference TempTable and Table Variable – TempTable in Memory a Myth. [...]
On our test/development database server (2 sql server instances) with 4GB memory had a proc using table variabes and was running over 5 minutes. Changed to temp tables and is now running under 30 seconds. In production (single instance with 4GB) ran fine with table variables – around 30 seconds. Assuming memory constraints contributed to poor performance on test/development server, rather than stats.
Hello Jakey,
Table variables performs good only for small resordset. Because there data statistics is not used while creating the execution plan and because of that sometimes fully optimized plan are not created.
Table variable should be used where they are joined with small tables only.
There are many more details which are associated with table variables but this is just one line answer here.
Regards,
Pinal Dave
Hello, In terms of performance, is it better to create the temp table and use it or is it better to create the temp table on the fly?
Is this better to do as below:
create table #tv_target (c11 int, c22 char(100))
go
INSERT INTO #tv_target (c11, c22)
SELECT c1, c2 FROM tv_source
or is it better to do as below:
SELECT c1, c2 into #tv_target
FROM tv_source
Please see this query :-
SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)
gives error like
‘ROW_NUMBER’ is not a recognized function name.
when i run ito SQL 2005
Please can u tell me function for gettin rowno in
MS-SQL-2005 or 2008
@Nitin :
I think u r missing table name in the last..
It shud be:
SELECT TOP 3000 ROW_NUMBER() OVER (ORDER BY name) from TEST
Thanks a lot…
it was my mistake…
I want to do replication of two tables having two diff names and (same structure but diff records )
in single table at subscriber side.
Is it possible in SQL server 2008 or 2005.
Please give me solution.
The following stmt gives me the temp tables I created:
SELECT TABLE_NAME
FROM tempdb.INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE ‘%#%’;
But it does not show me the table variables created in tempdb.
How can I get the list of table variables created in tempdb?
My ten pence.
I have a rule of thumb that you should use what ever tool is best for the job.
In this case temp tables or table variables.
When having to run a ‘while’ loop or having to rerun the query many times, OR if I need to create a rank in a set of data the Table variable is best as you can program your SQL to run like you would run a data set in code, you have no need to have to keep dropping the table (which is always annoying), and you can use a quick ID identity field in the table variable create script that can give you a handy rank, rownumber or sequence number.
Use Temp tables for all else and don’t forget the temp table has the global option which is very useful too when having two connections open and running multiple tests at the same time.
When putting together the design in the first place you may find that creating a non temporary static table is always the best option, maybe developers get too hung up on leaving the workings out in the database, when in fact it makes debugging and picking up other peoples development far easier and you can index in the normal fashion improving performance.
Regards
Darren Harvey
BI Dude MCTS, MCITP, MCDBA
Hello,
In my case when I use table variable my query time counts 7 sec and for temp table it is near about 30 sec.
my main table rows are 3 million. After reading all these comments i am confuesd what should i use ?
according to this blog table variable consumes double size then temp table.
Plz make me happy with some usefull advice.
Thanks & Regards
Pramod sharma
Pinal dave, I have used Global temp table and temp table.
Temp table has dropped once the query get closed,
Global temp table has dropped once the connection where it created get closed but in your definition you have mentioned that the global temp table definition will remain permanently, how ? kindly give me explaination..
If memory serves me correctly, it’s not about in-memory or not; it’s all about scope of the connection. #TableName will get you in trouble with web services that use one connection. However, if you use table variables, the temp table is contained within the scope of the stored procedure.
First, the table variable is NOT necessarily memory resident. Under memory pressure, the pages belonging to a table variable can be pushed out to tempdb. Here is one example shows space taken by table variable in tempdb
use tempdb
go
drop table #tv_source
go
create table #tv_source(c1 int, c2 char(8000))
go
declare @i int
select @i = 0
while (@i 100 and name like ‘c%’
This will return two rows containing columns c111 and c222. Now this means that if you were encountering DDL contention, you cannot address it by changing a #table to table variable.
· Third, transactional and locking semantics. Table variables don’t participate in transactions or locking. Here is one example
– create a source table
create table tv_source(c1 int, c2 char(100))
go
declare @i int
select @i = 0
while (@i < 100)
begin
insert into tv_source values (@i, replicate ('a', 100))
select @i = @i + 1
end
– using #table
create table #tv_target (c11 int, c22 char(100))
go
BEGIN TRAN
INSERT INTO #tv_target (c11, c22)
SELECT c1, c2
FROM tv_source
– using table variable
DECLARE @tv_target TABLE (c11 int, c22 char(100))
BEGIN TRAN
INSERT INTO @tv_target (c11, c22)
SELECT c1, c2
FROM tv_source
– Now if I look at the locks, you will see that only
– #table takes locks. Here is the query that used
– to check the locks
select
t1.request_session_id as spid,
t1.resource_type as type,
t1.resource_database_id as dbid,
(case resource_type
WHEN 'OBJECT' then object_name(t1.resource_associated_entity_id)
WHEN 'DATABASE' then ' '
ELSE (select object_name(object_id)
from sys.partitions
where hobt_id=resource_associated_entity_id)
END) as objname,
t1.resource_description as description,
t1.request_mode as mode,
t1.request_status as status,
t2.blocking_session_id
from sys.dm_tran_locks as t1 left outer join sys.dm_os_waiting_tasks as t2
ON t1.lock_owner_address = t2.resource_address
Another interesting aspect is that if I rollback the transaction involving the table variable earlier, the data in the table variable is not rolled back.
Rollback
– this query will return 100 for table variable but 0 for #table.
SELECT COUNT(*) FROM @tv_target
· Fourth, the operations done on table variable are not logged. Here is the example I tried
– create a table variable, insert bunch of rows and update
DECLARE @tv_target TABLE (c11 int, c22 char(100))
INSERT INTO @tv_target (c11, c22)
SELECT c1, c2
FROM tv_source
– update all the rows
update @tv_target set c22 = replicate ('b', 100)
– look at the top 10 log records. I get no records for this case
select top 10 operation,context, [log record fixed length], [log record length], AllocUnitId, AllocUnitName
from fn_dblog(null, null)
where AllocUnitName like '%tv_target%'
order by [Log Record Length] Desc
– create a local temptable
drop table #tv_target
go
create table #tv_target (c11 int, c22 char(100))
go
INSERT INTO #tv_target (c11, c22)
SELECT c1, c2
FROM tv_source
– update all the rows
update #tv_target set c22 = replicate ('b', 100)
– look at the log records. Here I get 100 log records for update
select operation,context, [log record fixed length], [log record length], AllocUnitName
from fn_dblog(null, null)
where AllocUnitName like '%tv_target%'
order by [Log Record Length] Desc
· Fifth, no DDL is allowed on table variables. So if you have a large rowset which needs to be queried often, you may want to use #table when possible so that you can create appropriate indexes. You can get around this by creating unique constraints when declaring table variable.
· Finally, no statistics is maintained on table variable which means that any changes in data impacting table variable will not cause recompilation of queries accessing table variable.
Hello,
please,How can I programaticaly get the structure of Table variable ?.
i can get the name from sys.type but not the metada.
need help.
[...] ISBN: 1466405643 Page#75-76 Difference Temp Table and Table Variable – Effect of Transaction Difference TempTable and Table Variable – TempTable in Memory a Myth ObjectID in Negative – Local TempTable has Negative [...]