sql database shrink

Recovery steps:

1, view the log file size [larger] general recovery

- For RDS For SQL Server2012 \ 2016

SELECT DB_NAME(database_id) AS [Database Name],[Name] AS [Logical Name],[Physical_Name] AS [Physical Name],((size * 8) / 1024) AS [Size(MB)]

FROM sys.master_files

ORDER BY [Size(MB)] DESC


- For RDS For SQL Server2008R2, need to individually perform database

USE database name

GO

SELECT a.name as logical file name, size / 128 [totalspace file size (MB)],

FILEPROPERTY (a.name, 'SpaceUsed') / 128 [usedspace used space (megabytes)],

size / 128 - FILEPROPERTY (a.name, 'SpaceUsed') / 128 [unused space (megabytes)],

FILEPROPERTY (a.name, 'SpaceUsed') * 100.0 / size [usage rate (%)]

FROM sys.database_files a cross join (select recovery_model_desc, log_reuse_wait,log_reuse_wait_desc,is_auto_shrink_on from sys.databases where name=DB_NAME())b

WHERE type=1


2, review the log file space is recyclable log_reuse_wait_desc is [only] NOTHING state before recovery

SELECT [name] ,[log_reuse_wait_desc]

FROM master.sys.databases

WHERE [name] = 'database name [Step 1] Get'


3, recovery log file space

DBCC SHRINKFILE (logicalName [get] Step 1


Note: If the log files can not be recovered, you can refer to the help documentation to make the operation a transaction log shrink, there is likely to contract out
https://help.aliyun.com/knowledge_detail/41796.html

 

4, view the data file size

USE database name

GO

SELECT a.name as logical file name, size / 128 [totalspace file size (MB)],

FILEPROPERTY (a.name, 'SpaceUsed') / 128 [usedspace used space (megabytes)],

size / 128 - FILEPROPERTY (a.name, 'SpaceUsed') / 128 [unused space (megabytes)],

FILEPROPERTY (a.name, 'SpaceUsed') * 100.0 / size [usage rate (%)]

FROM sys.database_files a cross join (select recovery_model_desc, log_reuse_wait,log_reuse_wait_desc,is_auto_shrink_on from sys.databases where name=DB_NAME())b

WHERE type=0


5, shrink data files [in accordance with experience, preferably every 5G contraction cycle, if the impact of business interruption at any time, not rolled back]

declare @usedspace int ,@totalspace int

select @usedspace= xxx,@totalspace =yyy

while @totalspace> @usedspace

begin

set @totalspace= @totalspace-5 *1024

DBCC SHRINKFILE (logical file name, @ totalspace)

end


NOTE: logical file names, usedspace, totalspace result set obtained from step 4

6, to see the progress of the contraction [estimates]

SELECT DB_NAME(database_id) as dbname,

session_id, request_id, start_time,command

, percent_complete

, dateadd(mi ,estimated_completion_time/60000,getdate ()) as ETC

FROM sys.dm_exec_requests where percent_complete<>0

 

Explanation

SQLServer is running a single-threaded file shrink, even if you have multiple CPU performance will not help

Here I did a test, open two Query run ShrinkFile command, the second statement will complain:

File ID 1 of database ID 17 cannot be shrunk as it is eitherbeing shrunk by another process or is empty.

DBCCexecution completed. If DBCC printed error messages, contact your systemadministrator.

That can only have a contraction data run, run a second time will be given, can be viewed through Step 6 above if there is an ongoing process of compression


Three steps to shrink the file (you can see from sys.dm_exec_request command field)

Step Command Description

1 DbccSpaceReclaim clean up and clear the delayed allocation for the data movement to prepare the empty section.

2 DbccFilesCompact needed to move a page from the target before the target and to truncate the file.

3 DbccLOBCompact compression LOB data.

 

Guess you like

Origin www.cnblogs.com/lb809663396/p/12006268.html