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


Is a Temporary Table Really Necessary?


Is a Temporary Table Really Necessary?

Author
Message
DCPeterson
DCPeterson
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1171 Visits: 432

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



*****************/
WHug
WHug
Valued Member
Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)Valued Member (67 reputation)

Group: General Forum Members
Points: 67 Visits: 399

I've index table variables many times:

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





DCPeterson
DCPeterson
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1171 Visits: 432

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



*****************/
Steve Rosenbach
Steve Rosenbach
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 206

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





Bob Clarke-304778
Bob Clarke-304778
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
All of the links at the end of the article are broken.....
jereme.guenther@gmail.com
jereme.guenther@gmail.com
Right there with Babe
Right there with Babe (791 reputation)Right there with Babe (791 reputation)Right there with Babe (791 reputation)Right there with Babe (791 reputation)Right there with Babe (791 reputation)Right there with Babe (791 reputation)Right there with Babe (791 reputation)Right there with Babe (791 reputation)

Group: General Forum Members
Points: 791 Visits: 194

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.


Roger Thomas
Roger Thomas
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 8
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.
jcb
jcb
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2764 Visits: 969

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


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)SSC Guru (54K reputation)

Group: General Forum Members
Points: 54726 Visits: 40395

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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Skip Hansen
Skip Hansen
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 18

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.





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