识别文件增长事件

Mat*_*DBA 9 sql-server datafile

我在我的投资组合中发现了许多使用默认自动增长设置(1 MB 或 10% 增量)创建的数据库,这些数据库已经扩展了很长一段时间。如果我想了解每个 DB 文件的外部碎片量,是否可以从元数据中获取 DB 文件的大小修改次数(通过自动增长或手动)?澄清一下,我是否可以从元数据中获取 DB 生命周期内 DB 文件修改的历史记录,而不仅仅是因为实例重启?

Tho*_*ger 20

如果启用,您可以从默认跟踪中获取自动增长事件信息:

select distinct
    ei.eventid,
    e.name
from sys.fn_trace_geteventinfo(1) ei
inner join sys.trace_events e
on e.trace_event_id = ei.eventid
where name like '%grow%';
Run Code Online (Sandbox Code Playgroud)

从中可以看出,默认跟踪确实具有数据文件自动增长日志文件自动增长事件捕获。要查看您是否在该实例上启用了默认跟踪,您可以执行以下操作:

exec sp_configure 'default trace enabled';
go
Run Code Online (Sandbox Code Playgroud)

注意:这是一个高级配置选项,因此show advanced options必须设置为 1 才能通过sp_configure. 此外,如果文件手动增长,则不会触发这两个事件

这是获取这些事件的快速示例查询:

select
    te.name as event_name,
    tr.DatabaseName,
    tr.FileName,
    tr.StartTime,
    tr.EndTime
from sys.fn_trace_gettable('<Trace Path>', 0) tr
inner join sys.trace_events te
on tr.EventClass = te.trace_event_id
where tr.EventClass in (92, 93)
order by EndTime;
Run Code Online (Sandbox Code Playgroud)

你可以<Trace Path>从系统函数中得到sys.fn_trace_getinfo

select *
from sys.fn_trace_getinfo(1);
Run Code Online (Sandbox Code Playgroud)