首页 > 数据库 >SQLServer增量收缩数据文件的大小

SQLServer增量收缩数据文件的大小

时间:2022-10-25 14:24:42浏览次数:60  
标签:数据文件 DBFileName -- time SQLServer file 增量 128 name

增量收缩数据文件脚本

-- SQLServer--Shrink_DB_File.sql
/*
This script is used to shrink a database file in increments until it reaches a target free space limit.

Run this script in the database with the file to be shrunk.
1. Set @DBFileName to the name of database file to shrink.
2. Set @TargetFreeMB to the desired file free space in MB after shrink.
3. Set @ShrinkIncrementMB to the increment to shrink file by in MB
4. Run the script
*/

declare @DBFileName sysname
declare @TargetFreeMB int
declare @ShrinkIncrementMB int

-- Set Name of Database file to shrink
set @DBFileName = 'MyDatabaseFileName'

-- Set Desired file free space in MB after shrink
set @TargetFreeMB = 1000

-- Set Increment to shrink file by in MB
set @ShrinkIncrementMB = 50

-- Show Size, Space Used, Unused Space, and Name of all database files
select
[FileSizeMB]=
convert(numeric(10,2),round(a.size/128.,2)),
[UsedSpaceMB]=
convert(numeric(10,2),round(fileproperty( a.name,'SpaceUsed')/128.,2)) ,
[UnusedSpaceMB]=
convert(numeric(10,2),round((a.size-fileproperty( a.name,'SpaceUsed'))/128.,2)) ,
[DBFileName]= a.name
from
sysfiles a

declare @sql varchar(8000)
declare @SizeMB int
declare @UsedMB int

-- Get current file size in MB
select @SizeMB = size/128. from sysfiles where name = @DBFileName

-- Get current space used in MB
select @UsedMB = fileproperty( @DBFileName,'SpaceUsed')/128.

select [StartFileSize] = @SizeMB, [StartUsedSpace] = @UsedMB, [DBFileName] = @DBFileName

-- Loop until file at desired size
while @SizeMB > @UsedMB+@TargetFreeMB+@ShrinkIncrementMB
begin

set @sql =
'dbcc shrinkfile ( '+@DBFileName+', '+
convert(varchar(20),@SizeMB-@ShrinkIncrementMB)+' ) '

print 'Start ' + @sql
print 'at '+convert(varchar(30),getdate(),121)

exec ( @sql )

print 'Done ' + @sql
print 'at '+convert(varchar(30),getdate(),121)

-- Get current file size in MB
select @SizeMB = size/128. from sysfiles where name = @DBFileName

-- Get current space used in MB
select @UsedMB = fileproperty( @DBFileName,'SpaceUsed')/128.

select [FileSize] = @SizeMB, [UsedSpace] = @UsedMB, [DBFileName] = @DBFileName

end

select [EndFileSize] = @SizeMB, [EndUsedSpace] = @UsedMB, [DBFileName] = @DBFileName

-- Show Size, Space Used, Unused Space, and Name of all database files
select
[FileSizeMB]=
convert(numeric(10,2),round(a.size/128.,2)),
[UsedSpaceMB]=
convert(numeric(10,2),round(fileproperty( a.name,'SpaceUsed')/128.,2)) ,
[UnusedSpaceMB]=
convert(numeric(10,2),round((a.size-fileproperty( a.name,'SpaceUsed'))/128.,2)) ,
[DBFileName]= a.name
from
sysfiles a

  

查看收缩的进度:

SELECT
	a.database_id,
	d.name,
	a.session_id,
	a.command,
	b.text,
	percent_complete,
	done_in_minutes = a.estimated_completion_time / 1000 / 60,
	min_in_progress = DATEDIFF( MI, a.start_time, DATEADD( ms, a.estimated_completion_time, GETDATE( ) ) ),
	a.start_time,
	CONVERT ( VARCHAR, ( total_elapsed_time / ( 1000 ) ) / 60 ) + 'M ' + CONVERT ( VARCHAR, ( total_elapsed_time / ( 1000 ) ) % 60 ) + 'S' AS [Elapsed],
	CONVERT ( VARCHAR, ( estimated_completion_time / ( 1000 ) ) / 60 ) + 'M ' + CONVERT ( VARCHAR, ( estimated_completion_time / ( 1000 ) ) % 60 ) + 'S' AS [ETA],
	a.cpu_time,
	a.status,
	estimated_completion_time = DATEADD( ms, a.estimated_completion_time, GETDATE( ) ),
	connection_id,
	blocking_session_id
FROM
	sys.dm_exec_requests a CROSS APPLY sys.dm_exec_sql_text ( a.sql_handle ) b
	LEFT JOIN sys.databases D ON a.database_id = d.database_id 
WHERE
	command LIKE '%dbcc%';

  

标签:数据文件,DBFileName,--,time,SQLServer,file,增量,128,name
From: https://www.cnblogs.com/abclife/p/16824692.html

相关文章

  • efcore 连接SqlServer2008R2报错:'OFFSET' 附近有语法错误。 在 FETCH 语句中选项 NEXT
    用的是EFCore6,连接SqlServer2008R2时,生成的分页方法会报错,只需要指定ProviderName时加上版本号就行:Microsoft.EntityFrameworkCore.SqlServer@2008,高于2008版本就按默......
  • PVE创建ubuntu CT并安装sqlServer
    一、创建LXC容器模板选择下载好的ubuntu20.04网络选择一个未分配的静态IP    二、登录到ubuntu系统,开启ssh登录......
  • [20221020]奇怪的增量备份.txt
    [20221020]奇怪的增量备份.txt--//生产系统做增量备份遇到的怪异问题,给奇葩的运维人员狠狠地涮了一把,做一个记录:1.环境:[email protected]:1521/orcl>@pr==========......
  • SqlServer修改数据库文件存放位置
    --查看当前的存放位置selectdatabase_id,name,physical_nameASCurrentLocation,state_desc,sizefromsys.master_files wheredatabase_id=db_id(N'数据库名'); --......
  • 位置式PID和增量式PID
    PID介绍PID是Proportional(比例)、Integral(积分)、Differential(微分)的首字母缩写;是一种结合比例、积分和微分三种环节于一体的闭环控制算法。PID控制的实质是对目标值和实......
  • SQLServer 分页Offset ...Rows Fetch Next ... Rows only
    语法:select[column1],[column2]...,[columnN]from[tableName]orderby[columnM]offset(pageNum-1)*pageSizerowsfetchnextpageSizerowsonly备注:column1......
  • jmeter使用CSV文件保存参数,读取为传参(CSV数据文件设置)
    为了模拟多用户操作系统,可以把用户名、密码等需要传递的参数保存于CSV文件,自动读取即可   这里我使用来读取的是token,批量保存下拉短期不过期的token ......
  • java连接sqlserver的方法分享
    转自:http://www.java265.com/JavaJingYan/202206/16552126983712.htmlsqlserver:   SQLServer是由Microsoft开发和推广的关系数据库管理系统(DBMS),它最初是由Micros......
  • 增量表与全量表
    目录如何判断一张表是增量表还是全量表如何判断一张表是增量表还是全量表1.看每天的数据量变化,如果每天都差不多,是全量,如果变化很大,是增量。验证语句:selectdt,count(1)......
  • SQLServer常用SQL脚本
    SQLServer系统表查询 selectnamefromsyscolumnswhereid=object_id('表名')SELECT*FROMSysObjectsWhereXType='U'SELECTNameFROMSysObjectsWhereXT......