SQLServer 维护脚本分享(02)数据库文件增长及收缩情况

--先确定是否开启了默认跟踪,没有则需开启跟踪一段时间
exec sp_configure 'show advanced options',1  
reconfigure  
exec sp_configure 'default trace enabled'


--当前数据库文件历史增长记录
exec sp_executesql @stmt=N'begin try  
if (select convert(int,value_in_use) from sys.configurations where name = ''default trace enabled'' ) = 1 
begin 
declare @curr_tracefilename varchar(500) ; 
declare @base_tracefilename varchar(500) ; 
declare @indx int ;

select @curr_tracefilename = path from sys.traces where is_default = 1 ; 
set @curr_tracefilename = reverse(@curr_tracefilename);
select @indx  = patindex(''%\%'', @curr_tracefilename) ;
set @curr_tracefilename = reverse(@curr_tracefilename) ;
set @base_tracefilename = left( @curr_tracefilename,len(@curr_tracefilename) - @indx) + ''\log.trc'' ;  

select  (dense_rank() over (order by StartTime desc))%2 as l1
,       convert(int, EventClass) as EventClass
,		case convert(int, EventClass) 
			when 92 then ''Data File Auto Grow''
			when 93 then ''Log File Auto Grow'' 
			when 94 then ''Data File Auto Shrink'' 
			when 95 then ''Log File Auto Shrink'' 
		end as description
,       DatabaseName
,       Filename
,       (Duration/1000) as [Duration(ms)]
,       StartTime
,       EndTime
,       (IntegerData*8.0/1024) as [ChangeInSize(MB)]
from ::fn_trace_gettable( @base_tracefilename, default ) 
where EventClass >=  92      and EventClass <=  95        
and ServerName = @@servername   and DatabaseName = db_name()  
order by StartTime desc ;   
end     else    
select -1 as l1, 0 as EventClass, 0 DatabaseName, 0 as Filename, 0 as Duration, 0 as StartTime, 0 as EndTime,0 as ChangeInSize 
end try 
begin catch 
select -100 as l1
,       ERROR_NUMBER() as EventClass
,       ERROR_SEVERITY() DatabaseName
,       ERROR_STATE() as Filename
,       ERROR_MESSAGE() as Duration
,       1 as StartTime, 1 as EndTime,1 as ChangeInSize 
end catch',@params=N''




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值