-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4043c9d
commit 8bbb78d
Showing
1 changed file
with
41 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,41 @@ | ||
CREATE FUNCTION [dbo].[ufn_time_intervals] | ||
( | ||
@snapshot_type_id tinyint = null, | ||
@interval_minutes smallint = 15, | ||
@report_window int = 4, | ||
@report_end_time datetime = '2099-12-31' | ||
) | ||
RETURNS TABLE | ||
AS RETURN ( | ||
select | ||
[spapshot_interval_start] | ||
, [snapshot_interval_end] = dateadd(mi, @interval_minutes, [spapshot_interval_start]) | ||
, [first_snapshot_time] = MIN(i.snapshot_time) | ||
, [last_snapshot_time] = MAX(i.snapshot_time) | ||
, [snapshot_age_hours] = datediff(hour,dateadd(mi, @interval_minutes, [spapshot_interval_start]),GETDATE()) | ||
, [report_time_interval_minutes] = @interval_minutes | ||
, s.[snapshot_type_id] | ||
from [dbo].[sql_perf_mon_snapshot_header] s | ||
inner join ( | ||
select | ||
[snapshot_time] | ||
, [spapshot_interval_start] = convert(datetime,dateadd(mi,(datediff(mi,0, [snapshot_time])/ @interval_minutes) * @interval_minutes,0)) | ||
, [report_time_interval_minutes] = @interval_minutes | ||
, [snapshot_type_id] | ||
from [dbo].[sql_perf_mon_snapshot_header] | ||
where snapshot_type_id = isnull(@snapshot_type_id,snapshot_type_id) | ||
and snapshot_time >= DATEADD(HOUR, -@report_window, @report_end_time) | ||
and snapshot_time <= @report_end_time | ||
) i | ||
on s.snapshot_time > [spapshot_interval_start] | ||
and s.snapshot_time <= dateadd(mi, @interval_minutes, [spapshot_interval_start]) | ||
and i.[snapshot_type_id] = s.[snapshot_type_id] | ||
where s.snapshot_type_id = isnull(@snapshot_type_id,s.snapshot_type_id) | ||
and dateadd(mi, @interval_minutes, i.[spapshot_interval_start]) > DATEADD(HOUR, -@report_window, @report_end_time) | ||
and dateadd(mi, @interval_minutes, i.[spapshot_interval_start]) <= @report_end_time | ||
group by [spapshot_interval_start], s.snapshot_type_id | ||
) | ||
GO | ||
CREATE FUNCTION [dbo].[ufn_time_intervals] | ||
( | ||
@snapshot_type_id tinyint = 1, | ||
@interval_minutes smallint = null, | ||
@report_window int = 4, | ||
@report_end_time datetime = null | ||
) | ||
RETURNS TABLE | ||
AS RETURN ( | ||
/* if no @interval_minutes parameter specified we are going to | ||
pick best interval based on report window here */ | ||
with cte_interval_window as ( | ||
select interval_minutes = case when @interval_minutes is null then | ||
case | ||
when @report_window <= 1 then 2 | ||
when @report_window <= 4 then 5 | ||
when @report_window <= 24 then 15 | ||
when @report_window <= 168 then 60 | ||
when @report_window <= 720 then 360 | ||
else 1440 end | ||
else @interval_minutes end | ||
) | ||
select | ||
[first_snapshot_time] = min([snapshot_time]) | ||
, [last_snapshot_time] = max([snapshot_time]) | ||
, [spapshot_interval_start] = convert(datetime,dateadd(mi,(datediff(mi,0, [snapshot_time])/ interval_minutes) * interval_minutes,0)) | ||
, [snapshot_interval_end] = dateadd(mi, interval_minutes, convert(datetime,dateadd(mi,(datediff(mi,0, [snapshot_time])/ interval_minutes) * interval_minutes,0))) | ||
, [report_time_interval_minutes] = interval_minutes | ||
, [snapshot_type_id] | ||
, [snapshot_age_hours] = datediff(hour,dateadd(mi, interval_minutes, convert(datetime,dateadd(mi,(datediff(mi,0, [snapshot_time])/ interval_minutes) * interval_minutes,0))),getdate()) | ||
from [dbo].[sql_perf_mon_snapshot_header] | ||
cross apply cte_interval_window | ||
where snapshot_type_id = isnull(@snapshot_type_id,snapshot_type_id) | ||
and snapshot_time >= DATEADD(HOUR, -@report_window, isnull(@report_end_time,getdate())) | ||
and snapshot_time <= isnull(@report_end_time,getdate()) | ||
group by | ||
convert(datetime,dateadd(mi,(datediff(mi,0, [snapshot_time])/ interval_minutes) * interval_minutes,0)) | ||
, dateadd(mi, interval_minutes, convert(datetime,dateadd(mi,(datediff(mi,0, [snapshot_time])/ interval_minutes) * interval_minutes,0))) | ||
, interval_minutes | ||
, [snapshot_type_id] | ||
) |