Tuesday, February 28, 2012

Shrink Database fails with Error “Backup, file manipulation operations”

In some cases users may get below error while trying to shrink their databases manually or using SQLServer management studio.

Executing the query "DBCC SHRINKDATABASE(N'OperationsManagerDW', 10, TR..." failed with the following error: "Backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

Possible reason:
The backup is running when user are trying to perform the database shrink. Please note that below operations may not be performed at the same time as a backup operation

• File management operations such as the ALTER DATABASE statement with either ADD FILE or REMOVE FILE clauses.
• The file truncation phase of shrink database or shrink file.

Please note that if backup is running and one of the above operations is attempted, then operation fails immediately. When user is trying to add or remove a file and if backups is started then backup will wait for a timeout period, then fail.

If user is trying to shrink database or file while a backup is running, the shrink stops without truncating the file. If backup started after shrink database operation then backup waits for shrink operation to complete.

Action:
Reissue the statement after the current backup or file manipulation operation is completed.

Regards,
Satishbabu Gunukula
http://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