Skip to content

Commit

Permalink
Merge pull request #224 from VladDBA/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
VladDBA authored Jun 6, 2024
2 parents abe4b79 + c232565 commit a45d43d
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 47 deletions.
17 changes: 13 additions & 4 deletions PSBlitz.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ param(

###Internal params
#Version
$Vers = "4.1.1"
$VersDate = "2024-06-04"
$Vers = "4.1.2"
$VersDate = "2024-06-05"
$TwoMonthsFromRelease = [datetime]::ParseExact("$VersDate", 'yyyy-MM-dd', $null).AddMonths(2)
$NowDate = Get-Date
#Get script path
Expand Down Expand Up @@ -1374,6 +1374,12 @@ if ($ToHTML -eq "Y") {
background-color: rgba(255, 255, 255, 0.7);
}
}
.IndexUsageTable{
td:nth-child(6) {
position: sticky; left: 0;
background-color: rgba(255, 255, 255, 0.7);
}
}
h1 {
text-align: center;
}
Expand Down Expand Up @@ -4798,6 +4804,8 @@ ELSE IF ( (SELECT PARSENAME(CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSI
@{Name = "Create Date"; Expression = { if ($_."Create Date" -ne [System.DBNull]::Value) { ($_."Create Date").ToString("yyyy-MM-dd HH:mm:ss") }else { $_."Create Date" } } },
@{Name = "Modify Date"; Expression = { if ($_."Modify Date" -ne [System.DBNull]::Value) { ($_."Modify Date").ToString("yyyy-MM-dd HH:mm:ss") }else { $_."Modify Date" } } },
"More Info" | ConvertTo-Html -As Table -Fragment
#add table specify style
$htmlTable = $htmlTable -replace '<table>', '<table class="IndexUsageTable">'
}

$html = $HTMLPre + @"
Expand Down Expand Up @@ -5653,7 +5661,8 @@ ELSE IF ( (SELECT PARSENAME(CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSI
@{Name = "Object Name"; Expression = { $_."object_name" } },
@{Name = "Object Type"; Expression = { $_."object_type" } },
@{Name = "Index Name"; Expression = { $_."index_name" } },
@{Name = "Index Type"; Expression = { $_."index_type" } },
@{Name = "Index Type"; Expression = { $_."index_type" } },
@{Name = "Partition"; Expression = {$_."partition_number"}},
@{Name = "Avg. Frag. %"; Expression = { $_."avg_frag_percent" } },
@{Name = "Page Count"; Expression = { $_."page_count" } },
@{Name = "Size in GB"; Expression = { $_."size_in_GB" } },
Expand Down Expand Up @@ -5694,7 +5703,7 @@ ELSE IF ( (SELECT PARSENAME(CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSI
$ExcelColNum = 1
$RowNum = 0
$DataSetCols = @("database", "object_name", "object_type", "index_name",
"index_type", "avg_frag_percent", "page_count", "size_in_GB", "record_count","forwarded_record_count")
"index_type", "partition_number", "avg_frag_percent", "page_count", "size_in_GB", "record_count","forwarded_record_count")

if ($DebugInfo) {
Write-Host " ->Writing Stats results to sheet Index Fragmentation" -fore yellow
Expand Down
99 changes: 57 additions & 42 deletions Resources/GetIndexInfoForWholeDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,62 @@ SET NOCOUNT ON;
SET STATISTICS XML OFF;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

SELECT DB_NAME() AS [database],
SCHEMA_NAME([obj].[schema_id]) + '.'
+ [obj].[name] AS [object_name],
[obj].[type_desc] AS [object_type],
ISNULL([ix].[name], '') AS [index_name],
[ips].[index_type_desc] AS [index_type],
SUM(CAST([ips].[avg_fragmentation_in_percent] AS DECIMAL(5, 2))) AS [avg_frag_percent],
SUM([ips].[page_count]) AS [page_count],
CAST(( CAST(SUM([ips].[page_count]) AS BIGINT) * 8 ) / 1024.00 / 1024.00 AS NUMERIC(20, 2)) AS [size_in_GB],
SUM(CASE WHEN [ips].[alloc_unit_type_desc] = N'IN_ROW_DATA' THEN CAST([ips].[record_count] AS BIGINT) ELSE 0 END) AS [record_count],
SUM([ips].[forwarded_record_count]) AS [forwarded_record_count]
FROM [sys].[indexes] AS [ix]
INNER JOIN [sys].[objects] AS [obj]
ON [ix].[object_id] = [obj].[object_id]
CROSS APPLY [sys].[dm_db_index_physical_stats](DB_ID(), [obj].[object_id], [ix].[index_id], NULL, 'SAMPLED') AS [ips]
WHERE [ix].[type] IN(0, 1,2,3,4,5,6,7)
AND [obj].[name] <> N'BlitzWho_AzureSQLDBReplace'
AND [obj].[type] in ('U', 'V')
AND [ips].[avg_fragmentation_in_percent] > 0
/*only tables larger than ~400MB */
AND [ips].[page_count] >= 52000
AND obj.object_id not in
(SELECT l.resource_associated_entity_id
FROM sys.dm_tran_locks l
WHERE l.request_mode = N'X'
AND l.request_type = N'LOCK'
AND l.resource_type = N'OBJECT'
AND l.resource_database_id = DB_ID()
GROUP BY l.resource_database_id,
l.resource_associated_entity_id)
GROUP BY SCHEMA_NAME([obj].[schema_id]) + '.'
+ [obj].[name], [obj].[type_desc], [ix].[name] , [ips].[index_type_desc]
ORDER BY SUM(CAST([ips].[avg_fragmentation_in_percent] AS DECIMAL(5, 2))) DESC;
IF OBJECT_ID('tempdb.dbo.#test', 'U') IS NOT NULL
DROP TABLE #test;
SELECT [l].[resource_associated_entity_id]
INTO #test
FROM sys.[dm_tran_locks] [l]
WHERE [l].[request_mode] = N'X'
AND [l].[request_type] = N'LOCK'
AND [l].[resource_type] = N'OBJECT'
AND [l].[resource_database_id] = DB_ID()
GROUP BY [l].[resource_database_id],
[l].[resource_associated_entity_id];


SELECT 'Exclusive Lock' AS [xlocked],
OBJECT_NAME(l.resource_associated_entity_id) AS [object_name]
FROM sys.dm_tran_locks l
WHERE l.request_mode = N'X'
AND l.request_type = N'LOCK'
AND l.resource_type = N'OBJECT'
AND l.resource_database_id = DB_ID()
GROUP BY l.resource_database_id,
l.resource_associated_entity_id;
SELECT DB_NAME() AS [database],
SCHEMA_NAME([obj].[schema_id]) + '.'
+ [obj].[name] AS [object_name],
[obj].[type_desc] AS [object_type],
ISNULL([ix].[name], '') AS [index_name],
[ips].[index_type_desc] AS [index_type],
[ips].[partition_number],
CAST([ips].[avg_fragmentation_in_percent] AS DECIMAL(5, 2)) AS [avg_frag_percent],
[ips].[page_count] AS [page_count],
CAST(( CAST([ips].[page_count] AS BIGINT) * 8 ) / 1024.00 / 1024.00 AS NUMERIC(20, 2)) AS [size_in_GB],
SUM(CASE
WHEN [ips].[alloc_unit_type_desc] = N'IN_ROW_DATA' THEN CAST([ips].[record_count] AS BIGINT)
ELSE 0
END) AS [record_count],
[ips].[forwarded_record_count] AS [forwarded_record_count]
FROM [sys].[indexes] AS [ix]
INNER JOIN [sys].[objects] AS [obj]
ON [ix].[object_id] = [obj].[object_id]
CROSS APPLY [sys].[dm_db_index_physical_stats](DB_ID(), [obj].[object_id], [ix].[index_id], NULL, 'SAMPLED') AS [ips]
WHERE [ix].[type] IN( 0, 1, 2, 3,
4, 5, 6, 7 )
AND [ix].[is_disabled] = 0
AND [obj].[name] <> N'BlitzWho_AzureSQLDBReplace'
AND [obj].[type] IN ( 'U', 'V' )
/*AND [ips].[avg_fragmentation_in_percent] > 0*/
/*only tables larger than ~400MB */
AND [ips].[page_count] >= 52000
AND [obj].[object_id] NOT IN (SELECT [resource_associated_entity_id]
FROM #test)
GROUP BY SCHEMA_NAME([obj].[schema_id]) + '.'
+ [obj].[name],
[obj].[type_desc],
[ix].[name],
[ips].[index_type_desc],
[ips].[partition_number],
[ips].[avg_fragmentation_in_percent],
[ips].[page_count],
[ips].[forwarded_record_count]
ORDER BY [ips].[avg_fragmentation_in_percent] DESC;

SELECT 'Exclusive Lock' AS [xlocked],
OBJECT_NAME([resource_associated_entity_id]) AS [object_name]
FROM #test;

IF OBJECT_ID('tempdb.dbo.#test', 'U') IS NOT NULL
DROP TABLE #test;
2 changes: 1 addition & 1 deletion Resources/GetStatsInfoForWholeDB.sql
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ SET [update_table_stats] = CASE
ELSE NULL
END

WHERE [id] IN(SELECT MAX([id])
WHERE [id] IN(SELECT MIN([id])
FROM ##PSBlitzStatsInfo
GROUP BY [object_name]);
UPDATE ##PSBlitzStatsInfo SET [update_individual_stats] = CASE
Expand Down
Binary file modified Resources/PSBlitzOutput.xlsx
Binary file not shown.

0 comments on commit a45d43d

Please sign in to comment.