I have several databases that cannot compress data files (problem not connected with logs files). The recovery model is simple. I made a backup and tried to use single user mode. It feels like all I can do is make a new data file and move the data there, and then into the original file after compressing it. And delete the second file, then update the indexes. Does anyone know an easier way? The problem is common, but I haven’t found an answer. I have SQL 2019, Standard.
I try de touch, offline, kill all connections, single user mode, recovery mode is simple, and nothing help
6
The things you tried shouldn’t work. SQL Server doesn’t shrink. Once it grows it’s files it keeps the space, even when the amount of space used inside those files decreases. And you should let it have space well beyond what it currently actually needs. If you don’t and it finds itself lacking space it will start the process of growing which can slow down things dramatically, right when you don’t need it. Particularly if the growth increment is small you may get many small and slow growths.
So, only if it’s really necessary should you shrink a file. It should be mentioned that shrinking files may lead to fragmentation. Defragmentation is part of my routine anyways, thus not much of a problem for me. So if, for example, an unusual query made the log file grow to a multiple of it’s former size I shrink it manually, using this:
- Checkpoint, twice to make sure.
- Get size and usage rate of the files of the db using a query
ofsys.database_files
. - Shrink the file using
DBCC SHRINKFILE
. This only works in recovery mode Simple. Parameters are the values of
name
from the query for data file(s) and log file, and – this one is crucial, pay attention here – the desired size in MB. For the data file I usually go with something like 30% above the current space used, but this is for You to decide. - Get size and usage rate again and compare.
This assumes you have a data file named YourDB
. If you want to shrink a log file named YourDB_log
exchange the name.
USE YourDB
GO
/* get size and usage rate */
SELECT
file_id
,name
,type_desc
,total_size_GB
,used_size_GB
,usage_in_percent = ROUND(used_size_GB / total_size_GB * 100, 1)
,physical_name = physical_name
FROM sys.database_files
OUTER APPLY( SELECT total_size_GB = ROUND(CONVERT(FLOAT, size * 8.0 / 1024 / 1024 ),3)
,used_size_GB = ROUND(CONVERT(FLOAT, FILEPROPERTY(name, 'SpaceUsed')/128.0/1024 ),3) )oa
/* write all unsaved changes from memory to disk */
CHECKPOINT
GO
CHECKPOINT
GO
/* shrink */
DBCC SHRINKFILE(
YourDB
,??? /* put in a number in unit MB */
)
GO
If you do any of this in production you may kill your business.
6