Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Restoring multiple transaction log backupsThe differences between LEN and DATALENGTH in SQL Server »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Someone was writing some queries that brought back a lot of data (and I mean a LOT!!) and after a while he got the following message

Connectivity error: [Microsoft][ODBC SQL Server Driver][SQL Server]Could not allocate new page for database 'TEMPDB'. There are no more pages available in filegroup DEFAULT. Space can be created by dropping objects, adding additional files, or or allowing file growth

That is not good, fortunately this wasn't one of our production machines but a dev/test box.

So what causes this? You might think that tempdb is only used for temporary(#temp or ##temp) tables but that is not true, tempdb is used for a lot of thing and since SQL server 2005 it is used more than ever. Here are some things that tempdb is used for:

  • If you do any ordering on a query and this needs more memory than you have available in RAM it will also go to tempdb
  • If you have resultsets that are large and you use unions, group by, outer joins, cursors etc
  • If you use temporary tables
  • Uncommited transaction that have not been rolled back
  • DBCC CHECKDB will use the tempdb, the larger your database the more space DBCC CHECKDB will need from tempdb

    If you are creating or rebuilding indexes with the SORT_IN_TEMPDB = ON option

    Here is some sample code that demonstrates that

    1. USE AdventureWorks;
    2. GO
    3. ALTER INDEX ALL ON Production.Product
    4. REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON,
    5.               STATISTICS_NORECOMPUTE = ON);
    6. GO

    See also tempdb and Index Creation in Books On Line
    Here is an excerpt

    If the SORT_IN_TEMPDB option is set to ON and tempdb is on a separate set of disks from the destination filegroup, during the first phase, the reads of the data pages occur on a different disk from the writes to the sort work area in tempdb. This means the disk reads of the data keys generally continue more serially across the disk, and the writes to the tempdb disk also are generally serial, as do the writes to build the final index. Even if other users are using the database and accessing separate disk addresses, the overall pattern of reads and writes are more efficient when SORT_IN_TEMPDB is specified than when it is not.

How can you fix this problem?
Here are a couple of ways

  1. Restart the SQL Server service, this will recreate the tempdb database
  2. Add another file on another disk with more space
  3. Shrink the log file of tempdb

Now most likely you want to put in place some long term solutions after you did a quick fix. First of all, you should make sure that autogrow is not turned off. If you run the query below you don't want to see a value of 0 in the growth column

  1. SELECT growth,* FROM master..sysaltfiles
  2. WHERE dbid = DB_ID('tempdb')

This query below is another way of getting the growth value for tempdb

  1. USE tempdb
  2. go
  3.  
  4. EXEC SP_HELPFILE

You also want to make sure that tempdb is in simple recovery mode and not in full, this query will return the recovery mode for tempdb

  1. SELECT DATABASEPROPERTYEX('tempdb','recovery')

Ideally you want tempdb on its own drive, doing this will also improve performance. If you can't have tempdb on its own drive then make sure you have plenty of space on that drive and if not then add one or more files on another drive.

Make sure that your queries have covering indexes, George wrote an article that explains how to create covering indexes here: SQL Server covering indexes

Most likely you don't really want to return multi million rows in 1 big chunk, try doing it in batches that are smaller. The same applies for delete statements, try to do those in chunks, this will also perform much better.




*** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
1707 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)