Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Temp Tables in SQL Server Expand / Collapse
Author
Message
Posted Friday, February 11, 2005 1:33 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Tuesday, August 10, 2010 4:54 AM
Points: 815, 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...
Post #161013
Posted Friday, February 11, 2005 2:59 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 24, 2007 11:48 PM
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.
Post #161037
Posted Friday, July 14, 2006 2:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, June 9, 2014 6:02 AM
Points: 2,674, Visits: 697
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/
Post #294334
Posted Friday, July 14, 2006 2:42 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 14, 2009 4:01 AM
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
Post #294345
Posted Friday, July 14, 2006 2:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:16 AM
Points: 7,095, Visits: 6,914

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.

Post #294347
Posted Friday, July 14, 2006 4:29 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, September 7, 2008 10:54 PM
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




Post #294381
Posted Friday, July 14, 2006 5:03 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 8:16 AM
Points: 7,095, Visits: 6,914

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.

Post #294389
Posted Friday, July 14, 2006 5:51 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, August 5, 2014 8:22 AM
Points: 645, Visits: 180

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.....

Post #294395
Posted Friday, July 14, 2006 7:19 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, June 9, 2014 6:02 AM
Points: 2,674, Visits: 697

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/
Post #294435
Posted Friday, July 14, 2006 8:20 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 6, 2014 10:36 AM
Points: 104, Visits: 98

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.

 




Post #294476
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse