Answers:
不确定从简单查询中获取性能计数器时为什么要使用性能计数器。实际上,尽管您可以从性能计数器(Log File(s) Size (KB)
/ Log File(s) Used Size (KB)
)获取有关日志文件的信息,但是没有这样的计数器可以统计数据文件中使用了多少空间。
;WITH f AS
(
SELECT name, size = size/128.0 FROM sys.database_files
),
s AS
(
SELECT name, size, free = size-CONVERT(INT,FILEPROPERTY(name,'SpaceUsed'))/128.0
FROM f
)
SELECT name, size, free, percent_free = free * 100.0 / size
FROM s;
我有另一种方法可以主动监视数据文件空间,并使用SQL Alert来警告可用空间是否低于某个百分比。
基础是
在sys.messages中创建用户定义的错误消息。这将由sql代理警报使用。
-- User-defined error messages can be an integer between 50001 and 2147483647.
EXEC sp_addmessage
@msgnum=911421, -- 911DBA
@severity=1, -- Informational message not generated by DB Engine
@msgtext=N'Data files are %d percent full in database %s.'
现在创建一个SQL Agent作业。确保set @threshold = 20 --->>>>>>>>>>>>>>>>> CHANGE HERE <<<<<<<<<<<<<<<<<<<<<---
在以下脚本中进行更改。我把他的门槛设置得很低,只是为了模拟警报。安排作业每30分钟运行一次(根据您的需要进行更改)。
if object_id('tempdb..#dbserversize') is not null
DROP TABLE #dbserversize;
create table dbo.#dbserversize (
[id] int identity (1,1)
,[databaseName] sysname
,[Drive] varchar(3)
,[Logical Name] sysname
,[Physical Name] varchar(max)
,[File Size MB] decimal(38, 2)
,[Space Used MB] decimal(38, 2)
,[Free Space] decimal(38, 2)
,[%Free Space] decimal(38, 2)
,[Max Size] varchar(max)
,[Growth Rate] varchar(max)
)
declare @id int
declare @threshold int
declare @dbname sysname
declare @sqltext nvarchar(max)
declare @freespacePct int
set @threshold = 20 --->>>>>>>>>>>>>>>>> CHANGE HERE <<<<<<<<<<<<<<<<<<<<<---
select @dbname = min(name) from sys.databases where database_id > 4 and [state] = 0
while @dbname is not NULL
begin
select @dbname = name from sys.databases where name = @dbname and database_id > 4 and [state] = 0
--- Modified from Erin's blog : Proactive SQL Server Health Checks, Part 1 : Disk Space
--- source http://sqlperformance.com/2014/12/io-subsystem/proactive-sql-server-health-checks-1
set @sqltext = ' use '+@dbname+';'+'
insert into dbo.#dbserversize
select '''+@dbname+''' as [databaseName]
,substring([physical_name], 1, 3) as [Drive]
,[name] as [Logical Name]
,[physical_name] as [Physical Name]
,cast(CAST([size] as decimal(38, 2)) / 128.0 as decimal(38, 2)) as [File Size MB]
,cast(CAST(FILEPROPERTY([name], ''SpaceUsed'') as decimal(38, 2)) / 128.0 as decimal(38, 2)) as [Space Used MB]
,cast((CAST([size] as decimal(38, 0)) / 128) - (CAST(FILEPROPERTY([name], ''SpaceUsed'') as decimal(38, 0)) / 128.) as decimal(38, 2)) as [Free Space]
,cast(((CAST([size] as decimal(38, 2)) / 128) - (CAST(FILEPROPERTY([name], ''SpaceUsed'') as decimal(38, 2)) / 128.0)) * 100.0 / (CAST([size] as decimal(38, 2)) / 128) as decimal(38, 2)) as [%Free Space]
,case
when cast([max_size] as varchar(max)) = - 1
then ''UNLIMITED''
else cast([max_size] as varchar(max))
end as [Max Size]
,case
when is_percent_growth = 1
then cast([growth] as varchar(20)) + ''%''
else cast([growth] as varchar(20)) + ''MB''
end as [Growth Rate]
from sys.database_files
where type = 0 -- for Rows , 1 = LOG'
--print @sqltext
exec (@sqltext)
select @dbname = min(name) from sys.databases where name > @dbname and database_id > 4 and [state] = 0
end
--- delete the entries that do not meet the threshold
delete from dbo.#dbserversize
where [%Free Space] < @threshold;
--select * from dbo.#dbserversize
--- NOW Raise errors for the databases that we got flagged up
while exists (select null from dbo.#dbserversize)
begin
select top 1 @id = id,
@dbname = databaseName,
@freespacePct = [%Free Space]
from dbo.#dbserversize;
RAISERROR(911421, 10,1,@freespacePct, @dbname) with LOG;
delete from dbo.#dbserversize where id = @id;
end
现在创建警报以响应911421
错误号。
USE [msdb]
GO
EXEC msdb.dbo.sp_add_alert @name=N'MDF file alert',
@message_id=911421,
@severity=0,
@enabled=1,
@delay_between_responses=1800,
@include_event_description_in=0,
@job_id=N'019c4770-865b-406b-894e-72a1ff34f732'
GO
EXEC msdb.dbo.sp_add_notification @alert_name=N'MDF file alert', @operator_name=N'Notify 911 DBA for MDF files getting full', @notification_method = 1
GO
注意:您可以使用上述我的想法进行其他类型的增强。
只是基于Aaron和Kin的答案,您可以使用perf计数器来实现,但是可以使用用户可设置的计数器之一。
我会:
如果您想得到适当的通知:
注意事项是:
但是它们可以在Perfmon或其他类似工具中使用。