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

Table variable

 
   Database Help (Home) -> Programming RSS
Next:  SQL Server 2008 SP 1 Install issue on Windows 7 6..  
Author Message
Abba

External


Since: Nov 20, 2009
Posts: 3



(Msg. 1) Posted: Wed Jan 06, 2010 11:25 pm
Post subject: Table variable
Archived from groups: microsoft>public>sqlserver>programming, others (more info?)

[SQL2005 latest sp]

If you have the belief that CTEs are always faster than Table variables, its
wrong.

Below are two queries, the first using a table variable and the second uses
a CTE. The first runs in a flash while the second one takes forever. The
execution plans suggest, it has to do with the CTE using nested loops
because it evaluates everything first which is very costly in this scenario.

However, this is not always the case. There are queries when CTEs are
actually faster. The bottomline is to test a query which uses a CTE or a
table variable and check the execution plans rather than to blindly believe
that CTEs are faster.

declare @t table (MemNo varchar(100))
insert into @t
select Common.SqlClr.fnEncryptPICString(MemNo) as MemNo from TrueMem
(nolock) where can = 6
select Common.SqlClr.fnDecryptPICString(TrueMemID)
from @t t
join Mem m (nolock)
on t.MemNo = m.MemNo


;with cte as
(
select Common.SqlClr.fnEncryptPICString(MemNo) as MemNo from TrueMem
(nolock) where can = 6
)
select Common.SqlClr.fnDecryptPICString(m.TrueMemID) as TrueMemID
from cte
join Mem m (nolock)
on cte.MemNo = m.MemNo


This post was a FYI, unless Iam missing something in my queries. If anyone
is interested to dig deep, I can give the DDL.


Cheers,
AbbA

 >> Stay informed about: Table variable 
Back to top
Login to vote
Dan Guzman1

External


Since: Aug 22, 2004
Posts: 840



(Msg. 2) Posted: Thu Jan 07, 2010 7:26 am
Post subject: Re: Table variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> If you have the belief that CTEs are always faster than Table variables,
> its wrong.

Agreed, but I've personally never had such a belief. CTEs and table
variables are completely different beasts. Functions are especially
difficult to optimize and can result in cursor-like performance.

> This post was a FYI, unless Iam missing something in my queries. If anyone
> is interested to dig deep, I can give the DDL.

I think DDL for the Mem and TrueMem tables might be enough for readers to
reproduce the issue. I can't say for sure but I suspect T-SQL function
stubs could be substituted for the CLR functions to simplify the repro.

--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/


"Abba" wrote in message

> [SQL2005 latest sp]
>
> If you have the belief that CTEs are always faster than Table variables,
> its wrong.
>
> Below are two queries, the first using a table variable and the second
> uses a CTE. The first runs in a flash while the second one takes forever.
> The execution plans suggest, it has to do with the CTE using nested loops
> because it evaluates everything first which is very costly in this
> scenario.
>
> However, this is not always the case. There are queries when CTEs are
> actually faster. The bottomline is to test a query which uses a CTE or a
> table variable and check the execution plans rather than to blindly
> believe that CTEs are faster.
>
> declare @t table (MemNo varchar(100))
> insert into @t
> select Common.SqlClr.fnEncryptPICString(MemNo) as MemNo from TrueMem
> (nolock) where can = 6
> select Common.SqlClr.fnDecryptPICString(TrueMemID)
> from @t t
> join Mem m (nolock)
> on t.MemNo = m.MemNo
>
>
> ;with cte as
> (
> select Common.SqlClr.fnEncryptPICString(MemNo) as MemNo from TrueMem
> (nolock) where can = 6
> )
> select Common.SqlClr.fnDecryptPICString(m.TrueMemID) as TrueMemID
> from cte
> join Mem m (nolock)
> on cte.MemNo = m.MemNo
>
>
> This post was a FYI, unless Iam missing something in my queries. If anyone
> is interested to dig deep, I can give the DDL.
>
>
> Cheers,
> AbbA
>

 >> Stay informed about: Table variable 
Back to top
Login to vote
TheSQLGuru

External


Since: Jan 11, 2008
Posts: 579



(Msg. 3) Posted: Thu Jan 07, 2010 9:25 am
Post subject: Re: Table variable [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Put your value into a temporary TABLE instead of table variable and try
that.

--
Kevin G. Boles
Indicium Resources, Inc.
SQL Server MVP
kgboles a earthlink dt net


"Abba" wrote in message

> [SQL2005 latest sp]
>
> If you have the belief that CTEs are always faster than Table variables,
> its wrong.
>
> Below are two queries, the first using a table variable and the second
> uses a CTE. The first runs in a flash while the second one takes forever.
> The execution plans suggest, it has to do with the CTE using nested loops
> because it evaluates everything first which is very costly in this
> scenario.
>
> However, this is not always the case. There are queries when CTEs are
> actually faster. The bottomline is to test a query which uses a CTE or a
> table variable and check the execution plans rather than to blindly
> believe that CTEs are faster.
>
> declare @t table (MemNo varchar(100))
> insert into @t
> select Common.SqlClr.fnEncryptPICString(MemNo) as MemNo from TrueMem
> (nolock) where can = 6
> select Common.SqlClr.fnDecryptPICString(TrueMemID)
> from @t t
> join Mem m (nolock)
> on t.MemNo = m.MemNo
>
>
> ;with cte as
> (
> select Common.SqlClr.fnEncryptPICString(MemNo) as MemNo from TrueMem
> (nolock) where can = 6
> )
> select Common.SqlClr.fnDecryptPICString(m.TrueMemID) as TrueMemID
> from cte
> join Mem m (nolock)
> on cte.MemNo = m.MemNo
>
>
> This post was a FYI, unless Iam missing something in my queries. If anyone
> is interested to dig deep, I can give the DDL.
>
>
> Cheers,
> AbbA
>
 >> Stay informed about: Table variable 
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 vs. temp table - 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..

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

Table variable problem - Hi, I'm using mix of table variable and temp tables in the procedures. Sometimes table variable works faster sometimes temp table does better. There's some cases I looked at the execution plan saying it's using Table SCAN and the statistics is missing....

ALTER TABLE using a Variable - Hi, I have a table ('Table A') with a trigger. I want to add a column to another table ('Table B') when a record is INSERTED into 'Table A'. I want the name of the new column in 'Table B' to be a value from the record inserted INTO 'Table A. I am..
   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 ]