时间:2023-10-26来源:系统城装机大师作者:佚名
SQL Server在删除数据后,会重新利用这部分空间,所以如果不是空间紧张的情况下,可以不回收。
回收一般先回收日志文件,因为这个回收速度非常快,可以短时间内清理出一部分可用空间。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
--适用于RDS For SQL Server2012 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 --适用于RDS For SQL Server2008R2,需要对数据库逐个执行 USE 数据库名 GO SELECT a. name as 逻辑文件名, size /128 [totalspace文件大小(兆)], FILEPROPERTY(a. name , 'SpaceUsed' )/128 [usedspace已用空间(兆)], size /128 - FILEPROPERTY(a. name , 'SpaceUsed' )/128 [未用空间(兆)], FILEPROPERTY(a. name , 'SpaceUsed' )*100.0/ size [使用率(%)] 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 |
1 2 3 4 5 |
SELECT [ name ] ,[log_reuse_wait_desc] FROM master.sys.databases WHERE [ name ]= '数据库名【第1步获取】' |
1 | DBCC SHRINKFILE(logicalName【第1步获取】) |
1 2 3 4 5 6 7 |
常见的日志等待类型是 LOG_BACKUP,日志还没有备份,所以不能截断 解决方案: ACTIVE_TRANSACTION,有活跃事务阻塞了日志截断 解决方案: 执行 DBCC OPENTRAN ,获取下长时间的活跃事务的SPID 然后执行 DBCC INPUTBUFFER(SPID) 查看下这个请求SQL,考虑是否可以kill阻塞源,kill后再查下log_reuse_wait,尝试shrink |
1 2 3 4 5 6 7 8 |
USE 数据库名 GO SELECT a. name as 逻辑文件名, size /128 [totalspace文件大小(兆)], FILEPROPERTY(a. name , 'SpaceUsed' )/128 [usedspace已用空间(兆)], size /128 - FILEPROPERTY(a. name , 'SpaceUsed' )/128 [未用空间(兆)], FILEPROPERTY(a. name , 'SpaceUsed' )*100.0/ size [使用率(%)] 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 |
1 2 3 4 5 6 7 |
declare @usedspace int ,@totalspace int select @usedspace= xxx,@totalspace =yyy while @totalspace> @usedspace begin set @totalspace= @totalspace-5 *1024 DBCC SHRINKFILE( 逻辑文件名,@totalspace ) end |
注:逻辑文件名,usedspace,totalspace从第4步的结果集获取
1 2 3 4 5 |
SELECT DB_NAME(database_id) as dbname, session_id, request_id, start_time , percent_complete , dateadd(mi ,estimated_completion_time/60000,getdate ()) as ETC FROM sys.dm_exec_requests where percent_complete<>0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
--查询当前数据库备份进度 SELECT DB_NAME(er.[database_id]) [DatabaseName],er.[command] AS [CommandType],er.[percent_complete] ,er.start_time, CONVERT ( DECIMAL (5, 2) , er.[percent_complete]) AS [Complete_Percent] , CONVERT ( DECIMAL (38, 2), er.[total_elapsed_time] / 60000.00) AS [ElapsedTime_m] , CONVERT ( DECIMAL (38, 2), er.[estimated_completion_time] / 60000.00) AS [EstimatedCompletionTime_m] FROM sys.dm_exec_requests AS er WHERE er.[command] in ( 'RESTORE DATABASE' , 'BACKUP DATABASE' ) --DB_NAME(er.[database_id]) in ('ky2011') and --查看数据库收缩进度 SELECT DB_NAME(er.[database_id]) [DatabaseName],er.[command] AS [CommandType],er.[percent_complete] ,er.start_time, CONVERT ( DECIMAL (5, 2) , er.[percent_complete]) AS [Complete_Percent] , CONVERT ( DECIMAL (38, 2), er.[total_elapsed_time] / 60000.00) AS [ElapsedTime_m] , CONVERT ( DECIMAL (38, 2), er.[estimated_completion_time] / 60000.00) AS [EstimatedCompletionTime_m] FROM sys.dm_exec_requests as er WHERE command in ( 'DbccFilesCompact' , 'AUTOSHRINK' ) |
到此这篇关于SQL Server 数据文件收缩和查看收缩进度的文章就介绍到这了
2023-10-27
windows11安装SQL server数据库报错等待数据库引擎恢复句柄失败解决办法2023-10-27
SQL Server截取字符串函数操作常见方法2023-10-27
浅谈SELECT *会导致查询效率低的原因收缩数据文件通过将数据页从文件末尾移动到更靠近文件开头的未占用的空间来恢复空间,在文件末尾创建足够的空间后,可取消对文件末尾的数据页的分配并将它们返回给文件系统,本文给大家介绍SQL Server 数据库中的收缩数据...
2023-10-27