Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
SQL Scripts & Samples
» Database Forum
» Slideshows
Free Newsletters:

News Via RSS Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles


Posted Jun 12, 2000

Avoiding Temporary Table Bottlenecks

By Neil Boyle


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

Bottlenecks in version 6.5

Many people use a select...into query to create a temporary table, something like this:

select *
into #tempTable
from source

While this works, it creates locks against the temporary 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 this 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 it the following format:

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.

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 optimiser is usually smart enough to realise 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 the tempdb.

Bottlenecks in version 6.5 and 7

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

The problem arises when you use the INSERT...EXEC statement to load a temporary table, and the stored procedure itself creates temporary tables, 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 for both versions 6.5 and 7

MS SQL Archives

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM