Introduction
Shrink database helps to reduce and compress database sql serverwhich leads reclaim spaces by recovering spaces. This blog, guides to shrink SQL server (MSSQL shrink database) to recovers space in SQL Server by using Microsoft SQL Server Management Studio 2017 and TSQL shrink database query.
Getting Started
SQL shrinking database moves pages of data from the end of the file to unoccupied space closer to the front of the file to recover spaces. When enough free space is created at the end of the file, data pages at the end of the file can be deallocated and returned to the file system.
Note that you cannot shrink a database while the database is being backed up. Conversely, you cannot back up a database while a shrink operation on the database is in process.
This demonstration is conducted in SQL Server Management Studio 2017. Here we will discuss how to shrink the SQL server in two different ways. Using SSMS options and SQL query.
Open SQL Server Management Studio and login to your SQL instance.
In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
Expand Databases, and then right-click the database that you want to shrink.
- Point to Tasks, point to Shrink, and then click Database.
- Click OK.
Database
Displays the name of the selected database.
Current allocated space
Displays the total used and unused space for the selected database.
Available free space
Displays the sum of free space in the log and data files of the selected database.
Reorganize files before releasing unused space
Selecting this option is equivalent to executing DBCC SHRINKDATABASE specifying a target percent option. Clearing this option is equivalent to executing DBCC SHRINKDATABASE with the TRUNCATEONLY option. By default, this option is not selected when the dialog is opened. If this option is selected, the user must specify a target percent option.
Maximum free space in files after shrinking
Enter the maximum percentage of free space to be left in the database files after the database has been shrunk. Permissible values are between 0 and 99.
The following SQL query helps to shrink the database. This example uses DBCC SHRINKDATABASE to decreases the size of the data and log files in the database and to allow for 10 percent free space in the database.
DBCC SHRINKDATABASE (database_name, 10);
GO
Tsql shrink database
The database cannot be made smaller than the minimum size of the database. The minimum size is the size specified when the database was originally created, or the last explicit size set by using a file-size-changing operation, such as DBCC SHRINKFILE.
For example, if a database was originally created with a size of 10 MB and grew to 100 MB, the smallest size the database could be reduced to is 10 MB, even if all the data in the database has been deleted.
Data that is moved to shrink a file can be scattered to any available location in the file. This causes index fragmentation and can slow the performance of queries that search a range of the index. To eliminate the fragmentation, consider rebuilding the indexes on the file after a shrink database.
Related Articles
- Create User In SQL Server Management Studio
- Recover SQL Databases from Suspect Mode in SQL Server
- SQL Sever Firewall Ports
- SQL Server Shrink Log File
- SQL Server Backup with Password
- Create A Database in SQL Server
Summary
In the above, we learn how to string SQL server database using two different ways using SSMS and T-SQL query. I hope you have enjoyed it a lot.
Thanks