Skip to content

Commit b3805a6

Browse files
committed
Update Grafana scripts to incorporate Inline TableValuedFunctions from Scaler functions
Update Grafana scripts to incorporate Inline TableValuedFunctions from Scaler functions
1 parent 6721dc1 commit b3805a6

19 files changed

+4448
-2640
lines changed
22.5 KB
Binary file not shown.

Baselining/Grafana-Monitoring - Live-1599299266939.json renamed to Baselining/Grafana - Monitoring - Live - 2020-Sep-20 - External Share.json

Lines changed: 20 additions & 20 deletions
Large diffs are not rendered by default.

Baselining/Grafana - Monitoring - Live - 2020-Sep-20 - Internal Share.json

Lines changed: 1666 additions & 0 deletions
Large diffs are not rendered by default.

Baselining/Grafana-Dashboard-SQLDBATools-1597626189988.json

Lines changed: 0 additions & 1193 deletions
This file was deleted.

Baselining/SQLDBATools-1597572564461.json

Lines changed: 0 additions & 1062 deletions
This file was deleted.

OlaHallengren.com/OlaHallengren.com.ssmssqlproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@
110110
<FullPath>Index-Defrag-UpdateStats-Analysis.sql</FullPath>
111111
</FileNode>
112112
<FileNode Name="Maintainence - Delete files older than 72 hours.sql">
113-
<AssociatedConnectionMoniker />
114-
<AssociatedConnSrvName />
113+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True</AssociatedConnectionMoniker>
114+
<AssociatedConnSrvName>(local)</AssociatedConnSrvName>
115115
<AssociatedConnUserName />
116116
<FullPath>Maintainence - Delete files older than 72 hours.sql</FullPath>
117117
</FileNode>

SQLDBATools-Inventory/(dba) Collect Metrics.sql

Lines changed: 1654 additions & 299 deletions
Large diffs are not rendered by default.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
set nocount on;
2+
set xact_abort on;
3+
4+
-- Set config variables
5+
declare @retention_days int = 15;
6+
declare @time_interval_minutes tinyint = 10;
7+
8+
-- declare local variables
9+
declare @r int = 1;
10+
declare @date_filter date;
11+
set @date_filter = CONVERT(date,DATEADD(day,-@retention_days,GETDATE()));
12+
13+
-- create table for storing deleted data for aggregation
14+
if OBJECT_ID('tempdb..#dm_os_performance_counters_nonsql_aggregated') is not null
15+
drop table #dm_os_performance_counters_nonsql_aggregated;
16+
CREATE TABLE #dm_os_performance_counters_nonsql_aggregated
17+
(
18+
[collection_time] [datetime2] NOT NULL,
19+
[server_name] [varchar](256) NOT NULL,
20+
[object_name] [nvarchar](128) NOT NULL,
21+
[counter_name] [nvarchar](128) NOT NULL,
22+
[instance_name] [nvarchar](128) NULL,
23+
[cntr_value] [bigint] NOT NULL,
24+
[cntr_type] [int] NOT NULL,
25+
[id] [smallint] NOT NULL
26+
);
27+
28+
while @r > 0
29+
begin
30+
truncate table #dm_os_performance_counters_nonsql_aggregated;
31+
32+
delete top (10000) [dbo].[dm_os_performance_counters_nonsql]
33+
output deleted.*
34+
into #dm_os_performance_counters_nonsql_aggregated
35+
where collection_time < @date_filter;
36+
set @r = @@ROWCOUNT;
37+
38+
insert [dbo].[dm_os_performance_counters_nonsql_aggregated]
39+
select [collection_time] = convert(smalldatetime,dbo.aggregate_time(collection_time,@time_interval_minutes)),
40+
server_name, [object_name], counter_name, instance_name,
41+
cntr_value = AVG(cntr_value),
42+
cntr_type,
43+
id = ROW_NUMBER()OVER(ORDER BY GETDATE())
44+
from #dm_os_performance_counters_nonsql_aggregated as omc
45+
group by convert(smalldatetime,dbo.aggregate_time(collection_time,@time_interval_minutes)),
46+
server_name, [object_name], counter_name, instance_name, cntr_type
47+
48+
print cast(@@ROWCOUNT as varchar)+ ' rows inserted into aggregated table';
49+
end
50+
go
51+
52+
-- select * from [dm_os_performance_counters_nonsql_aggregated]

SQLDBATools-Inventory/SCH-Create-Function-utc2local.sql

Lines changed: 223 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,258 @@
11
use DBA
22
go
33

4-
alter function dbo.utc2local (@utc_datetime datetime2)
4+
-- drop function dbo.utc2local
5+
-- Deterministic. CI Index Seek doing Seek Predicate for all Keys. Parallelism
6+
declare @tsql nvarchar(max);
7+
declare @utc_2_local_diff_ms bigint = Datediff(MILLISECOND, SYSUTCDATETIME(), SYSDATETIME());
8+
9+
set @tsql = '
10+
create or alter function dbo.utc2local (@utc_datetime datetime2)
11+
returns table with schemabinding
12+
as
13+
return
14+
(
15+
select [local_time] = Dateadd(MILLISECOND, '+cast(@utc_2_local_diff_ms as varchar)+', @utc_datetime)
16+
)
17+
'
18+
--print @tsql
19+
EXEC (@tsql)
20+
go
21+
22+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.utc2local'), 'IsDeterministic')
23+
go
24+
25+
-- drop function dbo.local2utc
26+
-- Deterministic. CI Index Seek doing Seek Predicate for all Keys. Parallelism
27+
declare @tsql nvarchar(max);
28+
declare @local_2_utc_diff_ms bigint = Datediff(MILLISECOND, SYSDATETIME(), SYSUTCDATETIME());
29+
30+
set @tsql = '
31+
create or alter function dbo.local2utc (@local_datetime datetime2)
32+
returns table with schemabinding
33+
as
34+
return
35+
(
36+
select [utc_time] = DATEADD(MILLISECOND, '+cast(@local_2_utc_diff_ms as varchar)+', @local_datetime)
37+
)
38+
'
39+
--print @tsql
40+
EXEC (@tsql)
41+
go
42+
43+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.local2utc'), 'IsDeterministic')
44+
go
45+
46+
47+
-- drop function dbo.perfmon2utc
48+
-- Deterministic. CI Index Seek doing Seek Predicate for all Keys. Parallelism
49+
declare @tsql nvarchar(max);
50+
declare @local_2_utc_diff_s bigint = Datediff(second, SYSDATETIME(), SYSUTCDATETIME());
51+
52+
set @tsql = '
53+
create or alter function dbo.perfmon2utc (@CounterDateTime varchar(24))
54+
returns table with schemabinding
55+
as
56+
return
57+
(
58+
--select [utc_time] = DATEADD(second, '+cast(@local_2_utc_diff_s as varchar)+', @local_datetime)
59+
select [utc_time] = DATEADD(second, '+cast(@local_2_utc_diff_s as varchar)+', CONVERT(DATETIME, SUBSTRING(@CounterDateTime, 1, 23), 102))
60+
)
61+
'
62+
--print @tsql
63+
EXEC (@tsql)
64+
go
65+
66+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.perfmon2utc'), 'IsDeterministic')
67+
go
68+
69+
-- drop function dbo.perfmon2local
70+
create or alter function dbo.perfmon2local (@CounterDateTime varchar(24))
71+
returns table with schemabinding
72+
as
73+
return
74+
(
75+
--select [local_time] = Cast(Cast(@CounterDateTime as CHAR(23)) as datetime2)
76+
--select [local_time] = convert(datetime2, convert(CHAR(23), @CounterDateTime))
77+
select [local_time] = CONVERT(DATETIME, SUBSTRING(@CounterDateTime, 1, 23), 102)
78+
)
79+
go
80+
81+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.perfmon2local'), 'IsDeterministic')
82+
go
83+
84+
create or alter function dbo.aggregate_time (@collection_time datetime2, @time_interval_minutes smallint = 10)
585
returns datetime2 with schemabinding
686
as
787
begin
8-
declare @local_time datetime2;
9-
select @local_time = Dateadd(MILLISECOND, Datediff(MILLISECOND, Getutcdate(), Getdate()), @utc_datetime);
88+
declare @aggreate_time datetime2;
89+
select @aggreate_time = CONVERT(varchar(10),@collection_time,120) +' '+ RIGHT('0' + CAST(DATEPART(HOUR,@collection_time) AS Varchar(2)), 2) + ':' + RIGHT('0' + CAST(DATEPART(MINUTE,@collection_time)/@time_interval_minutes*@time_interval_minutes AS varchar(2)), 2)+ ':00';
1090

11-
return (@local_time);
91+
return (@aggreate_time);
1292
end
1393
go
1494

15-
alter function dbo.local2utc (@local_datetime datetime2)
95+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.aggregate_time'), 'IsDeterministic')
96+
go
97+
98+
create or alter function dbo.aggregate_time_dummy (@collection_time datetime2, @time_interval_minutes smallint = 10)
99+
returns table with schemabinding
100+
as
101+
return
102+
(
103+
select [aggreate_time] = CONVERT(varchar(10),@collection_time,120) +' '+ RIGHT('0' + CAST(DATEPART(HOUR,@collection_time) AS Varchar(2)), 2) + ':' + RIGHT('0' + CAST(DATEPART(MINUTE,@collection_time)/@time_interval_minutes*@time_interval_minutes AS varchar(2)), 2)+ ':00'
104+
)
105+
go
106+
107+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.aggregate_time_dummy'), 'IsDeterministic')
108+
go
109+
110+
-- drop function dbo.time2duration
111+
create or alter function dbo.time2duration (@time varchar(27), @unit varchar(20) = 'second')
112+
returns table with schemabinding
113+
as
114+
return
115+
(
116+
select [duration] =
117+
case when @unit in ('datetime','datetime2','smalldatetime')
118+
then Concat
119+
(
120+
RIGHT('00'+CAST(ISNULL((datediff(second,@time,GETDATE()) / 3600 / 24), 0) AS VARCHAR(2)),2)
121+
,' '
122+
,RIGHT('00'+CAST(ISNULL(datediff(second,@time,GETDATE()) / 3600 % 24, 0) AS VARCHAR(2)),2)
123+
,':'
124+
,RIGHT('00'+CAST(ISNULL(datediff(second,@time,GETDATE()) / 60 % 60, 0) AS VARCHAR(2)),2)
125+
,':'
126+
,RIGHT('00'+CAST(ISNULL(datediff(second,@time,GETDATE()) % 3600 % 60, 0) AS VARCHAR(2)),2)
127+
)
128+
when @unit in ('second','ss','s')
129+
then Concat
130+
(
131+
RIGHT('00'+CAST(ISNULL((@time / 3600 / 24), 0) AS VARCHAR(2)),2)
132+
,' '
133+
,RIGHT('00'+CAST(ISNULL(@time / 3600 % 24, 0) AS VARCHAR(2)),2)
134+
,':'
135+
,RIGHT('00'+CAST(ISNULL(@time / 60 % 60, 0) AS VARCHAR(2)),2)
136+
,':'
137+
,RIGHT('00'+CAST(ISNULL(@time % 3600 % 60, 0) AS VARCHAR(2)),2)
138+
)
139+
else null
140+
end
141+
)
142+
go
143+
144+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.aggregate_time_dummy'), 'IsDeterministic')
145+
go
146+
147+
148+
149+
--select /* Convert 5428424 seconds into [DD hh:mm:ss] */ master.dbo.time2duration(5428424,'s');
150+
151+
/*
152+
153+
use DBA
154+
go
155+
156+
-- Version 01 - Scaler Function without schemabinding. CI index Seek doing RANGE SCAN
157+
create function dbo.utc2local (@utc_datetime datetime2)
158+
returns datetime2
159+
as
160+
begin
161+
declare @local_time datetime2;
162+
select @local_time = Dateadd(MILLISECOND, Datediff(MILLISECOND, Getutcdate(), Getdate()), @utc_datetime);
163+
164+
return (@local_time);
165+
end
166+
go
167+
168+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.utc2local'), 'IsDeterministic')
169+
go
170+
171+
172+
-- Version 02 - Scaler Function with Schemabinding. CI index Seek doing RANGE SCAN
173+
create function dbo.utc2local (@utc_datetime datetime2)
16174
returns datetime2 with schemabinding
17175
as
18176
begin
19-
declare @utc_time datetime2;
20-
select @utc_time = DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), @local_datetime);
177+
declare @local_time datetime2;
178+
select @local_time = Dateadd(MILLISECOND, Datediff(MILLISECOND, Getutcdate(), Getdate()), @utc_datetime);
179+
180+
return (@local_time);
181+
end
182+
go
21183
22-
return (@utc_time);
184+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.utc2local'), 'IsDeterministic')
185+
go
186+
187+
188+
-- Version 03 - Deterministic. CI index Seek doing Seek Predicate for all keys. But No Parallelism
189+
declare @tsql nvarchar(max);
190+
declare @utc_2_local_diff_ms bigint = Datediff(MILLISECOND, SYSUTCDATETIME(), SYSDATETIME());
191+
192+
set @tsql = '
193+
create or alter function dbo.utc2local (@utc_datetime datetime2)
194+
returns datetime2 with schemabinding
195+
as
196+
begin
197+
declare @local_time datetime2;
198+
select @local_time = Dateadd(MILLISECOND, '+cast(@utc_2_local_diff_ms as varchar)+', @utc_datetime);
199+
200+
return (@local_time);
23201
end
202+
'
203+
--print @tsql
204+
EXEC (@tsql)
205+
go
206+
207+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.utc2local'), 'IsDeterministic')
24208
go
25209
26-
alter function dbo.perfmon2utc (@CounterDateTime varchar(24))
210+
211+
-- drop function dbo.utc2local
212+
-- Version 04 - Deterministic. CI Index Seek doing Seek Predicate for all Keys. Parallelism
213+
declare @tsql nvarchar(max);
214+
declare @utc_2_local_diff_ms bigint = Datediff(MILLISECOND, SYSUTCDATETIME(), SYSDATETIME());
215+
216+
set @tsql = '
217+
create or alter function dbo.utc2local (@utc_datetime datetime2)
218+
returns table with schemabinding
219+
as
220+
return
221+
(
222+
select [local_time] = Dateadd(MILLISECOND, '+cast(@utc_2_local_diff_ms as varchar)+', @utc_datetime)
223+
)
224+
'
225+
--print @tsql
226+
EXEC (@tsql)
227+
go
228+
229+
SELECT OBJECTPROPERTY(OBJECT_ID('dbo.utc2local'), 'IsDeterministic')
230+
go
231+
232+
create or alter function dbo.perfmon2utc (@CounterDateTime varchar(24))
27233
returns datetime2 with schemabinding
28234
as
29235
begin
30236
declare @utc_time datetime2;
31-
select @utc_time = DATEADD(second, DATEDIFF(second, GETDATE(), GETUTCDATE()), CONVERT(DATETIME, SUBSTRING(@CounterDateTime, 1, 23), 102));
237+
select @utc_time = DATEADD(second, DATEDIFF(second, SYSDATETIME(), GETUTCDATE()), CONVERT(DATETIME, SUBSTRING(@CounterDateTime, 1, 23), 102));
32238
33239
return (@utc_time);
34240
end
35241
go
36242
37-
alter function dbo.perfmon2local (@CounterDateTime varchar(24))
243+
create or alter function dbo.aggregate_time (@collection_time datetime2, @time_interval_minutes smallint = 10)
38244
returns datetime2 with schemabinding
39245
as
40246
begin
41-
declare @local_time datetime2;
42-
select @local_time = Cast(Cast(@CounterDateTime as CHAR(23)) as datetime2);
247+
declare @aggreate_time datetime2;
248+
select @aggreate_time = CONVERT(varchar(10),@collection_time,120) +' '+ RIGHT('0' + CAST(DATEPART(HOUR,@collection_time) AS Varchar(2)), 2) + ':' + RIGHT('0' + CAST(DATEPART(MINUTE,@collection_time)/@time_interval_minutes*@time_interval_minutes AS varchar(2)), 2)+ ':00';
43249
44-
return (@local_time);
250+
return (@aggreate_time);
45251
end
46252
go
47253
48-
alter function dbo.time2duration (@time varchar(27), @unit varchar(20) = 'second')
254+
255+
create or alter function dbo.time2duration (@time varchar(27), @unit varchar(20) = 'second')
49256
returns varchar(30) with schemabinding
50257
as
51258
begin
@@ -85,5 +292,4 @@ begin
85292
end
86293
go
87294
88-
89-
--select /* Convert 5428424 seconds into [DD hh:mm:ss] */ master.dbo.time2duration(5428424,'s');
295+
*/

0 commit comments

Comments
 (0)