Shrink Database Files

SQL Allow you to shrink data and log file. You can shrink a file to a size that is less than the size specified when it was created. This resets the minimum file size to the new value.
We will discussed here some ways to shrink database files.

Before we move to shrink file using queries, first we need to get names of files which are attached to the database.
Below is the query which is used to retrieve logical name of mdf and ldf files.

Get Name of mdf and lfd file

Syntax:

USE Database_Name
GO
EXEC sp_helpfile
GO


Shrink database file using queries

By using this query you will be find name of files. No we will move to use this file name into the query to shrink those files.

Syntax:

Use Database_Name
DBCC SHRINKFILE (FileNameToShrink' , 1)

SHRINKFILE requires two parameter. First is the name of file to shrink and other is size for the file in megabytes, expressed as an integer. If not specified, DBCC SHRINKFILE reduces the size to the default file size. The default size is the size specified when the file was created.

Shrink database files through management studio

  1. Log in to the Microsoft SQL 2000 Server as an administrator.
  2. Open SQL Enterprise Manager.
  3. Right-click the database that VirtualCenter is using.
  4. Click Properties.
  5. Click the Options Tab.
  6. Set the Recovery Model to Simple as follows:

  1. Click OK.
  2. Right-click on the database again.
  3. Click All Tasks ->Shrink Database.
  4. On the Shrink Database window, click Files.
  5. Select the transaction log from the database file list. This appears in the list as databasename_Log as follows:
The space used versus the space allocated displays. In general after you set the recovery model to Simple you see the majority of the space in the transaction log released.
  1. Ensure that the Compress pages and then truncate free space from end of file radio button is selected. 
  2. Click OK to shrink the log.
  3. Click OK, to perform a shrink the database, and close the wizard. 
Space is now freed on the server.  If there is more than one transaction log for the database, perform the above steps for each of the logs.


Comments

Popular posts from this blog

Connecting Remote server with Local Server

MDX Error handling tricks

JSON_Automated_stored_procedure