Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Table variable performance vs. temp table

 
   Database Help (Home) -> Programming RSS
Next:  Optimizing Stored procedures  
Author Message
brandon.richter

External


Since: Nov 07, 2006
Posts: 1



(Msg. 1) Posted: Thu Dec 14, 2006 2:36 pm
Post subject: Table variable performance vs. temp table
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

I've got an identical query, one with temp tables and one with table
variables that results in two completely different execution times.
The plans look virtually the same but execution time is around 2
minutes with the variables and less than a second with the temp table.
The plan shows that most of the time with the variable scenario is
inserting into @c. Any ideas what could cause this?

Table Variable:

declare @c table (
kerberos nvarchar(20),
inv nvarchar(20)
)

insert into @c (kerberos)
select distinct kerberos from prod_users where isdisabled <> 'yes'

update @c set inv = 'yes'
where kerberos in (select kerberos from prod_radius_membership)

select kerberos, inv
from @c


Temp Table:

create table ##c (
kerberos nvarchar(20),
inv nvarchar(20)
)

insert into ##c (kerberos)
select distinct kerberos from prod_users where isdisabled <> 'yes'

update ##c set inv = 'yes'
where kerberos in (select kerberos from prod_radius_membership)

select kerberos, inv
from ##c

drop table ##c

 >> Stay informed about: Table variable performance vs. temp table 
Back to top
Login to vote
Tom Cooper

External


Since: Jan 10, 2008
Posts: 463



(Msg. 2) Posted: Thu Dec 14, 2006 6:25 pm
Post subject: Re: Table variable performance vs. temp table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

There can be a number of reasons why a temp table can be faster than a table
variable and a number of reasons why temp tables can be slower then table
variables. If you have a case where the temp table is significantly faster
than the table variable, three common reasons are 1) SQL won't create
statistics on table variables, 2) SQL won't use parallelism on inserts to
table variables and 3) the indexes that can be created on a table variable
are limited. Since you aren't using indexes and you think the time is being
spent in the insert, my guess would be reason number 2.

You might want to look at
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-ta...-variab
for more info.

But it is often difficult to tell why the difference exists (at least for
me). I usually don't worry too much about why when I find a difference. I
just pick either a table variable or a temp table when designing the code.
Which one I pick depends on several things. The more data that goes into
the table and the more complex the usage of the table, the more likely I am
to pick a temporary table. Then if the performance when testing the choice
I made with real world sized data is good, great. If not, it's easy to
change the code to try the other way.

I realize that the code you have given may be just a sample demonstrating
the problem. But if it is your real code, I would have two comments.
First, if all you do with a temporary table is create it, load and process
data, and then drop the table and the table is never used by some other
connection, a local temporary table (#c) would be sufficient, you don't need
and therefore shouldn't use a global temporary table (##c). Also, if this
is all you are doing, loading this data into the table and then selecting it
and then dropping the table, it may be faster yet to just do this in one
select, something like

select distinct u.kerberos,
case when m.kerberos is not null then 'yes'
else null end as inv
from prod_users u
left outer join prod_radius_membership m on u.kerberos = m.kerberos
where u.isdisabled <> 'yes'

Tom

wrote in message

> Hi,
>
> I've got an identical query, one with temp tables and one with table
> variables that results in two completely different execution times.
> The plans look virtually the same but execution time is around 2
> minutes with the variables and less than a second with the temp table.
> The plan shows that most of the time with the variable scenario is
> inserting into @c. Any ideas what could cause this?
>
> Table Variable:
>
> declare @c table (
> kerberos nvarchar(20),
> inv nvarchar(20)
> )
>
> insert into @c (kerberos)
> select distinct kerberos from prod_users where isdisabled <> 'yes'
>
> update @c set inv = 'yes'
> where kerberos in (select kerberos from prod_radius_membership)
>
> select kerberos, inv
> from @c
>
>
> Temp Table:
>
> create table ##c (
> kerberos nvarchar(20),
> inv nvarchar(20)
> )
>
> insert into ##c (kerberos)
> select distinct kerberos from prod_users where isdisabled <> 'yes'
>
> update ##c set inv = 'yes'
> where kerberos in (select kerberos from prod_radius_membership)
>
> select kerberos, inv
> from ##c
>
> drop table ##c
>

 >> Stay informed about: Table variable performance vs. temp table 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2061



(Msg. 3) Posted: Thu Dec 14, 2006 11:35 pm
Post subject: Re: Table variable performance vs. temp table [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

( ) writes:
> I've got an identical query, one with temp tables and one with table
> variables that results in two completely different execution times.
> The plans look virtually the same but execution time is around 2
> minutes with the variables and less than a second with the temp table.
> The plan shows that most of the time with the variable scenario is
> inserting into @c. Any ideas what could cause this?

If you are running the table-variable batch first, it may be as simple
that that batch will have to carry the burden to read all data from
the disk. Then the batch with the temp table has it all server on a
silver plate with the data in cache. This would align with the fact
that the plans are the same.

Then again if "virtually" means almost, maybe there is a small difference
after all? There is one catch with table variables: when you insert into
them, you can never get parallelism. Could that be what you are seeing.



--
Erland Sommarskog, SQL Server MVP, esquel.DeleteThis@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
 >> Stay informed about: Table variable performance vs. temp table 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
inserting efficiently into a temp table/table variable - I am a newbie to Sql Server programming, and I was hoping that someone might be able to point me in the proper direction. I am trying to write a C function that takes an array of strings and insert them into either a temporary table or a table variable....

Table variable performance degradation in stored procedures - We have run into an issue that's becoming more pervasive, and I wonder if anyone else is running into it. We have processes that are ideal candidates for using table variables - high usage procs with low row count (<100) in the work tables. If the...

table variable - get the structure from an existing table - Hi all, Is it possible to declare a table variable and get the structure from an existing table? Something like "select * into @my_table" or any other recommended way Thanks, Avi

Temp Table - I have created a temp table using the following query: CREATE TABLE #temptbl1 (Col1 varchar(50), Col2 int) When I run the following query: USE tempdb IF EXISTS (SELECT * FROM sysobjects WHERE NAME = '#temptbl1') PRINT 'TABLE EXISTS' ELSE PRINT 'TABL...

Temp Table's - Hello All, I use several local temp tables in an application. These tables store information that is built from some text files by the user. This data cannot be mixed up by another user logging in at the same time. Is there a chance that another user can...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]