Avoiding Bottlenecks with Temporary Tables

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
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
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.


Leave a comment

Your email address will not be published.