Re: Table Variable vs Temporary Table

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Louis Davidson (dr_dontspamme_sql_at_hotmail.com)
Date: 11/24/04


Date: Wed, 24 Nov 2004 11:27:48 -0600

It probably was, and certainly no fault to be assigned here :) I just think
back to how I felt when I was a newbie and consider what detail I would like
to read about a problem and just added some color.

-- 
----------------------------------------------------------------------------
Louis Davidson - drsql@hotmail.com
SQL Server MVP
Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design - 
http://www.apress.com/book/bookDisplay.html?bID=266
Note: Please reply to the newsgroups only unless you are interested in 
consulting services.  All other replies may be ignored :)
"Alejandro Mesa" <AlejandroMesa@discussions.microsoft.com> wrote in message 
news:3000ADF3-D8D3-4A4A-8F27-BBE47FA4D5C9@microsoft.com...
>I did not mention it, thought that mentioning that a new execution plan is
> being generated was enough.
>
> mea culpa!!!
>
>
>
> AMB
>
> "Louis Davidson" wrote:
>
>> And in some cases this can be a good thing.  Temporary tables act more 
>> like
>> physical tables, and as such can give better performance.  Best practice 
>> is
>> to only use local variable tables when you are working with small 
>> datasets,
>> where things like stats don't make a difference.  Too much data and they
>> become too much overhead (as Yoshi said a few posts back :)
>>
>> -- 
>> ----------------------------------------------------------------------------
>> Louis Davidson - drsql@hotmail.com
>> SQL Server MVP
>>
>> Compass Technology Management - www.compass.net
>> Pro SQL Server 2000 Database Design -
>> http://www.apress.com/book/bookDisplay.html?bID=266
>> Note: Please reply to the newsgroups only unless you are interested in
>> consulting services.  All other replies may be ignored :)
>>
>> "Alejandro Mesa" <AlejandroMesa@discussions.microsoft.com> wrote in 
>> message
>> news:74E4B1E2-DD76-473F-9D0B-43C831AF481B@microsoft.com...
>> > Temporary tables also cause sp recompilation (a new execution plan).
>> >
>> > INF: Frequently Asked Questions - SQL Server 2000 - Table Variables
>> > http://support.microsoft.com/default.aspx?scid=kb;en-us;305977
>> >
>> >
>> >
>> > AMB
>> >
>> > "Yoshi" wrote:
>> >
>> >> Craig,
>> >>
>> >> We had a similiar issue where a developer wrote a Stored Procedure 
>> >> using
>> >> a
>> >> temporary "memory" variable. The process took all day. I changed it to 
>> >> a
>> >> temporary table and it ran under 3 hours.
>> >>
>> >> This is what I understand... table variables are created in memory 
>> >> (RAM).
>> >> They are supposed to be faster because of this. However, I have found
>> >> that
>> >> if the server has limited RAM and is not a dedicated SQL Server, table
>> >> variables can be quite slow. I believe if the table variable is
>> >> processing
>> >> alot of data and the server doesn't have not RAM/memory, then it swaps 
>> >> to
>> >> disk.
>> >>
>> >> My rule is if your dealing with alot of data, then use a temporary 
>> >> table
>> >> otherwise use the table variable.
>> >>
>> >> Y
>> >>
>> >>
>> >> "CB" <craig.bryden@derivco.com> wrote in message
>> >> news:unzTDNj0EHA.3808@tk2msftngp13.phx.gbl...
>> >> > Hi
>> >> >
>> >> > I have discoverred a situation where the performance between using a
>> >> > table
>> >> > variable and a temporary table is remarkably different.
>> >> > We have a query that declares a table variable, then populates it 
>> >> > (this
>> >> > is
>> >> > quick) and then this is used in a very complicated query where it is
>> >> > being
>> >> > referenced many times in sub-queries. The query takes about 180 
>> >> > seconds
>> >> > to
>> >> > run.
>> >> >
>> >> > I changed the query to use a temporary table. There were no logic
>> >> > changes.
>> >> > In fact I simply did a search and replace on the table name (i am
>> >> > aliasing
>> >> > the table everywhere). Using this method the query runs in 5 
>> >> > seconds.
>> >> >
>> >> > Can anyone please explain this difference to me? I did expect that 
>> >> > the
>> >> > two
>> >> > would not be the same but this is ridiculous.
>> >> >
>> >> > Any help would be appreciated
>> >> >
>> >> > Thanks
>> >> >
>> >> > Craig
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >>
>>
>>
>> 


Relevant Pages

  • Re: CONTAINS performance
    ... That said, and with the query plan, I can start to give you more ... relational join in the context of the free-text optimization, ... SQL Server tables. ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Indexing Service, Openquery and sp_executesql
    ... SQL Server version and sp are you running? ... > data from the indexing service catalog when pasted into ... > query analyzer, but failed when put against sp_executesql ... I would choose Microsoft Indexing ...
    (microsoft.public.sqlserver.fulltext)
  • Re: Problem using Access or Query Designer to run queries in SQL Serve
    ... >or Query Designer within Enterprise Manager, it works and I get data back. ... >ODBC Call Failed [ODBC SQL Server Driver] Timeout Expirederror in Access ... >[ODBC SQL Server Driver] Timeout Expired ...
    (microsoft.public.sqlserver.odbc)
  • Re: Transaction Isolation Level
    ... This means that while I initiate a transaction selecting all new orders it ... My query is executed on a SQL server 2005. ... the isolation level you should use is snapshot isolation. ...
    (comp.databases.ms-sqlserver)
  • Extreme performance issues (SQL Server 2000/ADO.NET/C#)
    ... This process runs very quickly if run through Query ... same exact stored procedures and views, run in the same exact order, through ... system that runs SQL Server (a 4-cpu Xeons system with 2gigs of physical ... When I execute these steps manually through query analyser,, ...
    (microsoft.public.dotnet.framework.adonet)