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

Is a Temporary Table Really Necessary? Expand / Collapse
Author
Message
Posted Friday, June 2, 2006 9:00 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 17, 2014 2:18 PM
Points: 1,035, Visits: 411

Good article, but I tend to disagree on the "roll your own" cursors.  I have seen several situations where using a cursor vastly outperformed the solution you seem to advocate.  This is particularly true where you are using explicit transactions for some reason.

Over the years I have come to like, and rely on, derived tables and subqueries.  These are generally my "go to" option.  Then if I need to I'll use table variables.  Temp tables are generally the last thing I look at using and usually it's in situations where putting an index on them really helps.  Finally, using an indexed view can sometimes be a good alternative to temp tables.




/*****************

If most people are not willing to see the difficulty, this is mainly because, consciously or unconsciously, they assume that it will be they who will settle these questions for the others, and because they are convinced of their own capacity to do this. -Friedrich August von Hayek



*****************/
Post #284579
Posted Friday, June 2, 2006 9:52 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 12:25 PM
Points: 30, Visits: 301

I've index table variables many times:

declare @table table (id int not null primary key . . .




Post #284597
Posted Friday, June 2, 2006 9:59 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 17, 2014 2:18 PM
Points: 1,035, Visits: 411

While you can set some basic constraints on a table variable you cannot delcare indexes per se, and from my experience the unique or PK constraints don't seem to help query performance the way that an index on a table would.

From BOL "Indexes cannot be created explicitly on table variables, and no statistics are kept on table variables."

Without statistics, the PK or unique constraints cannot be used by the optimizer to boost query performance.




/*****************

If most people are not willing to see the difficulty, this is mainly because, consciously or unconsciously, they assume that it will be they who will settle these questions for the others, and because they are convinced of their own capacity to do this. -Friedrich August von Hayek



*****************/
Post #284602
Posted Friday, June 2, 2006 10:05 AM


SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Monday, October 20, 2014 12:09 PM
Points: 91, Visits: 198

Hi Stephen,

The problem with creating and dropping a "normal" table in a stored procedure is that two users might try to run the same procedure at the same time and step all over each other, trying to access the "real" table.

A local temporary table (prefixed by # as opposed to ##), on the other hand, is local to a connection, so two users won't interfere with each other provided they are using 2 different connections to the database.

What happens with local temporary tables is that behind the scenes, a "real" table is created in tempdb with a lot of underscores and some hex digits added on to the the name you see. If my friend Sam and I both run a stored procedure that creates a temp table #Foo, what happens is that Sam creates a table in tempdb that will be called something like #Foo________________________________________________________________________________________________________________000000017D5D
while I will create, for example, one called #Foo________________________________________________________________________________________________________________000000029A4F

(if you scroll all the way to the right, you'll see that the last several digits in the name are different)

Regards,

SteveR




Post #284604
Posted Friday, June 2, 2006 10:24 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 14, 2012 7:48 AM
Points: 1, Visits: 7
All of the links at the end of the article are broken.....
Post #284610
Posted Friday, June 2, 2006 11:11 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Wednesday, February 5, 2014 8:30 AM
Points: 769, Visits: 191

I too like temporary tables, I don't use them to often but find them a good alternative to cursors.  Recently I have had reason to use a global temp table which is scary and I don't really like so if anyone has better suggestions let me know.

I have an sp which queries a linked server.  The linked server has about twenty databases which have the same table structure along with other misc databases.  I need to loop through every database and run a query against it, then return the compiled results.

My current solution involves using a comma delimited string as a sort of array holding the database names, and use a while loop to loop through all the databases.  I then use the exec command to create a query with the database variable and load the results into a global temp table.  Obviously I am very careful to drop the table at the end but still...

Using a global temp table was the only way I could think of to join and interact with the results of a query run with the exec command.  The exec command was the only method I could find that would allow me to write and reuse one query in a stored procedure.  Yes it does have to be in an sp.

Something I just thought of is calling another sp from the main sp passing the database variable from one to the other.  Then I could compile the results in the main sp.  If that works it should get rid of the global temp table but not the exec command, still it might be a step in the right direction.

Post #284621
Posted Friday, June 2, 2006 1:16 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, April 14, 2011 12:01 PM
Points: 6, Visits: 6
Very helpful - I learned some things I didn't know (and which I'd been doing wrong ) - but now I know how to fix them. I'd really like to know some of the pros and cons of using a Server 2000 table data type vs. a temp table.
Post #284641
Posted Friday, June 2, 2006 1:22 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, September 29, 2014 10:46 AM
Points: 2,734, Visits: 943

I found #temp tables a nice tool for split very complex queryes and avoid cursors. The unique incovenient is to be sure to drop de #temp (even at a transaction error).

SQL abuses from tempdb behind the curtains. If u put u tempdb in another disk u can improve the performance since u can join data from two sources (double IO!) instead of scanning two tables at the same disk.

At a very uncommon issue i needed get a query, make some processing and run the same query again. temp table saved me since i can run the query just one time.

At a performance analysis i found u can save time at a disk space cost if u properly use temp tables.

 

Jean

Post #284642
Posted Friday, June 2, 2006 10:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 8:26 PM
Points: 35,617, Visits: 32,213

http://support.microsoft.com/default.aspx?scid=kb;en-us;305977&Product=sql2k

You may never use a table variable outside a function ever again if you study Q3, Q4, and Q5 a bit...



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #284686
Posted Saturday, June 3, 2006 6:57 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: 2 days ago @ 6:49 AM
Points: 3, Visits: 15

Good stuff. Thanks for info - and the links others have supplied to asociated information is helpful too.

We have found #temp tables often significantly outperform derived tables in situations where there is a large amount of data. We often create "driver" tables of the primary keys for the data we want from several other tables. If this driver table is used in several queries, there is a marked perfromance improvement.

Key thing that most everyone here seems to agree on - for poor performing queries try different solutions. Don't fall in love with a single technique and expect it to work for everything.




Post #284709
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse