Showing posts with label TempDB. Show all posts
Showing posts with label TempDB. Show all posts

Friday, March 9, 2012

How to move TempDB in SQLServer?

If the TempDB grows fast and does not have have enough space to grow then the best option is move the TempDB to another physical drive, which will help to improve database performance.

You will receive below error messages, if tempdb is full

Source: MSSQLSERVER
Event ID: 17052
Description: The log file for database 'tempdb' is full.
Back up the transaction log for the database to free up some log space


Server: Msg 1101, Level 17, State 10, Line 1
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 allowing file growth.


Follow the below steps to move the tempdb to new location.

Step 1: Open a “New Query” Window and run the below command to get the path and names of the TempDB.

USE TempDB
GO
EXEC sp_helpfile
GO

Or

SELECT name, physical_name FROM sys.master_files
WHERE database_id = DB_ID('TempDB');

Step 2: In the following query Replace “NewPath” with new drive path and run the command to change the LDF and MDF file path.

USE master;
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = tempdev, FILENAME = '\tempdb.mdf');
GO
ALTER DATABASE tempdb
MODIFY FILE (NAME = templog, FILENAME = '}\templog.ldf');
GO

The tempDB definition has been changed, but no changes are made to until you restart SQLServer. Please stop and restart SQL Server and

Step 3: Stop and Start the SQLServer

Control Panel -- > Administrative Tools --> Services --> Right click on SQLServer (MSSQLSERVER) --> Restart

or

SQL Server Configuration Manager --> SQLServer 2008 Services --> Right Click on SQLServer (MSSQLSERVER) --> Restart

Now TempDB files created in new drive.

Step 4: Check the TempDB new file location patch

SELECT name, physical_name FROM sys.master_files
WHERE database_id = DB_ID('TempDB');

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

Thursday, March 1, 2012

How to Shrink Tempdb in SQLServer

Once in a while DBA’s will come across the situation that tempdb is full and no longer able to expand. You can avoid this situation by proactively monitoring the system with notification system setup for particular threshold.

To avoid this situation proactive monitoring is the best solution and it is always recommend to setup alerts for particular threshold.

It's highly recommended to disable "Autogrow" option for better performance. By default this option is enabled and it grows by 10% of its initial size. While performing auto growth, it will lock all applications and internal operations. This will also lead to fragmentation in the tempdb database.

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. The default size of tempdb is 8MB

You can try below solutions to shrink the databases.

Method 1: Shrink database using DBCC SHRINKDATABASE.

DBCC SHRINKDATABASE (tempdb,’’)
For ex:-
DBCC SHRINKDATABASE(tempdb,40)

This command shrinks the tempdb database as a whole with specified percentage.

Method 2: Shrink the tempdb data file or log file

Use tempdb
GO
--shrink the data file
DBCC shrinkfile (tempdb_data,’< target_size_in_MB’>)
GO
--shrink the log file
DBCC shrinkfile (tempdb_log, ’)

Ex:-
DBCC shrinkfile (tempdb_data, 5000);

Method 3: shrink the tempdb data file or log file using T-SQL Command

-- Shrink the data file
ALTER DATABASE tempdb MODIFY FILE (NAME = 'temp_data', SIZE = target_size_in_MB)

-- Shrink the log file
ALTER DATABASE tempdb MODIFY FILE (NAME = 'temp_log', SIZE = target_size_in_MB)

Where target_size_in_MB= Desired target size for the data/log file

Errors while shrinking tempdb
If you have any active transactions in tempdb then you may receive below consistency errors.

1. Server: Msg 2501, Level 16, State 1, Line 1 Could not find table named '1525580473'. Check sysobjects.

This error may not be an indicative of any corruption, but it causes the shrink operation to fail.

2. Server: Msg 8909, Level 16, State 1, Line 0 Table Corrupt: Object ID 1, index ID 0, page ID %S_PGID. The PageId in the page header = %S_PGID.

This 8909 error indicates tempdb corruption. You can clean up the consistency errors by restarting the SQL Server. If you still see any errors then restore the database from valid backup.

3. DBCC SHRINKFILE: Page 1:456120 could not be moved because it is a work file page.

This error indicates that Page could not be moved because it is a work file page. You can release the cached objects by running “DBCC FREEPROCCACHE”. Now try shrink tempdb once again to release the free space.

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

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