«
»

performance tuning, SQL Server

T-SQL temp table vs table variables vs Common Table Expressions (CTE)

05.17.10 | 5 Comments

Temp tables

Behave just like normal tables, but are created in the TempDB database. They persist until dropped, or until the connection that created them disappears. They are visible in the procedure that created them and any procedures that that proc calls.

Just like normal tables, they can have primary keys, constraints and indexes, and column statistics are kept for the table.

Temp tables, while they have space assigned to them in the tempDB database, will generally be accessed only from memory, unless the server is under memory pressure, or the amount of data in the table is large.

Table Variables

These tables behave very much like other variables in their scoping rules. They are created when they are declared and are dropped when they go out of scope. They cannot be explicitly dropped.

Like with temp tables, table variables reside in TempDB. they have entries in the system tables in tempDB, just like temp tables, and they follow the same behaviour regarding whether they are in memory or on disk.

Table variables can have a primary key, but indexes cannot be created on them, neither are statistics maintained on the columns. This makes table variables less optimal for large numbers of rows, as the optimiser has no way of knowing the number of rows in the table variable.

CTE

In my experience, CTEs are more like temporary views than anything else. When you look at the execution plan, you’ll see that they are inlined into the query, not materialised and stored. I find, with the exception of recursion, they’re more to make queries simpler to write than faster to run.

(Bron http://www.sqlservercentral.com/Forums/Topic415829-338-1.aspx)

meer nuttige artikels

http://www.sql-server-performance.com/articles/per/temp_tables_vs_variables_p1.aspx

http://sqllearnings.blogspot.com/2009/04/issues-with-abusing-table-variables.html

http://blogs.msdn.com/craigfr/archive/2007/10/18/ctes-common-table-expressions.aspx


5 Comments

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:

:


«
»

My Index

0-10

A

Adwords campagne beheer

B

bakgerei voor kindjes

C

CE HTML
Community Server

D

E

Elegant
Elegant opinie

F

Familie Anrijs
fotoshoot
professionele familiereportage
fotoshoot
familie fotoshoot
FrontierVille
FrontierVille guides review site

G

GarageTV
garagetv.be
Google Adwords - adverteren op zoekmachines
Google-approved Qualified Professional
Groene stroom

H

huwelijksreportage

I

Internet reclame

J

JWI Consulting

K

Kantoorinrichting
Kaartjes bestellen
kerstkaartjes bestellen
KMO website snelheidstest

L

Lamineerapparaat a3

M

N

O

Online kaartjes bestellen

P

P&O ferries
Philips Net TV
Playmobilkasteel
Portfolio Development (.NET C# PHP)
Portfolio SEO (Search Engine Optimalisatie)
Provinciaal Voetbal
provinciaal voetbal Antwerpen
provinciaal voetbal Branbant
provinciaal voetbal Limburg
provinciaal voetbal Oost-Vlaanderen
provinciaal voetbal West-Vlaanderen

Q

R

S

SEO (Search Engine Optimization)
Snelheids voor kmo websites
Suikerpasta

T

trouwfoto's
trouwfoto's

U

www.uwfotomoment.be

V

verjaardagsfeestjes
Verjaardagsfeestjes voor kindjes
Voetbal uitslagen
online voetbalschoenen kopen

W

google webmaster tools

X

Y

Z

Zoekmachine optimalisatie