Friday, February 24, 2012

What is TEMPDB in SQLServer?

What is TempDB?
The tempdb is a system database, which is global resource available to all users.

The tempdb is a temporary workspace for storing temporary tables, worktables that hold intermediate results during the sorting or query processing and materialized static cursors, which intern increase the performance for SQLServer.

The SQLServer records the information in tempdb which is required to roll back a transaction in case required. It will not record any information which is required for database recovery. The tempdb is re-created each time the SQL Server service starts with clean copy of the database. The tempdb is created from model database and reset to its last configured size.

Create or move your temp db in a driver where you have adequate storage. It is advised to place tempdb on RAID 1+0 (dependent on TEMPDB usage) or RAID 10 for performance benefits.

Who can access tempdb?
Any user can create the objects in tempdb, but they can only access their own objects unless until they have additional permissions.

Can I Backup tempdb Backups?
The tempdb can only be configured in the simple recovery model. Note that no backup and restore operations are not allowed on tempdb.

How to estimate Tempdb size?
It is very difficult to estimate the tempdb space requirement for an application. You can only estimate the size with experience and experiment to gain a satisfactory result. It is always recommend keeping 20% more space for tempdb.

The following features use tempdb:
• Temporary tables, table variables, and table-valued functions
• Query, Cursors, Triggers
• Snapshot isolation and read committed snapshot
• MARS
• Index creation, Online index creation
• DBCC CHECK
• LOB parameters , XML and LOB variable
• Service Broker and event notification
• Query notifications, Database mail
• User-defined functions

If your application using tempdb database more and causing it to grow larger than its default size then it is advised to increase the default size of the tempdb to a size closer to what is typically used by your application.

Can I shrink the tempdb?
Yes, you can shrink tempdb. But shrinking files regularly is not a recommended practice, because these files may probably grow again. Also shrink operations causes’ data fragmentation.

We have some limitations on shrinking tempdb:
• Shrink operations do not shrink the version store or internal objects.
• In some cases the DBA might need to restart the server in a single user mode to allow shrinking of the tempdb.

You can query sys.dm_db_file_space_usage table to see the space allocation. Use below query to see space allocation information for the files associated with tempdb

SELECT
SUM (user_object_reserved_page_count)*8 as usr_obj_kb,
SUM (internal_object_reserved_page_count)*8 as internal_obj_kb,
SUM (version_store_reserved_page_count)*8 as version_store_kb,
SUM (unallocated_extent_page_count)*8 as freespace_kb,
SUM (mixed_extent_page_count)*8 as mixedextent_kb
FROM sys.dm_db_file_space_usage

Major Problems with TEMPDB:-
1. tempdb has run out of space.
2. tempdb is experiencing I/O bottleneck
3. tempdb is experiencing contention

Regards
Satishbabu Gunukula
http://www.sqlserver-expert.com

3 comments:

  1. very informative!
    i got this like:
    user can manipulate tempdb but it will resume its original state after sql server restarts or session ends... is that so?
    thanks

    ReplyDelete
  2. Each time the SQL Server service starts, the tempdb is newly created by copying from the model database and inheriting some database configurations from it. Note that shrinking the tempdb causes fragmentation and other issues.

    Please see my other post on "Shrink TempDB". It should clear all your doubts.

    http://www.sqlserver-expert.com/2012/03/how-to-shrink-tempdb-in-sqlserver.html

    ReplyDelete
  3. Great article. When you say that query, cursors and triggers use tempdb, does that include performing a delete statement as well ? In other words, if I delete 1 million records from a table, does that affect tempdb size ?
    Thanks

    ReplyDelete