Skip to content

Commit

Permalink
more changes for #279, #280
Browse files Browse the repository at this point in the history
  • Loading branch information
VladDBA committed Nov 17, 2024
1 parent 2c94a07 commit 1085011
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 40 deletions.
72 changes: 34 additions & 38 deletions PSBlitz.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ if (($IsAzure -eq $false) -and ([string]::IsNullOrEmpty($ASDBName)) -and ($IsAzu
$SqlConnection.ConnectionString = $ConnString

[int]$CmdTimeout = 100
Write-Host "Detecting type of environment... " -NoNewLine
Write-Host "Detecting environment type... " -NoNewLine
$AzCheckQuery = new-object System.Data.SqlClient.SqlCommand
$Query = "SELECT CAST(SERVERPROPERTY('EngineEdition') AS INT) AS [EngineEdition],"
$Query += "`nCAST(SERVERPROPERTY('Edition') AS NVARCHAR(128)) AS [Edition];"
Expand Down Expand Up @@ -1039,18 +1039,18 @@ if (($IsAzure -eq $false) -and ([string]::IsNullOrEmpty($ASDBName)) -and ($IsAzu
if ($Edition -eq "SQL Azure") {
if ($EngineEdition -eq 8) {
$IsAzureSQLMI = $true
Write-Host " ->Azure SQL MI"
Write-Host "->Azure SQL MI"
}
elseif ($EngineEdition -eq 5) {
$IsAzureSQLDB = $true
Write-Host " ->Azure SQL DB"
Write-Host "->Azure SQL DB"
}
}
elseif ($EngineEdition -in 2, 3, 4) {
Write-Host " ->SQL Server $Edition"
Write-Host "->SQL Server $Edition"
}
else {
Write-Host " ->Well this is awquard, use the following info to debug:"
Write-Host "->Well this is awquard, use the following info to debug:"
Write-Host " Edition - $Edition; EngineEdition - $EngineEdition"
}
}
Expand Down Expand Up @@ -1090,7 +1090,7 @@ if (!([string]::IsNullOrEmpty($SQLLogin))) {
else {
$ConnString = "Server=$ServerName;Database=master;User Id=$SQLLogin;Password=$SQLPass;Connection Timeout=$ConnTimeout;Application Name=$AppName"
}
$Auth="SQL"
$Auth = "SQL"
}
else {
if ($IsAzureSQLDB) {
Expand All @@ -1099,7 +1099,7 @@ else {
else {
$ConnString = "Server=$ServerName;Database=master;trusted_connection=true;Connection Timeout=$ConnTimeout;Application Name=$AppName"
}
$Auth="Trusted"
$Auth = "Trusted"
}
$SqlConnection.ConnectionString = $ConnString

Expand Down Expand Up @@ -1210,7 +1210,8 @@ if (!([string]::IsNullOrEmpty($CheckDB))) {
Remove-Variable -Name CheckDBAdapter
Remove-Variable -Name CheckDBQuery

}elseif($IsAzureSQLDB -eq $false){
}
elseif ($IsAzureSQLDB -eq $false) {
#if we're not in Azure SQL DB mode and no database was provided, get a user database count
Write-Host "Checking user database count..."
$CheckDBQuery = new-object System.Data.SqlClient.SqlCommand
Expand All @@ -1225,28 +1226,18 @@ if (!([string]::IsNullOrEmpty($CheckDB))) {
$CheckDBAdapter.Fill($CheckDBSet) | Out-Null
$SqlConnection.Close()
[int]$UsrDBCount = $CheckDBSet.Tables[0].Rows[0]["DBCount"]
if($UsrDBCount -ge $MaxUsrDBs) {
if ($UsrDBCount -ge $MaxUsrDBs) {
Write-Host "->Instance has $UsrDBCount user databases" -Fore Yellow
$DbSpecific = Read-Host -Prompt "Switch to database-specific plan cache, index, and deadlock check?[Y/N]"
if($DbSpecific -eq "Y"){
while ([string]::IsNullOrEmpty($CheckDB)) {
$CheckDB = Read-Host -Prompt " Name of the database to check"
}

Write-Host "->The following checks will be limited to the database that shows up the most in the cache results:"
if ($IsIndepth -eq "Y") {
Write-Host " - Index Summary"
Write-Host " - Index Usage Details"
Write-Host " - Detailed Index Diagnosis"
}
else {
Write-Host "Continuing with an instance-wide check..."
Write-Host "->The following checks will be limited to the database that shows up the most in the plan cache results:"
if($IsIndepth -eq "Y"){
Write-Host " - Index Summary"
Write-Host " - Index Usage Details"
Write-Host " - Detailed Index Diagnosis"
} else {
Write-Host " - Index Diagnosis"
}
}
}

}
}
}

###Create directories
Expand Down Expand Up @@ -2979,7 +2970,7 @@ $JumpToTop
$htmlTable = $DBInfoTbl | Select-Object "Database", @{Name = "Created"; Expression = { if ($_."Created" -ne [System.DBNull]::Value) { ($_."Created").ToString("yyyy-MM-dd HH:mm:ss") }else { $_."Created" } } },
"DatabaseState", "UserAccess", "DataFiles", "DataFilesSizeGB", "LogFiles",
"LogFilesSizeGB", "VirtualLogFiles", "FILESTREAMContainers", "FSContainersSizeGB",
"DatabaseSizeGB", "CurrentLogReuseWait", "CompatibilityLevel", "PageVerifyOption", "Containment", "Collation",
"DatabaseSizeGB", "CachedSizeMB", "BufferPool%", "CurrentLogReuseWait", "CompatibilityLevel", "PageVerifyOption", "Containment", "Collation",
"SnapshotIsolationState", "ReadCommittedSnapshotOn", "RecoveryModel", "AutoCloseOn",
"AutoShrinkOn", "QueryStoreOn", "TrustworthyOn", "IsEncrypted", "EncryptionState" | ConvertTo-Html -As Table -Fragment

Expand Down Expand Up @@ -3083,7 +3074,7 @@ $htmlBlock
#List of columns that should be returned from the data set
$DataSetCols = @("Database", "Created", "DatabaseState", "UserAccess", "DataFiles", "DataFilesSizeGB", "LogFiles",
"LogFilesSizeGB", "VirtualLogFiles", "FILESTREAMContainers", "FSContainersSizeGB",
"DatabaseSizeGB", "CurrentLogReuseWait", "CompatibilityLevel", "PageVerifyOption", "Containment", "Collation", "SnapshotIsolationState",
"DatabaseSizeGB", "CachedSizeMB", "BufferPool%", "CurrentLogReuseWait", "CompatibilityLevel", "PageVerifyOption", "Containment", "Collation", "SnapshotIsolationState",
"ReadCommittedSnapshotOn", "RecoveryModel", "AutoCloseOn",
"AutoShrinkOn", "QueryStoreOn", "TrustworthyOn", "IsEncrypted", "EncryptionState")
if ($DebugInfo) {
Expand Down Expand Up @@ -3862,9 +3853,10 @@ $JumpToTop
$PreviousOutcome = $StepOutcome
$StepOutcome = "Success"
$RecordsReturned = $BlitzCacheSet.Tables[0].Rows.Count
if($OrigCacheMinutesBack -ne 0){
if ($OrigCacheMinutesBack -ne 0) {
$AdditionalInfo = ", MinutesBack=$CacheMinutesBack"
} else {
}
else {
$AdditionalInfo = ""
}
Add-LogRow "sp_BlitzCache $SortOrder $AdditionalInfo" $StepOutcome "$RecordsReturned records returned"
Expand Down Expand Up @@ -4799,16 +4791,18 @@ ELSE IF ( (SELECT PARSENAME(CONVERT(NVARCHAR(128), SERVERPROPERTY ('PRODUCTVERSI
elseif ($IsAzureSQLDB) {
[string]$Query = $Query -replace ";SET @GetAllDatabases = 1;", ";SET @GetAllDatabases = 0;"
Write-Host " Retrieving index info for $ASDBName"
} elseif($UsrDBCount -ge $MaxUsrDBs) {
}
elseif ($UsrDBCount -ge $MaxUsrDBs) {
#If the number of user databases >= MaxUsrDBs
#set the database to the one that accounts for the most records in the plan cache
$TopDBinCache = $DBArray | Group-Object | Sort-Object Count -Descending | Select-Object -First 1
Write-Host " You're trying to get index info on an instance with $UsrDBCount databases." -ForegroundColor Yellow
Write-Host " Doing so an instance with $MaxUsrDBs+ may cause temporary problems for the server and/or PSBlitz" -ForegroundColor Yellow
Write-Host " Limiting index info to $($TopDBinCache.Name) which accounts for $($TopDBinCache.Count) records returned from cache"
Write-Host " Retrieving index info for $($TopDBinCache.Name)"
[string]$Query = $Query -replace $OldCheckDBStr, ";SET @DatabaseName = '$($TopDBinCache.Name)';"
Add-LogRow "sp_BlitzIndex" "User database count>= $MaxUsrDBs" "Limiting index info to $TopDBinCache.Name which accounts for $($TopDBinCache.Count) records in the plan cache results"
$TopDBinCache = $DBArray | Group-Object | Sort-Object Count -Descending | Select-Object -First 1
Write-Host " You're trying to get index info on an instance with $UsrDBCount databases." -ForegroundColor Yellow
Write-Host " Doing so an instance may cause temporary problems for the server and/or PSBlitz." -ForegroundColor Yellow
Write-Host " Limiting index info to $($TopDBinCache.Name) which accounts for $($TopDBinCache.Count) records returned from cache"
Write-Host " Retrieving index info for $($TopDBinCache.Name)"
[string]$Query = $Query -replace $OldCheckDBStr, ";SET @DatabaseName = '$($TopDBinCache.Name)';"
[string]$Query = $Query -replace ";SET @GetAllDatabases = 1;", ";SET @GetAllDatabases = 0;"
Add-LogRow "sp_BlitzIndex" "User database count>= $MaxUsrDBs" "Limiting index info to $($TopDBinCache.Name) which accounts for $($TopDBinCache.Count) records in the plan cache results"
}

else {
Expand Down Expand Up @@ -6756,6 +6750,8 @@ finally {
$QuerySource = "sp_BlitzIndex @Mode = $Mode"
if (!([string]::IsNullOrEmpty($CheckDB))) {
$QuerySource += ", @DatabaseName = '$CheckDB'; "
}elseif ($UsrDBCount -ge $MaxUsrDBs){
$QuerySource += ", @DatabaseName = '$($TopDBinCache.Name)'; "
}
else {
$QuerySource += ", @GetAllDatabases = 1; "
Expand Down
32 changes: 30 additions & 2 deletions Resources/GetDbInfo.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ SELECT @SkipThis = CASE
ELSE 0
END;

/*Make sure temp table doesn't exist*/
/*Make sure temp tables don't exist*/
IF OBJECT_ID(N'tempdb.dbo.#FSFiles', N'U') IS NOT NULL
DROP TABLE #FSFiles;
/*Create temp table*/
IF OBJECT_ID(N'tempdb.dbo.#BufferPoolInfo', N'U') IS NOT NULL
DROP TABLE #BufferPoolInfo;
/*Create temp tables*/
CREATE TABLE #FSFiles
( [DatabaseID] [SMALLINT] NULL,
[FSFilesCount] [INT] NULL,
[FSFilesSizeGB] [NUMERIC](23, 3) NULL);

CREATE TABLE #BufferPoolInfo(
[database_id] [int] NULL,
[CachedSizeMB] [numeric](23, 3) NULL,
[BufferPool%] [decimal](5, 2) NULL);

/*Cursor to get FILESTREAM files and their sizes for databases that use FS*/
DECLARE DBsWithFS CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY FOR
SELECT DISTINCT DB_NAME([mf].[database_id])
Expand Down Expand Up @@ -74,6 +81,22 @@ WHILE @@FETCH_STATUS = 0
CLOSE DBsWithFS;
DEALLOCATE DBsWithFS;

/*Populate BufferPoolInfo table
I'm not filtering by database name here*/
WITH AggBPInfo
AS
(SELECT [database_id],
CAST(COUNT(*) * 8/1024.0 AS NUMERIC(23, 3)) AS [CachedSizeMB]
FROM sys.dm_os_buffer_descriptors
WHERE [database_id] <> 32767
GROUP BY [database_id])
INSERT INTO #BufferPoolInfo([database_id],[CachedSizeMB],[BufferPool%])
SELECT
[database_id],
[CachedSizeMB],
CAST([CachedSizeMB] / SUM([CachedSizeMB]) OVER() * 100.0 AS DECIMAL(5,2)) AS [BufferPool%]
FROM AggBPInfo

/*Return database files and size info*/
SELECT @ExecSQL = CAST(N'SELECT d.[name] AS [Database],d.[create_date] AS [Created],' AS NVARCHAR(MAX))
+ @LineFeed
Expand Down Expand Up @@ -144,12 +167,16 @@ SELECT @ExecSQL = CAST(N'SELECT d.[name] AS [Database],d.[create_date] AS [Creat
+ @LineFeed + N'WHEN ek.[encryption_state] = 6 THEN ''Protection change in progress'''
+ @LineFeed + N'END AS [EncryptionState]'
END
+ @LineFeed
+ N',bpi.[CachedSizeMB],bpi.[BufferPool%]'
+ @LineFeed + N'FROM sys.master_files AS f'
+ @LineFeed
+ N'INNER JOIN sys.databases AS d ON f.database_id = d.database_id'
+ @LineFeed
+ N'LEFT JOIN #FSFiles AS fs ON f.database_id = fs.DatabaseID'
+ @LineFeed
+ N'LEFT JOIN #BufferPoolInfo AS bpi ON d.database_id = bpi.database_id'
+ @LineFeed
+ CASE
WHEN @SkipThis = 1 THEN ''
ELSE 'CROSS APPLY (SELECT [file_id],'
Expand All @@ -175,6 +202,7 @@ SELECT @ExecSQL = CAST(N'SELECT d.[name] AS [Database],d.[create_date] AS [Creat
+ @LineFeed
+ N'[d].[containment_desc],[d].[page_verify_option_desc],[d].[is_query_store_on], [d].[is_trustworthy_on], d.[is_trustworthy_on],d.[is_encrypted]'
+ @LineFeed
+ N',bpi.[CachedSizeMB],bpi.[BufferPool%]'
+ CASE
WHEN @SkipThis = 1 THEN ''
ELSE ',[l].[VirtualLogFiles], ek.[encryption_state]'
Expand Down
Binary file modified Resources/PSBlitzOutput.xlsx
Binary file not shown.

0 comments on commit 1085011

Please sign in to comment.