通过DMVS采集并存储SQL Server性能计数器数据
我想定期去采集并存储一些SQL Server性能计数器数据,因为服务器太多,又不想用Windows系统自带的性能监视器在每一台服务器上去部署。
通过动态性能视图sys.dm_os_performance_counters,编写采集SQL代码,通过SQL Server Agent作业定期采集性能数据。
第一步:
创建注册服务器组,将要采集的服务器分组。可供后面批量部署脚本。
先通过下面的语句熟悉性能计数器的存储内容:
SELECT [object_name], [counter_name], [instance_name], [cntr_value] FROM sys.dm_os_performance_counters WHERE counter_name = 'Buffer cache hit ratio';
第二步:
我利用一个专门的数据库在所有的被监控数据库服务器上来放置采集逻辑,包括表、视图、存储过程和函数,然后在每个数据库实例上用统一的规则运行。
这里,我创建iDBA数据库,以及一些处理sys.dm_os_performance_counters数据采集的对象:
・ MetaBOT schema
・ MetaBOT.dm_os_performance_counters表 -- 存储从sys.dm_os_performance_counters采集的结果
・ MetaBOT.watched_counters表 �C 存储打算采集的计数器
USE [iDBA]; GO CREATE SCHEMA MetaBOT AUTHORIZATION dbo; GO IF NOT EXISTS (SELECT name FROM iDBA.sys.[tables] T WHERE name = 'dm_os_performance_counters') CREATE TABLE [MetaBOT].[dm_os_performance_counters] ( [object_name] NVARCHAR(128), [counter_name] NVARCHAR(128), [instance_name] NVARCHAR(128), [cntr_value] bigint, [date_stamp] DATETIME ); IF NOT EXISTS (SELECT name FROM iDBA.sys.[tables] T WHERE name = 'watched_counters') CREATE TABLE [iDBA].[MetaBOT].[watched_counters] ( [object_name] NVARCHAR(128), [counter_name] NVARCHAR(128), [active] bit ); GO
然后初始化MetaBOT.watched_counters表,最后创建存储过程,通过SQL Server Agent作业去存储到MetaBOT.dm_os_performance_counters。关于命名实例在object_name列的影响,动态地通过基于对象名和实例名构建,通过数据库系统变量@@SERVICENAME:
--+-- Now populate the watched counters table based upon instance properties DECLARE @NamedInstance bit DECLARE @ObjectNamePrefix VARCHAR(50) SELECT @NamedInstance = 1 IF @@SERVICENAME = 'MSSQLSERVER' BEGIN SELECT @NamedInstance = 0 --This is the default instance INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Buffer cache hit ratio', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Buffer cache hit ratio base', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Database pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Free pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Page life expectancy', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Page lookups/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Page reads/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Page writes/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Reserved pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Stolen pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Target pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Buffer Manager', 'Total pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Cursor Manager by Type', 'Active cursors', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Databases', 'Active Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Databases', 'Data File(s) Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Databases', 'Log File(s) Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Databases', 'Log File(s) Used Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Databases', 'Percent Log Used', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Databases', 'Transactions/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Exec Statistics', 'DTC calls', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Exec Statistics', 'OLEDB calls', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_namme], [counter_name], [active]) VALUES ('SQLServer:General Statistics', 'Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:General Statistics', 'User Connections', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Latches', 'Latch Waits/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Latches', 'Total Latch Wait Time (ms)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Locks', 'Average Wait Time (ms)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Locks', 'Lock Wait Time (ms)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Memory Manager', 'Target Server Memory (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Memory Manager', 'Total Server Memory (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Plan Cache', 'Cache Hit Ratio', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Plan Cache', 'Cache Hit Ratio Base', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Plan Cache', 'Cache Pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:SQL Statistics', 'Batch Requests/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Transactions', 'Free Space in tempdb (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Transactions', 'Longest Transaction Running Time', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Transactions', 'NonSnapshot Version Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Transactions', 'Snapshot Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Transactions', 'Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Transactions', 'Update Snapshot Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Transactions', 'Version Store Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Lock waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Log buffer waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Log write waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Memory grant queue waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Network IO waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Non-Page latch waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Page IO latch waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Page latch waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Thread-safe memory objects waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Transaction ownership waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Wait for the worker', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES ('SQLServer:Wait Statistics', 'Workspace synchronization waits', 1); END ELSE BEGIN --Account for named instance when adding object names to watch SELECT @ObjectNamePrefix = 'MSSQL$' + @@SERVICENAME INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Buffer cache hit ratio', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Buffer cache hit ratio base', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Database pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Free pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Page life expectancy', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Page lookups/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Page reads/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Page writes/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Reserved pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Stolen pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Target pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Buffer Manager', 'Total pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Cursor Manager by Type', 'Active cursors', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Databases', 'Active Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Databases', 'Data File(s) Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Databases', 'Log File(s) Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Databases', 'Log File(s) Used Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Databases', 'Percent Log Used', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Databases', 'Transactions/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Exec Statistics', 'DTC calls', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Exec Statistics', 'OLEDB calls', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':General Statistics', 'Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':General Statistics', 'User Connections', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Latches', 'Latch Waits/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Latches', 'Total Latch Wait Time (ms)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Locks', 'Average Wait Time (ms)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Locks', 'Lock Wait Time (ms)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Memory Manager', 'Target Server Memory (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Memory Manager', 'Total Server Memory (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Plan Cache', 'Cache Hit Ratio', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Plan Cache', 'Cache Hit Ratio Base', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Plan Cache', 'Cache Pages', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':SQL Statistics', 'Batch Requests/sec', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Transactions', 'Free Space in tempdb (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Transactions', 'Longest Transaction Running Time', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Transactions', 'NonSnapshot Version Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Transactions', 'Snapshot Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Transactions', 'Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Transactions', 'Update Snapshot Transactions', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Transactions', 'Version Store Size (KB)', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Lock waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Log buffer waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Log write waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Memory grant queue waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Network IO waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Non-Page latch waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Page IO latch waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Page latch waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Thread-safe memory objects waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Transaction ownership waits', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Wait for the worker', 1); INSERT INTO [MetaBOT].[watched_counters] ([object_name], [counter_name], [active]) VALUES (@ObjectNamePrefix + ':Wait Statistics', 'Workspace synchronization waits', 1); END
你可能注意到我跟踪了大量的性能计数器,我期望部署的计数器能覆盖到问题所以我宁愿多采集而不会不够。根据你的环境和数据库的数量,你可能希望采集更少的计数器信息。一些计数器在你的数据库计数器中是1:1的关系。我基于我的实例去做计算,决定每行数据存储平均232字节;基于此,我然后评估多久运行一次我的数据采集作业。运行采集进程每10分钟,估计每月存储需要1.5GB。当每两分钟运行一次,每月存储增加到8GB,并且这只是单实例的情况。
现在创建要在SQL Server Agent作业里调用的存储过程。它是一个简单的INSERT通过sys.dm_os_performance_counters过滤行INNER JOIN关联MetaBOT.watched_counters表。我没有创建任何索引在这个表,因为表数据量太小。遵循规则一条没有被使用的索引是不需要的。
CREATE PROCEDURE MetaBOT.usp_collect_perfmon_counters AS DECLARE @datestamp DATETIME SELECT @datestamp = GETDATE() INSERT INTO MetaBOT.[dm_os_performance_counters] ( [object_name], [counter_name], [instance_name], [cntr_value], [date_stamp] ) SELECT DOPC.[object_name], DOPC.[counter_name], DOPC.[instance_name], DOPC.[cntr_value], @datestamp FROM sys.[dm_os_performance_counters] DOPC INNER JOIN iDBA.[MetaBOT].[watched_counters] WC ON [DOPC].[object_name] = [WC].[object_name] AND [DOPC].[counter_name] = [WC].[counter_name] ORDER BY [object_name], [counter_name];
第三步:
在每一个实例上创建用于采集性能计数器值的作业。
最简单的方式,是通过SQL Server Management Studio图形管理界面创建作业,然后生成脚本。我就是这样做的。然后,有一项需要去修改。你必需要么移除或注释掉job_id GUID的输出代码行。这是不需要的,会导致脚本失败。有一个变量@RunEveryXMinutes在代码中允许你设置作业的运行频率;设置这个值为作业运行的间隔分钟。
USE [msdb] GO /****** Object: Job [Metadata_Collect_SQL_Perfmon_Counters] Script Date: 12/09/2010 12:14:38 ******/ BEGIN TRANSACTION DECLARE @ReturnCode INT DECLARE @RunEveryXMinutes TINYINT SELECT @ReturnCode = 0 SELECT @RunEveryXMinutes = 10 /****** Object: JobCategory [Tuning and Optimization] Script Date: 12/09/2010 12:14:39 ******/ IF NOT EXISTS ( SELECT name FROM msdb.dbo.syscategories WHERE name=N'Tuning and Optimization' AND category_class=1) BEGIN EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'Tuning and Optimization' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback END DECLARE @jobId BINARY(16) EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Metadata_Collect_SQL_Perfmon_Counters', @enabled=1, @notify_level_eventlog=0, @notify_level_email=0, @notify_level_netsend=0, @notify_level_page=0, @delete_level=0, @description=N'Poll and store output from sys.dm_os_performance_counters', @category_name=N'Tuning and Optimization', @owner_login_name=N'SPECTRUM-HEALTH\svcSQLNotify', @job_id = @jobId OUTPUT IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback /****** Object: Step [Collect Metadata] Script Date: 12/09/2010 12:14:39 ******/ EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Collect Metadata', @step_id=1, @cmdexec_success_code=0, @on_success_action=1, @on_success_step_id=0, @on_fail_action=2, @on_fail_step_id=0, @retry_attempts=0, @retry_interval=0, @os_run_priority=0, @subsystem=N'TSQL', @command=N'EXEC MetaBOT.usp_collect_perfmon_counters;', @database_name=N'iDBA', @flags=0 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1 IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_add_jobschedule @job_id=@jobId, @name=N'dm_os_perfmon_counters Job', @enabled=1, @freq_type=4, @freq_interval=1, @freq_subday_type=4, @freq_subday_interval=@RunEveryXMinutes, @freq_relative_interval=0, @freq_recurrence_factor=0, @active_start_date=20101209, @active_end_date=99991231, @active_start_time=30, @active_end_time=235959 -- ,@schedule_uid=N'519325e6-3114-453c-bfeb-6597300a45d2' <---Comment this line out of auto-generated script IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)' IF (@@ERROR <> 0 OR @ReturnCode <> 0) GOTO QuitWithRollback COMMIT TRANSACTION GOTO EndSave QuitWithRollback: IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION EndSave: GO
一旦启动作业,你就可以在MetaBOT.dm_os_performance_counters表查询相关计数。我推荐你设置清理命令,这已经被作为MetaBOT.usp_collect_perfmon_counters存储过程的一部分,去从MetaBOT.dm_os_performance_counters表删除date_stamp列小于你设置的阈值的的记录。根据你的性能需要,我也推荐在该列创建索引。