Avoiding Temporary Table Bottlenecks
June 12, 2000
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:
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
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:
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
Even though SQL Server will not trawl through the
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.