Temp table, table variable and table expression comparision...

Question: Could you please review this comparison table between temp tables, table variables, and table expressions? There are seven entries as listed. The items with ??? are missing where I need most help but it will nice to have your comment on others as well.Temps
a. Scope
a1. Temp tables are visible only to a session across all batches (if local) and visible across a session if global.
a2. Table variables are visible only to the batch that declared them and are destroyed at the end of the batch.
a3. N/A because table expressions do not have physical representation and their scope equates those of table called by such a table expression.

b. Can apply DDL after creation/declaration?
b1. Can apply a DDL to temp tables after it is created.
b2. ???
b3. n/a

c. Can have indexes?
c1. Can have indexes and other DDL changes.
c2. ???
c3. n/a

d. Affected by rollback/ logged?
d1. Yes.
d2. No.
d3. n/a

e. Has distribution statistics?
e1. Yes. Allows histogram.
e2. No.
e3. n/a

f. Has physical presence in temp db?
f1. Yes
f2. Yes
f3. No.

g. Suitable for what table size?
g1. Large data
g2. Small data
g3. ???

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
a1: Local temp tables are only visible in the session where created. Global temp tables are visible in all sessions.

b2, c2: It's a variable. DDL could not be applied.

g: You use what you gives the best performance.

The data size is only a criteria between temp table and table var, cause the optimizer uses an fixed value of 1 for the row estimate. This may lead to non optimal plans.
Whether you chose an temp table/variable or a CTE depends on whether a materialized intermediate result improves the query plan.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.