Skip to content

Commit

Permalink
Merge pull request #39 from marcingminski/development
Browse files Browse the repository at this point in the history
weekly merge into master.
  • Loading branch information
marcingminski authored Sep 14, 2018
2 parents 1763e69 + 85ce53f commit e9857bd
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 26 deletions.
1 change: 1 addition & 0 deletions SQLWATCHDB/SQLWATCH.sqlproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Build Include="dbo\Procedures\sp_sql_perf_mon_logger.sql" />
<Build Include="dbo\Procedures\sp_sql_perf_mon_retention.sql" />
<Build Include="dbo\Procedures\sp_sql_perf_mon_add_database.sql" />
<Build Include="dbo\Tables\sql_perf_mon_config_snapshot_header_type.sql" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Script.PostDeployment1.sql" />
Expand Down
11 changes: 11 additions & 0 deletions SQLWATCHDB/Script.PostDeployment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,17 @@ if (select count(*) from [dbo].[sql_perf_mon_config_report_time_interval]) = 0
--------------------------------------------------------------------------------------
--
--------------------------------------------------------------------------------------
if (select count(*) from [dbo].[sql_perf_mon_config_snapshot_type]) = 0
begin
insert into [dbo].[sql_perf_mon_config_snapshot_type]
values (1, 'Performance', 7),
(2, 'Growth', 365)
end

--------------------------------------------------------------------------------------
--
--------------------------------------------------------------------------------------


--setup jobs
--we have to switch database to msdb but we also need to know which db jobs should run in so have to capture current database:
Expand Down
Binary file modified SQLWATCHDB/bin/Debug/SQLWATCH.dacpac
Binary file not shown.
Binary file modified SQLWATCHDB/bin/Debug/SQLWATCHDB.dll
Binary file not shown.
12 changes: 8 additions & 4 deletions SQLWATCHDB/dbo/Procedures/sp_sql_perf_mon_logger.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ declare @sql nvarchar(4000)
--------------------------------------------------------------------------------------------------------------
select @date_snapshot_previous = max([snapshot_time])
from [dbo].[sql_perf_mon_snapshot_header]
where snapshot_type_id = 1

set @date_snapshot_current = getdate();
insert into [dbo].[sql_perf_mon_snapshot_header]
values (@date_snapshot_current)
values (@date_snapshot_current, 1)


--------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -98,6 +99,7 @@ declare @sql nvarchar(4000)
,base_cntr_value=bc.cntr_value
,pc.cntr_type
,snapshot_time=@date_snapshot_current
, 1
from (
select * from sys.dm_os_performance_counters
union all
Expand Down Expand Up @@ -150,7 +152,7 @@ declare @sql nvarchar(4000)
-- get process memory
--------------------------------------------------------------------------------------------------------------
insert into dbo.sql_perf_mon_os_process_memory
select snapshot_time=@date_snapshot_current, *
select snapshot_time=@date_snapshot_current, * , 1
from sys.dm_os_process_memory

--------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -245,6 +247,7 @@ declare @sql nvarchar(4000)
-- there are many memory clerks. we'll chart any that make up 5% of sql memory or more; less significant clerks will be lumped into an "other" bucket
graph_type=case when mc.total_kb / convert(decimal, ta.total_kb_all_clerks) > 0.05 then mc.[type] else N'other' end
,memory_available=@memory_available
, 1
from @memory_clerks as mc
-- use a self-join to calculate the total memory allocated for each time interval
join
Expand Down Expand Up @@ -277,13 +280,14 @@ declare @sql nvarchar(4000)
fs.num_of_reads, fs.num_of_bytes_read, fs.io_stall_read_ms, fs.num_of_writes, fs.num_of_bytes_written,
fs.io_stall_write_ms, fs.size_on_disk_bytes,
snapshot_time=@date_snapshot_current
, 1
from sys.dm_io_virtual_file_stats (default, default) as fs
inner join sys.master_files as f on fs.database_id = f.database_id and fs.[file_id] = f.[file_id]
--------------------------------------------------------------------------------------------------------------
-- wait stats snapshot
--------------------------------------------------------------------------------------------------------------
insert into [dbo].[sql_perf_mon_wait_stats]
select [wait_type], [waiting_tasks_count], [wait_time_ms],[max_wait_time_ms], [signal_wait_time_ms], [snapshot_time]=@date_snapshot_current
select [wait_type], [waiting_tasks_count], [wait_time_ms],[max_wait_time_ms], [signal_wait_time_ms], [snapshot_time]=@date_snapshot_current, 1
from sys.dm_os_wait_stats;
--------------------------------------------------------------------------------------------------------------
-- sp_whoisactive
Expand Down Expand Up @@ -312,7 +316,7 @@ declare @sql nvarchar(4000)
,[database_name],[program_name],[sql_text],[sql_command],[login_name]
,[open_tran_count],[wait_info],[blocking_session_id],[blocked_session_count]
,[CPU],[used_memory],[tempdb_current],[tempdb_allocations],[reads]
,[writes],[physical_reads],[login_time]
,[writes],[physical_reads],[login_time], 1
from [dbo].[sql_perf_mon_who_is_active_tmp]
-- exclude anything that has been running for less that the desired age in seconds (default 60)
-- this parameterised so feel free to change it to your liking. To change parameter:
Expand Down
19 changes: 14 additions & 5 deletions SQLWATCHDB/dbo/Procedures/sp_sql_perf_mon_retention.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE PROCEDURE [dbo].[sp_sql_perf_mon_retention](
@retention_period_days smallint = 7,
--this is only kept for backward compatibility so we dont have to change existing jobs for now:
@retention_period_days smallint = 7,
@batch_size smallint = 500
)
as
Expand All @@ -9,10 +10,18 @@ declare @row_count int = 1
while @row_count > 0
begin
begin tran
delete top (@batch_size)
from dbo.sql_perf_mon_snapshot_header with (readpast)
where datediff(day,snapshot_time,getdate()) > @retention_period_days
set @row_count = @@ROWCOUNT
delete top (@batch_size)
from dbo.sql_perf_mon_snapshot_header
where exists (
select sh.[snapshot_time], sh.snapshot_type_id, st.snapshot_retention_days
from dbo.sql_perf_mon_snapshot_header sh
inner join [dbo].[sql_perf_mon_config_snapshot_type] st
on sh.[snapshot_type_id] = st.[snapshot_type_id]
where datediff(day,sh.snapshot_time,getdate()) > st.snapshot_retention_days
and dbo.sql_perf_mon_snapshot_header.[snapshot_time] = sh.[snapshot_time]
and dbo.sql_perf_mon_snapshot_header.snapshot_type_id = sh.snapshot_type_id
)
set @row_count = @@ROWCOUNT
commit tran
end
go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE [dbo].[sql_perf_mon_config_snapshot_type]
(
[snapshot_type_id] tinyint NOT NULL PRIMARY KEY,
[snapshot_type_desc] varchar(255) not null,
[snapshot_retention_days] smallint not null,
)
4 changes: 3 additions & 1 deletion SQLWATCHDB/dbo/Tables/sql_perf_mon_file_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
[num_of_bytes_written] bigint not null,
[io_stall_write_ms] bigint not null,
[size_on_disk_bytes] bigint not null,
[snapshot_time] datetime foreign key references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time]) on delete cascade not null,
[snapshot_time] datetime not null,
[snapshot_type_id] tinyint not null default 1 ,
constraint fk_sql_perf_mon_file_stats_snapshot_header foreign key ([snapshot_time],[snapshot_type_id]) references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time],[snapshot_type_id]) on delete cascade ,
constraint pk_sql_perf_mon_file_stats primary key clustered (
[snapshot_time], [database_name], [logical_file_name], [type_desc]
)
Expand Down
4 changes: 3 additions & 1 deletion SQLWATCHDB/dbo/Tables/sql_perf_mon_os_memory_clerks.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE TABLE [dbo].[sql_perf_mon_os_memory_clerks]
(
[snapshot_time] datetime foreign key references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time]) on delete cascade not null,
[snapshot_time] datetime not null,
[total_kb] bigint,
[allocated_kb] bigint,
[total_kb_all_clerks] bigint,
[clerk_name] varchar(255),
[memory_available] int,
[snapshot_type_id] tinyint not null default 1 ,
constraint fk_sql_perf_mon_os_memory_clerks_snapshot_header foreign key ([snapshot_time],[snapshot_type_id]) references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time],[snapshot_type_id]) on delete cascade ,
constraint [pk_sql_perf_mon_os_memory_clerks] primary key (
[snapshot_time], [clerk_name]
)
Expand Down
17 changes: 6 additions & 11 deletions SQLWATCHDB/dbo/Tables/sql_perf_mon_os_process_memory.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@
[available_commit_limit_kb] [bigint] NOT NULL,
[process_physical_memory_low] [bit] NOT NULL,
[process_virtual_memory_low] [bit] NOT NULL,
PRIMARY KEY CLUSTERED
(
[snapshot_time] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[sql_perf_mon_os_process_memory] WITH CHECK ADD CONSTRAINT [fk_sql_perf_mon_os_process_memory] FOREIGN KEY([snapshot_time])
REFERENCES [dbo].[sql_perf_mon_snapshot_header] ([snapshot_time])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[sql_perf_mon_os_process_memory] CHECK CONSTRAINT [fk_sql_perf_mon_os_process_memory]
[snapshot_type_id] tinyint not null default 1 ,
constraint fk_sql_perf_mon_os_process_memory_snapshot_header foreign key ([snapshot_time],[snapshot_type_id]) references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time],[snapshot_type_id]) on delete cascade ,
constraint pk_sql_perf_mon_os_process_memory primary key clustered (
[snapshot_time] ASC
)
)
GO
4 changes: 3 additions & 1 deletion SQLWATCHDB/dbo/Tables/sql_perf_mon_perf_counters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[cntr_value] bigint not null,
[base_cntr_value] bigint null,
[cntr_type] bigint not null,
[snapshot_time] datetime foreign key references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time]) on delete cascade not null,
[snapshot_time] datetime not null,
[snapshot_type_id] tinyint not null default 1 ,
constraint fk_sql_perf_mon_perf_counters_snapshot_header foreign key ([snapshot_time],[snapshot_type_id]) references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time],[snapshot_type_id]) on delete cascade ,
constraint [pk_sql_perf_mon_perf_counters] primary key ([snapshot_time] asc, [object_name] asc, [counter_name] asc, [instance_name] asc)
)
10 changes: 9 additions & 1 deletion SQLWATCHDB/dbo/Tables/sql_perf_mon_snapshot_header.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
CREATE TABLE [dbo].[sql_perf_mon_snapshot_header]
(
[snapshot_time] datetime primary key
[snapshot_time] datetime,
[snapshot_type_id] tinyint not null default 1 foreign key references dbo.[sql_perf_mon_config_snapshot_type]([snapshot_type_id]),
CONSTRAINT pk_snapshot PRIMARY KEY (
[snapshot_time],[snapshot_type_id]
)
)
go

create nonclustered index idx_snapshot_type_id on [dbo].[sql_perf_mon_snapshot_header]([snapshot_type_id])
go
4 changes: 3 additions & 1 deletion SQLWATCHDB/dbo/Tables/sql_perf_mon_wait_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[wait_time_ms] bigint not null,
[max_wait_time_ms] bigint not null,
[signal_wait_time_ms] bigint not null,
[snapshot_time] datetime foreign key references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time]) on delete cascade not null,
[snapshot_time] datetime not null,
[snapshot_type_id] tinyint not null default 1 ,
constraint fk_sql_perf_mon_wait_stats_snapshot_header foreign key ([snapshot_time],[snapshot_type_id]) references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time],[snapshot_type_id]) on delete cascade ,
constraint [pk_sql_perf_mon_wait_stats] primary key (
[snapshot_time] asc, [wait_type] asc
)
Expand Down
8 changes: 7 additions & 1 deletion SQLWATCHDB/dbo/Tables/sql_perf_mon_who_is_active.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
create table [dbo].[sql_perf_mon_who_is_active] ([snapshot_time] datetime foreign key references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time]) on delete cascade not null,[start_time] datetime NOT NULL,[session_id] smallint NOT NULL,[status] varchar(30) NOT NULL,[percent_complete] varchar(30) NULL,[host_name] nvarchar(128) NULL,[database_name] nvarchar(128) NULL,[program_name] nvarchar(128) NULL,[sql_text] xml NULL,[sql_command] xml NULL,[login_name] nvarchar(128) NOT NULL,[open_tran_count] varchar(30) NULL,[wait_info] nvarchar(4000) NULL,[blocking_session_id] smallint NULL,[blocked_session_count] varchar(30) NULL,[CPU] varchar(30) NULL,[used_memory] varchar(30) NULL,[tempdb_current] varchar(30) NULL,[tempdb_allocations] varchar(30) NULL,[reads] varchar(30) NULL,[writes] varchar(30) NULL,[physical_reads] varchar(30) NULL,[login_time] datetime NULL)
create table [dbo].[sql_perf_mon_who_is_active] (
[snapshot_time] datetime not null,
[start_time] datetime NOT NULL,[session_id] smallint NOT NULL,[status] varchar(30) NOT NULL,[percent_complete] varchar(30) NULL,[host_name] nvarchar(128) NULL,[database_name] nvarchar(128) NULL,[program_name] nvarchar(128) NULL,[sql_text] xml NULL,[sql_command] xml NULL,[login_name] nvarchar(128) NOT NULL,[open_tran_count] varchar(30) NULL,[wait_info] nvarchar(4000) NULL,[blocking_session_id] smallint NULL,[blocked_session_count] varchar(30) NULL,[CPU] varchar(30) NULL,[used_memory] varchar(30) NULL,[tempdb_current] varchar(30) NULL,[tempdb_allocations] varchar(30) NULL,[reads] varchar(30) NULL,[writes] varchar(30) NULL,[physical_reads] varchar(30) NULL
,[login_time] datetime NULL,
[snapshot_type_id] tinyint not null default 1 ,
constraint fk_sql_perf_mon_who_is_active_snapshot_header foreign key ([snapshot_time],[snapshot_type_id]) references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time],[snapshot_type_id]) on delete cascade ,
)
go
create nonclustered index idx_whoisactive on [dbo].[sql_perf_mon_who_is_active] ([snapshot_time])

0 comments on commit e9857bd

Please sign in to comment.