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


Sql Server Performance


Sql Server Performance

Author
Message
suresh0534
suresh0534
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1027 Visits: 344
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)

Group: General Forum Members
Points: 555429 Visits: 47762
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, 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


TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)SSC Guru (77K reputation)

Group: General Forum Members
Points: 77624 Visits: 8880
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 on googles mail service
deepkt
deepkt
Mr or Mrs. 500
Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)Mr or Mrs. 500 (521 reputation)

Group: General Forum Members
Points: 521 Visits: 362
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)SSC Guru (555K reputation)

Group: General Forum Members
Points: 555429 Visits: 47762
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, 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


Abhishek Chaturvedi
Abhishek Chaturvedi
Mr or Mrs. 500
Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)Mr or Mrs. 500 (564 reputation)

Group: General Forum Members
Points: 564 Visits: 283
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.
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