Free Newsletters:
DatabaseJournal  
DBANews
Database Journal
Search Database Journal:
 
HOME News MS SQL Oracle DB2 Access MySQL PostgreSQL PHP SQL Etc Scripts Links Forums DBA Talk
internet.com

» HOME
» NEWS
» FEATURES
» RESOURCES
Products
Books
Scripts
Links
» FORUMS

internet.commerce
Be a Commerce Partner
Online Universities
Prepaid Phone Card
Search Engine Ranking
Custom Websites
Shared Hosting
Memory Flash Cards
Cell Phone Plans
Promote Your Website
Compare Book Prices
Get Free Ringtones
Compare MP3 Players
Phone Systems
Website Marketing




IP E911: Better Plan Than FCC's?

Warily Watching Worm Variants

Akamai's News Junkie Index

internet.com
Developer
Downloads
International
Internet Lists
Internet News
Internet Resources
IT
Linux/Open Source
Personal Technology
Small Business
Windows Technology
xSP Resources

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers



Whitepaper: Symantec LiveState™ Delivery. Configure, provision, update and migrate client devices, and aid recovery from disrupted operations.

MS SQL
June 12, 2000
Avoiding Temporary Table Bottlenecks
By Neil Boyle

Overview

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


Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
Simple Distinct Query (?) Shitmeister 0 August 19th, 02:28 PM
Free Query Tool from AgileInfoSoftware MarkBrown 0 August 17th, 10:00 PM
Tool for syncronization Databases on 2 servers lena 4 August 17th, 09:59 PM
pupblish saad 1 August 15th, 10:32 AM






JupiterWeb networks:

internet.comearthweb.comDevx.comGraphics.com

Search JupiterWeb:

Jupitermedia Corporation has four divisions:
JupiterWeb, JupiterResearch, JupiterEvents and JupiterImages

Copyright 2005 Jupitermedia Corporation All Rights Reserved.
Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Jupitermedia Corporate Info | Newsletters | Tech Jobs | Shopping | E-mail Offers