SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Simplify Large Queries with Temporary Tables, Table Variables and CTEs


Simplify Large Queries with Temporary Tables, Table Variables and CTEs

Author
Message
amir.mochtar
amir.mochtar
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 49
i created #tmp table when processing large of rows then i made index for some columns, those are often used in inquiry. Did i do right?
Stan Kulp-439977
Stan Kulp-439977
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1358 Visits: 1131
Indexes will definitely help.

The only way to know for sure is try it both ways.
the sqlist
the sqlist
SSC Veteran
SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)

Group: General Forum Members
Points: 282 Visits: 724
amir.mochtar (8/10/2011)
i created #tmp table when processing large of rows then i made index for some columns, those are often used in inquiry. Did i do right?


The only problem with the indexes on temp tables in stored procedures or scripts is that they are not given by the SQL engine unique names as in the case of temp tables themselves. So if one connection invokes a procedure or script that creates a temp table and then its indexes, a second connection that will try to do this at the same time will fail because the indexes were already created by the first one. Remember, objects in SQL server has to have unique names.

Don't just give the hungry man a fish, teach him how to catch it as well.

the sqlist
the sqlist
the sqlist
SSC Veteran
SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)SSC Veteran (282 reputation)

Group: General Forum Members
Points: 282 Visits: 724
the sqlist (8/11/2011)
amir.mochtar (8/10/2011)
i created #tmp table when processing large of rows then i made index for some columns, those are often used in inquiry. Did i do right?


The only problem with the indexes on temp tables in stored procedures or scripts is that they are not given by the SQL engine unique names as in the case of temp tables themselves. So if one connection invokes a procedure or script that creates a temp table and then its indexes, a second connection that will try to do this at the same time will fail because the indexes were already created by the first one. Remember, objects in SQL server has to have unique names.


I will have to retract that. You actually can create indexes with the same name but on distinct tables.

No issues with temp tables indexes.

Don't just give the hungry man a fish, teach him how to catch it as well.

the sqlist
GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114048 Visits: 45466
the sqlist (8/11/2011)
I will have to retract that. You actually can create indexes with the same name but on distinct tables.

No issues with temp tables indexes.


Index names have to be unique on a table. Constraint names (incl primary key and unique constraints) have to be unique in a database.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


amir.mochtar
amir.mochtar
SSC Rookie
SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)SSC Rookie (47 reputation)

Group: General Forum Members
Points: 47 Visits: 49
the sqlist (8/11/2011)
the sqlist (8/11/2011)
amir.mochtar (8/10/2011)
i created #tmp table when processing large of rows then i made index for some columns, those are often used in inquiry. Did i do right?


The only problem with the indexes on temp tables in stored procedures or scripts is that they are not given by the SQL engine unique names as in the case of temp tables themselves. So if one connection invokes a procedure or script that creates a temp table and then its indexes, a second connection that will try to do this at the same time will fail because the indexes were already created by the first one. Remember, objects in SQL server has to have unique names.


I will have to retract that. You actually can create indexes with the same name but on distinct tables.

No issues with temp tables indexes.


yeah, thanks.

i got error that you mention earlier, i didn't realize the error will show in second connection, i put in foreachLoop component. i was planning to create hole process in ssis package that will be run in sql agent.

by the way, do you know where should i implement begin trans, commit, and rollback in package data flow? if in store procedure, i could easily implement those transaction keyword.
mstjean
mstjean
Mr or Mrs. 500
Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)

Group: General Forum Members
Points: 597 Visits: 2550
EdSwiedler (8/9/2011)
One thing that I have found that can cause a temporary table or table variable to perform better than a CTE is that you can create indexes on them. This will help when joining them to other tables in later processing. No, I have not actually captured statistics on this, but have noticed a perceived performance boost. As with everything SQL Server, this will all be impacted by other tasks running at the same time.

I will say that I have found that a series of CTE's, each building on the last, does better than the massive join's that I have encountered.


As far as I know you can only create a single column, automatically-named primary key on a table variable (just learned this pretty recently).


Cursors are useful if you don't know SQL
GilaMonster
GilaMonster
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114048 Visits: 45466
mstjean (8/11/2011)
EdSwiedler (8/9/2011)
One thing that I have found that can cause a temporary table or table variable to perform better than a CTE is that you can create indexes on them. This will help when joining them to other tables in later processing. No, I have not actually captured statistics on this, but have noticed a perceived performance boost. As with everything SQL Server, this will all be impacted by other tasks running at the same time.

I will say that I have found that a series of CTE's, each building on the last, does better than the massive join's that I have encountered.


As far as I know you can only create a single column, automatically-named primary key on a table variable (just learned this pretty recently).


Automatically named, yes, one column, no.

DECLARE @Test TABLE (
Col1 INT,
col2 INT,
col3 INT,
col4 INT,
col5 INT,
PRIMARY KEY (col1, col2, col4)
)



You can create multiple unique constraints in the same way.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


mstjean
mstjean
Mr or Mrs. 500
Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)Mr or Mrs. 500 (597 reputation)

Group: General Forum Members
Points: 597 Visits: 2550
GilaMonster (8/12/2011)
mstjean (8/11/2011)
EdSwiedler (8/9/2011)
One thing...(snipped).

As far as I know...(snipped).

Automatically named, yes, one column, no...(snipped)...You can create multiple unique constraints in the same way.


Awesome! Now that is gonna come in handy!


Cursors are useful if you don't know SQL
Federico Iori
Federico Iori
SSC-Enthusiastic
SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)SSC-Enthusiastic (135 reputation)

Group: General Forum Members
Points: 135 Visits: 300
Josh Ashwood (8/9/2011)
Nice article, and definitely on topic for real database developers...

However the more relevant question we are often faced with is what performs better - table variables, temp tables, or CTE's or leave the damn thing in one huge query ?

An 'It depends' answer would be ok, if expanded on!


Answer : CTE and leave the damn thing in one huge query should be the same because they do the same
Table variables have the advantage that are defined witih the session
Best performance is with temp tables, which are regular tables stored into the db tempd
I want to underline that on temp tables is possible to create indexes and statistics , and this can really improve the performance
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