Skip to content

Instantly share code, notes, and snippets.

@taddison
Created October 30, 2017 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taddison/616ae19cb8b0c8e9a45e9ff117783054 to your computer and use it in GitHub Desktop.
Save taddison/616ae19cb8b0c8e9a45e9ff117783054 to your computer and use it in GitHub Desktop.
Record wait stats and schedule with tsqlscheduler
if not exists (select 1 from sys.schemas as s where s.name = 'logs')
exec('create schema logs authorization dbo');
go
if not exists(select 1 from sys.schemas as s where s.name = 'logging')
exec('create schema logging authorization dbo');
go
create table logs.InstanceWaitStats
(
CollectionTime datetime2(0) not null
,WaitType nvarchar(60) not null
,WaitingTasksCount bigint not null
,WaitTimems bigint not null
,MaxWaitTimems bigint not null
,SignalWaitTimems bigint not null
);
go
create clustered columnstore index CCIX_InstanceWaitStats on logs.InstanceWaitStats
go
create procedure logging.RecordInstanceWaitStats
as
begin
set nocount on;
insert logs.InstanceWaitStats
(
CollectionTime
,WaitType
,WaitingTasksCount
,WaitTimems
,MaxWaitTimems
,SignalWaitTimems
)
select getutcdate() as CollectionTime
,dows.wait_type
,dows.waiting_tasks_count
,dows.wait_time_ms
,dows.max_wait_time_ms
,dows.signal_wait_time_ms
from sys.dm_os_wait_stats as dows
where dows.waiting_tasks_count > 0;
end
go
insert into scheduler.Task
(
Identifier
,TSQLCommand
,StartTime
,FrequencyType
,FrequencyInterval
,NotifyOnFailureOperator
,IsNotifyOnFailure
,IsEnabled
,IsDeleted
)
values
(
N'DBAdmin-RecordInstanceWaitStats'
,N'exec DBAdmin.logging.RecordInstanceWaitStats'
,'00:00:00'
,3
,5
,N'DBAs'
,0
,1
,0
)
go
exec scheduler.CreateJobFromTask @identifier = N'DBAdmin-RecordInstanceWaitStats'
go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment