Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Temp Tables in SQL Server


Temp Tables in SQL Server

Author
Message
David le Quesne
David le Quesne
SSC Eights!
SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)SSC Eights! (819 reputation)

Group: General Forum Members
Points: 819 Visits: 32

I use temporary tables to store large results sets for reporting, usually where the values required cannot be calculated in a single SELECT statement. The reporting runs in a third party application with a scripting language which allows me to create and access the temporary tables. Doing this gave my application a huge performance boost (table hints also helped), and helped solved locking problems I was having due to the applications insistance on starting a transaction before running any script code.

However, I have one process which uses a stored procedure for speed, and have used a shared 'permanent' table because temporary tables go out of scope when the stored procedure finishes, as described in this quote from MS Books Online:

"A local temporary table created in a stored procedure is dropped automatically when the stored procedure completes. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table cannot be referenced by the process which called the stored procedure that created the table"

I was wondering if a global temporary table would remain in scope after the procedure had completed, it is not clear from the explanation in Books Online.

Alternatively, I might be able to return a table datatype from the procedure, and create a local temporary table from that to drive the report.

Any suggestions welcome!



If it ain't broke, don't fix it...
Jaroslav Urban
Jaroslav Urban
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
Just quick notes about the index for tmp sys table.
it's useless to create index just after tmp table creation. I add index after I fill table with data. Then the data are reindexed and all queries are faster after that.

Based on my experience I can say that using of tmp table is slower compare to use share table. Share table is more complicated with management, but faster. In my case, which is about this range (0-4000 rows and 9 columns(mostly varchar or uniqueidetifier)) I also use sys tmp table for related table to my share table, but there are only three columns. And finaly I use also table datatypes, but there is only one column and max 100 records.

I just don't know how I could solve problem with updating of statistics above this share table due to public role of logged dbuser.
colin.Leversuch-Roberts
colin.Leversuch-Roberts
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2725 Visits: 715
This article should have been updated prior to re-publish as there are some errors and misconceptions contained within it. Sorry.

The GrumpyOldDBA
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
connel
connel
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 7

I agree with the last reply: This article should be rewritten taking into consideration the comments raised by those involved.

There is no explanantion to justify the statements regarding the limitations of temp tables.

We use a HUGE number of temp tables in our overnight batch jobs and we are constantly looking where temp tables can be replaced by shared permanent table objects. We also saw the benefit of creating indexes on the temp tables and moved the create index statements after the INSERT to improve performance. Does that apply to the CLUSTERED INDEX too? And should we update statistics following insert of a large number of rows?

Last question: Is it worth using TABLOCK on the temp table? It should be unique to that connection so in theory not required?

The article WITH the replies has been very useful. Thanks to all that contributed.

C




Best Regards,

C
David Burrows
David Burrows
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7963 Visits: 9411

Yes you can

Or I sometimes declare it with the create table, e.g.

CREATE TABLE #mytable (mykey int PRIMARY KEY CLUSTERED, mycol char(1))

to let SQL make up the name




Far away is close at hand in the images of elsewhere.

Anon.


grego
grego
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 4

Hi Dinesh, (Sorry if this comes through twice, I posted but it seems to have got somewhere? )

Nice simple article, well done. Just one thing which is a misconception with table variables. That is they are not stored in memory at all. Actually they are created in the tempdb jus the same way that temp tables. There are lots of articles around say they are stored in memory but it's just not true. A simple reason for this is explained by this example. Say you have a small SQL Server with 1GB of RAM. You create a table variable and insert 2GB of data into it. If table variable were stored in memory this would cause lots of issues. A simple way of see that table variables are treated as temp table is to do the following.

SET STATISTICS IO on
begin
declare @lognames table (a int not null identity (1, 1) primary key, b varchar(255) not null, c int not null, d varchar(100) )
insert into @lognames(b,c,d) values('1',2,'3')
select * from @lognames
end
SET STATISTICS IO off

You will see that you have a table #something. So table variables are not stored in memory they are a temp table . It doesn't state anywhere (I can find) in BOL that they are stored in memory.

Anyway well done

Kind regards
Greg O





David Burrows
David Burrows
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7963 Visits: 9411

quoteYou will see that you have a table #something. So table variables are not stored in memory they are a temp table . It doesn't state anywhere (I can find) in BOL that they are stored in memory.

This has been discussed in other threads and I believe there are articles on MS site that refer to this. When you create a table variable, SQL will create a #table in tempdb but may or may not use it. It all depends on how much data and whether SQL has space for it in memory. It could start of in memory but at any time be transferred to tempdb.




Far away is close at hand in the images of elsewhere.

Anon.


Judith Webber
Judith Webber
SSChasing Mays
SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)SSChasing Mays (645 reputation)

Group: General Forum Members
Points: 645 Visits: 182

David,

In refrence to your question about Global tables and their scope. A global table with be deleted when all connections refrencing it have been terminated. So if you create a global table via a stored procedure and someone accesses that table either by executing the same stored procedure or another they will be accessing the data you created. If your stored procedure were to complete the table would not be deleted until everyone accessing the data closed their connection.

I have yet to find a case where I wanted temporary data accessed by anything other than the process for which the data was created. If the data needs to be accessed by other processes I simply create a real table and if it is really just a work table that can be deleted I will prefix it with something like tmp or wrk.

Hope that made sense.....


colin.Leversuch-Roberts
colin.Leversuch-Roberts
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2725 Visits: 715

Both #temp tables and table variables may or may not be created in memory or on disk - it's a matter of available resource.

Your use of one type of table or another largely relates to your requirements - you should be aware of the limitations and advantages of each.



The GrumpyOldDBA
www.grumpyolddba.co.uk
http://sqlblogcasts.com/blogs/grumpyolddba/
jcraddock
jcraddock
SSC-Enthusiastic
SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)SSC-Enthusiastic (106 reputation)

Group: General Forum Members
Points: 106 Visits: 99

Table Variables are inferior to Temp Tables in performance except in cases where performance won't really be an issue (small tables) .

Table variables are great for small things, but if you get to large datasets a temp table will blow it away for performance. Why? I don't know, but having used them a lot, I know that's how it is.





Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search