Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Filtered Indexes in SQL Server 2008
Importance of Database Backups and Recovery Plan
Data Compression in SQL Server 2008
SQL Server 2008 MERGE Statement

More     
 
Latest FAQ's

ALTER TABLE SWITCH statement failed because the object '%.*ls' is not ...
ALTER TABLE SWITCH statement failed because column '%.*ls' at ordinal %d ...
ALTER TABLE SWITCH statement failed because table '%.*ls' has %d columns ...
SQL Server Reporting Server (SSRS) service is failing to start ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> performance tuning >> Avoiding Bottlenecks with Temporary Tables ...

Avoiding Bottlenecks with Temporary Tables

By : Neil Boyle
Apr 05, 2000

Overview

Were you aware that the act of populating a SQL Server temporary table can cause system-wide performance bottlenecks on your server? Problems can occur both with SQL Server 6.5 and 7.0 in different ways, and in this article I will discuss how best to avoid them.

 

Bottlenecks in SQL Server 6.5

Many people use a SELECT...INTO query to create a temporary table, something like this:

SELECT *
INTO #TempTable
FROM SourceTable


While this works, it creates locks against the tempdb database for the duration of the SELECT statement (quite a while if you are trawling through a lot of data in the source table, and longer still if the SELECT...INTO is at the start of a longer-running explicit transaction) While the lock is in place, no other user can create temporary tables. The actual location of the bottleneck is a lock on tempdb system tables. In later versions of SQL Server, the locking model has changed and the problem is avoided.

If a number of concurrent processes are trying to load temporary tables in this way, particularly if large amounts of data are involved, then a bottleneck is inevitable. 

The trick to freeing up tempdb is to ensure that the "create temporary table" part of the operation is committed as quickly as possible. To do this, recode the above statement like this: 

CREATE TABLE #temp
(
......
)

INSERT #temp
SELECT *
FROM SourceTable


In this manner we create our temporary table and free the sysobjects or schema lock as quickly as possible, substantially reducing locking, and boosting performance.

 

Short Cut to a Solution

If you want to avoid coding the INSERT...INTO statement, or if you are writing a generic piece of code and will not know the exact table definition until run-time, you can revert to this trick:

SELECT *
INTO #temp
FROM SourceTable
WHERE 1 = 0

INSERT #temp
SELECT *
FROM SourceTable


Obviously WHERE 1 = 0 is never true. No matter how much data is in SourceTable SQL Server's optimizer is usually smart enough to realize that because one is never equal to zero, it's not worth trawling through the source table. (If I can I always check the query execution plan just to make sure, but I have never caught it out yet.) 

Even though SQL Server will not trawl through the source table, the #temp table will still be defined, based on the format of the data in the SELECT statement, but it will contain no rows. You can then run the INSERT...SELECT statement secure in the knowledge that you are not blocking other processes' access to tempdb.

 

Bottlenecks in SQL Server 6.5 and 7.0

For the most part, the problem described above does not apply to SQL Server 7.0, but there is one instance where you can still unintentionally create these bottlenecks under either version of SQL Server.

The problem arises when you use the INSERT...EXEC statement to load a temporary table, as the stored procedure itself creates temporary tables, and you end up with blocking locks in tempdb similar to those described above. The prescribed workarounds are either "don't do it in the first place", which is inconvenient if you do not want to mess with legacy code or code you do not control, or otherwise to execute the stored procedure as a remote stored procedure, (i.e. "INSERT #temp EXEC server.database.owner.proc) which again is not ideal in all circumstances.

 

Further Reading

Check out this TechNet article for more information on tempdb locking problems in SQL 6.5, and see this article that deals with locking issues found in both SQL Server 6.5 and 7.0.


        








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved