Skip to content

Commit

Permalink
Merge pull request #45 from marcingminski/development
Browse files Browse the repository at this point in the history
v1.1.6833.36316 PowerBI Disk utilisation and performance tweaks
  • Loading branch information
marcingminski authored Sep 17, 2018
2 parents b85b0cc + c619223 commit f3f637d
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 1,366 deletions.
Binary file added SQLWATCH Disk Utilisation Dashboard.pbit
Binary file not shown.
4 changes: 2 additions & 2 deletions SQLWATCHDB/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.*")]
[assembly: AssemblyFileVersion("1.1.0.0")]
3 changes: 3 additions & 0 deletions SQLWATCHDB/SQLWATCH.sqlproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<PageVerify>CHECKSUM</PageVerify>
<AllowSnapshotIsolation>True</AllowSnapshotIsolation>
<ReadCommittedSnapshot>True</ReadCommittedSnapshot>
<DacVersion>1.1.0.0</DacVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release\</OutputPath>
Expand Down Expand Up @@ -95,6 +96,8 @@
<Build Include="dbo\Tables\sql_perf_mon_master_files.sql" />
<Build Include="dbo\Tables\logger_disk_utilisation_database.sql" />
<Build Include="dbo\Procedures\usp_logger_disk_utilisation.sql" />
<Build Include="dbo\ufn_time_intervals.sql" />
<Build Include="dbo\Tables\logger_disk_utilisation_volume.sql" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Script.PostDeployment1.sql" />
Expand Down
55 changes: 54 additions & 1 deletion SQLWATCHDB/Script.PostDeployment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ if (select name from sysjobs where name = 'SQLWATCH-LOGGER-DISK-UTILISATION') is
@command=N'exec [dbo].[usp_logger_disk_utilisation]',
@database_name=@database,
@flags=0

EXEC msdb.dbo.sp_update_job @job_name=N'SQLWATCH-LOGGER-DISK-UTILISATION',
@enabled=1,
@start_step_id=1,
Expand All @@ -860,4 +861,56 @@ if (select name from sysjobs where name = 'SQLWATCH-LOGGER-DISK-UTILISATION') is
@active_end_date=99991231,
@active_start_time=437,
@active_end_time=235959, @schedule_id = @schedule_id OUTPUT
end
end

set @jobId = null
declare @command nvarchar(4000)
set @command = N'
[datetime]$snapshot_time = (Invoke-SqlCmd -ServerInstance "' + @server + '" -Database ' + @database + ' -Query "select [snapshot_time]=max([snapshot_time])
from [dbo].[sql_perf_mon_snapshot_header]
where snapshot_type_id = 2").snapshot_time
#https://msdn.microsoft.com/en-us/library/aa394515(v=vs.85).aspx
#driveType 3 = Local disk
Get-WMIObject Win32_Volume | ?{$_.DriveType -eq 3} | %{
$VolumeName = $_.Name
$VolumeLabel = $_.Label
$FileSystem = $_.Filesystem
$BlockSize = $_.BlockSize
$FreeSpace = $_.Freespace
$Capacity = $_.Capacity
$SnapshotTime = Get-Date $snapshot_time -format "yyyy-MM-dd HH:mm:ss.fff"
Invoke-SqlCmd -ServerInstance "' + @server + '" -Database ' + @database + ' -Query "
insert into [dbo].[logger_disk_utilisation_volume](
[volume_name]
,[volume_label]
,[volume_fs]
,[volume_block_size_bytes]
,[volume_free_space_bytes]
,[volume_total_space_bytes]
,[snapshot_type_id]
,[snapshot_time])
values (''$VolumeName'',''$VolumeLabel'',''$FileSystem'',$BlockSize,$FreeSpace,$Capacity,2,''$SnapshotTime'')
"
}'
select @jobId = job_id from msdb.dbo.sysjobs where name = 'SQLWATCH-LOGGER-DISK-UTILISATION'
if (select step_name from msdb.dbo.sysjobsteps
where job_id = @jobId and step_name = 'Get-WMIObject Win32_Volume') is null
begin
EXEC msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Get-WMIObject Win32_Volume',
@step_id=2,
@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'PowerShell',
@command=@command,
@database_name=@database,
@flags=0
EXEC msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
end
EXEC msdb.dbo.sp_update_jobstep @job_id=@jobId, @step_id=1 ,@on_success_action=3

Binary file modified SQLWATCHDB/bin/Debug/SQLWATCH.dacpac
Binary file not shown.
Binary file modified SQLWATCHDB/bin/Debug/SQLWATCHDB.dll
Binary file not shown.
74 changes: 65 additions & 9 deletions SQLWATCHDB/dbo/Procedures/usp_logger_disk_utilisation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare @snapshot_type tinyint = 2
declare @product_version nvarchar(128)
declare @product_version_major decimal(10,2)
declare @product_version_minor decimal(10,2)
declare @sql varchar(max)

set @product_version = convert(nvarchar(128),serverproperty('productversion'));
select @product_version_major = substring(@product_version, 1,charindex('.', @product_version) + 1 )
Expand All @@ -21,16 +22,71 @@ values (@snapshot_time, @snapshot_type)
-- get sp_spaceused
--------------------------------------------------------------------------------------------------------------
declare @spaceused table (
[database_name] nvarchar(128),
[database_size] varchar(18),
[unallocated_space] varchar(18),
[reserved] varchar(18),
[data] varchar(18),
[index_size] varchar(18),
[unused] varchar(18)
[database_name] nvarchar(128),
[database_size] varchar(18),
[unallocated_space] varchar(18),
[reserved] varchar(18),
[data] varchar(18),
[index_size] varchar(18),
[unused] varchar(18)
)
insert into @spaceused
exec sp_MSforeachdb 'use [?]; exec sp_spaceused @oneresultset = 1;'

if @product_version_major >= 13
/* since SQL 2016 Microsoft have improved sp_spaceused which now returns one recordset making it easier
to insert into tables */
begin
insert into @spaceused
exec sp_MSforeachdb 'use [?]; exec sp_spaceused @oneresultset = 1;'
end
else
begin
/* pre 2016 however is not all that easy. sp_spaceused will return multiple resultsets making it impossible
to insert into a table. The below is more or less what sp_spaceused is doing */
insert into @spaceused
exec sp_MSforeachdb 'USE [?];
declare @id int
,@type character(2)
,@pages bigint
,@dbname sysname
,@dbsize bigint
,@logsize bigint
,@reservedpages bigint
,@usedpages bigint
,@rowCount bigint
select
@dbsize = sum(convert(bigint,case when status & 64 = 0 then size else 0 end))
, @logsize = sum(convert(bigint,case when status & 64 <> 0 then size else 0 end))
from dbo.sysfiles
select
@reservedpages = sum(a.total_pages),
@usedpages = sum(a.used_pages),
@pages = sum(
case
-- XML-Index and FT-Index and semantic index internal tables are not considered "data", but is part of "index_size"
when it.internal_type IN (202,204,207,211,212,213,214,215,216,221,222,236) then 0
when a.type <> 1 and p.index_id < 2 then a.used_pages
when p.index_id < 2 then a.data_pages
else 0
end
)
from sys.partitions p join sys.allocation_units a on p.partition_id = a.container_id
left join sys.internal_tables it on p.object_id = it.object_id
select
database_name = db_name(),
database_size = ltrim(str((convert (dec (15,2),@dbsize) + convert (dec (15,2),@logsize))
* 8192 / 1048576,15,2) + '' MB''),
''unallocated space'' = ltrim(str((case when @dbsize >= @reservedpages then
(convert (dec (15,2),@dbsize) - convert (dec (15,2),@reservedpages))
* 8192 / 1048576 else 0 end),15,2) + '' MB''),
reserved = ltrim(str(@reservedpages * 8192 / 1024.,15,0) + '' KB''),
data = ltrim(str(@pages * 8192 / 1024.,15,0) + '' KB''),
index_size = ltrim(str((@usedpages - @pages) * 8192 / 1024.,15,0) + '' KB''),
unused = ltrim(str((@reservedpages - @usedpages) * 8192 / 1024.,15,0) + '' KB'')
'
end

--------------------------------------------------------------------------------------------------------------
-- get log usage
Expand Down
15 changes: 15 additions & 0 deletions SQLWATCHDB/dbo/Tables/logger_disk_utilisation_volume.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE [dbo].[logger_disk_utilisation_volume]
(
[volume_name] nvarchar(255) not null,
[volume_label] nvarchar(255),
[volume_fs] varchar(255),
[volume_block_size_bytes] int,
[volume_free_space_bytes] bigint,
[volume_total_space_bytes] bigint,
[snapshot_time] datetime not null,
[snapshot_type_id] tinyint,
constraint PK_disk_util_vol primary key clustered (
snapshot_time, volume_name
),
constraint FK_disk_util_vol_snapshot_header foreign key ([snapshot_time],[snapshot_type_id]) references [dbo].[sql_perf_mon_snapshot_header]([snapshot_time],[snapshot_type_id]) on delete cascade
)
2 changes: 2 additions & 0 deletions SQLWATCHDB/dbo/Tables/sql_perf_mon_database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
[database_name],[database_create_date]
)
)
GO
CREATE NONCLUSTERED INDEX idx_perf_mon_database_current ON [dbo].[sql_perf_mon_database]([database_current])
38 changes: 38 additions & 0 deletions SQLWATCHDB/dbo/ufn_time_intervals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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
Loading

0 comments on commit f3f637d

Please sign in to comment.