×


Auto shrink feature in SQL server

Auto shrink database property in SQL server allows to automatically shrink the database files if the value is set to ON/True.
However, it is not a good practice to shrink a database. Because it is a very expensive operation in terms of I/O, CPU usage, locking, and transaction log generation.
It is only recommended to shrink the database in cases like data deletion. That is we can run the shrink operation if we’ve deleted some portion of the data to retrieve that space back. Also, we need to ensure that we rebuild the fragmented indexes post performing the shrink operation because this can cause index fragmentation.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform SQl related queries.
In this context, we shall look into AutoShrink property in SQL.

The need to enable Auto shrink database property in SQL server ?

Here are the different reasons why the auto shrink property must not be enabled for the database.
1. It will cause index fragmentation which in return reduces the database performance.
2. Shrink operation takes a lot of IO and CPU resources. As a result, it may cause long disk queue lengths and possibly IO timeouts.
3. Repeatedly shrinking and growing the data files will cause file-system level fragmentation. As a result, it will slow down performance.
4. Growing the database in small increments or growing it and then shrinking can cause disk fragmentation. This can cause performance issues.
5. It slows down the database server. Because shrinking is a heavy load operation that would take a long time to complete.
6. It is unpredictable. Because the Autoshrink operation can start at any time, maybe during the peak time.

How to enable or disable the AutoShrink operation in SQL Server?

Follow the steps below to enable and disable the AutoShrink database property:
1. We can turn it On or Off using the SQL Server Management Studio or T-SQL code.
2. First, we right-click on the database for which we need to enable or disable the AutoShrink. Then we choose the Properties.
3. Next, on the Database Properties window, we click on the Options from the left side pane.
4. Now, we can see the AutoShrink option on the right side of the pane. If we wish to turn it On/Off we can click on the drop-down of the option and select True/False.

Here is the T-SQL code to enable or disable the AutoShrink option for any database:

To Set Auto Shrink ON or True;

ALTER DATABASE DBName SET AUTO_SHRINK ON

To Set Auto Shrink OFF or False;

ALTER DATABASE DBName SET AUTO_SHRINK OFF

[Need urgent assistance with SQL queries? – We'll help you. ]


Conclusion

This article will guide you on how to enable and disable AutoShrink #database property. Basically, it is not recommended to enable the AutoShrink database property as it can cause many issues in the #SQL Server.
Auto Shrink is database property that allow SQL Server to automatically shrink database files if its value set to ON/True. Shrinking a database is not a good practice because it is very expensive operation in terms of I/O, CPU usage, locking and #transaction log generation.
Users can enable and disable database auto shrink option using SSMS and T-SQL both ways.
To turn on auto shrink in SQL Server:
1. Activating the database auto shrink by using #SSMS:
2. Enable database auto shrink using T-SQL:
3. Database >> #Reports >> Standard Reports >> Disk Usage.
4. Check free space for the database files:
5. Shrink database file: