Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Sql Server Performance Expand / Collapse
Author
Message
Posted Wednesday, January 18, 2012 4:33 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 9:28 AM
Points: 48, Visits: 273
Hi,
If we create Temporay Tables in a Stored Procedure will degrade the performance of the query?
Ex:
CREATE TABLE #TempTable
(id int,name varchar(50))

INSERT INTO #TempTable
SELECT id,name FROM Employee

How to improve the performance of stored procedure while using temporary tables?

Thanks,
Suresh
Post #1238378
Posted Thursday, January 19, 2012 1:19 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:07 AM
Points: 40,404, Visits: 36,848
suresh0534 (1/18/2012)
Hi,
If we create Temporay Tables in a Stored Procedure will degrade the performance of the query?


Maybe, or maybe it'll improve the performance due to been able to process smaller chunks of data. You should probably only use them when they do improve things



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1238492
Posted Friday, January 20, 2012 6:56 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 7:10 AM
Points: 4,436, Visits: 6,338
suresh0534 (1/18/2012)
Hi,
If we create Temporay Tables in a Stored Procedure will degrade the performance of the query?
Ex:
CREATE TABLE #TempTable
(id int,name varchar(50))

INSERT INTO #TempTable
SELECT id,name FROM Employee

How to improve the performance of stored procedure while using temporary tables?

Thanks,
Suresh


I have gotten 4+ orders of magnitude performance improvement by adding temporary table(s) to a stored procedure. I have also gotten 4+ orders of magnitude performance improvement by REMOVING temporary table(s) from a sproc. There is no right answer and doing this type of refactoring/tuning isn't something that can be covered in a few forum posts. Sorry...


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1239419
Posted Friday, January 20, 2012 7:16 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, November 5, 2014 6:18 AM
Points: 219, Visits: 340
Hi,

You can also use table variables for better performance but it has some limitations like,
1. Transaction logs are not recorded for the table variables. Hence, they are out of scope of the transaction mechanism
2. Any procedure with a temporary table cannot be pre-compiled, while an execution plan of procedures with table variables can be compiled in advance.
3. Table variables exist only in the same scope as variables
Post #1239437
Posted Friday, January 20, 2012 7:32 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 10:07 AM
Points: 40,404, Visits: 36,848
deepkt (1/20/2012)

1. Transaction logs are not recorded for the table variables. Hence, they are out of scope of the transaction mechanism


Half true. They are out of transaction scope, but that does not mean that they are unlogged. They are logged like any other operations

2. Any procedure with a temporary table cannot be pre-compiled, while an execution plan of procedures with table variables can be compiled in advance.


Stored procedures are never pre-compiled. Execution plans are created on first execution only and remain until invalidated or removed from cache. In SQL 2000 temp tables did always cause recompiles, not the case in 2005 or above.

You also neglected to mention the severe performance implication (in a bad way) from the lack of statistics on table variables. They should be avoided if the correct costing of execution plans is even a consideration.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1239450
Posted Monday, January 23, 2012 11:07 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 9:58 AM
Points: 64, Visits: 268
If you are doing a lot of Temp table operations. You can improve performance by moving the TempDB files to a different drive and Create multiple files as oppose to one.
This is not a direct answer but it will help on a large scale.
Post #1240351
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse