浏览代码

a bit of prettifying

Rob.Sewell 6 年之前
父节点
当前提交
d06f4e3dfd
共有 1 个文件被更改,包括 65 次插入20 次删除
  1. 65 20
      BPCheck/Create Notebook from sql file.ps1

+ 65 - 20
BPCheck/Create Notebook from sql file.ps1

@@ -3,6 +3,8 @@ $RawSql = Get-Content .\BPCheck\Check_BP_Servers.sql -Raw
 $SplitSQL = $RawSql -split '--------------------------------------------------------------------------------------------------------------------------------'
 
 $GlobalVariables = @"
+-- These are the variables that are required for the Azure Data Studio Notebook to function in the same way as the stored procedure. Unfortunately, it is easier to add them to the beginning of each code block.  
+
 SET NOCOUNT ON;
 SET ANSI_WARNINGS ON;
 SET QUOTED_IDENTIFIER ON;
@@ -34,16 +36,68 @@ DECLARE @query NVARCHAR(1000)
 DECLARE @diskfrag bit
 DECLARE @accntsqlservice NVARCHAR(128)
 DECLARE @maxservermem bigint, @systemmem bigint
--- Does not include reserved memory in the memory manager
 DECLARE @mwthreads_count int
 DECLARE @ifi bit
 DECLARE @duration tinyint
 DECLARE @adhoc smallint
 DECLARE @gen_scripts bit 
-DECLARE @ixfrag bit = 1 --(1 = ON; 0 = OFF)
-DECLARE @ixfragscanmode VARCHAR(8) = 'LIMITED' --(Valid inputs are DEFAULT, NULL, LIMITED, SAMPLED, or DETAILED. The default (NULL) is LIMITED)
-DECLARE @logdetail bit = 0 --(1 = ON; 0 = OFF)
+DECLARE @ixfrag bit
+DECLARE @ixfragscanmode VARCHAR(8) 
+DECLARE @logdetail bit 
+DECLARE @spn_check bit 
+DECLARE @dbcmptlevel int
+
+-- With the variables declared we then set them. You can alter these values for different checks. The instructions will show where you should do this.
+
+-- Set @dbScope to the appropriate list of database IDs if there's a need to have a specific scope for database specific checks.
+-- Valid input should be numeric value(s) between single quotes, as follows: '1,6,15,123'
+-- Leave NULL for all databases
+SELECT @dbScope = NULL -- (NULL = All DBs; '<database_name>')
+
+-- Set @ptochecks to OFF if you want to skip more performance tuning and optimization oriented checks.
+SELECT @ptochecks = 1 -- 1 for ON 0 for OFF
+
+-- Set @duration to the number of seconds between data collection points regarding perf counters, waits and latches. -- Duration must be between 10s and 255s (4m 15s), with a default of 90s.
+SELECT @duration = 90
+
+-- Set @logdetail to OFF if you want to get just the summary info on issues in the Errorlog, rather than the full detail.
+SELECT @logdetail = 0 --(1 = ON; 0 = OFF)
 
+-- Set @diskfrag to ON if you want to check for disk physical fragmentation. 
+--	Can take some time in large disks. Requires elevated privileges.
+--	See https://support.microsoft.com/help/3195161/defragmenting-sql-server-database-disk-drives
+SELECT @diskfrag = 0 --(1 = ON; 0 = OFF)
+
+-- Set @ixfrag to ON if you want to check for index fragmentation. 
+--	Can take some time to collect data depending on number of databases and indexes, as well as the scan mode chosen in @ixfragscanmode.
+SELECT @ixfrag = 0 --(1 = ON; 0 = OFF)
+
+-- Set @ixfragscanmode to the scanning mode you prefer. 
+-- 	More detail on scanning modes available at https://docs.microsoft.com/sql/relational-databases/system-dynamic-management-views/sys-dm-db-index-physical-stats-transact-sql
+SELECT @ixfragscanmode = 'LIMITED' --(Valid inputs are DEFAULT, NULL, LIMITED, SAMPLED, or DETAILED. The default (NULL) is LIMITED)
+
+-- Set @bpool_consumer to OFF if you want to list what are the Buffer Pool Consumers from Buffer Descriptors. 
+-- Mind that it may take some time in servers with large caches.
+SELECT @bpool_consumer = 1 -- 1 for ON 0 for OFF
+
+-- Set @spn_check to OFF if you want to skip SPN checks.
+SELECT  @spn_check = 0 --(1 = ON; 0 = OFF)
+
+-- Set @gen_scripts to ON if you want to generate index related scripts.
+-- 	These include drops for Duplicate, Redundant, Hypothetical and Rarely Used indexes, as well as creation statements for FK and Missing Indexes.
+SELECT @gen_scripts = 0 -- 1 for enable 0 for disable
+
+-- Set @allow_xpcmdshell to OFF if you want to skip checks that are dependant on xp_cmdshell. 
+-- Note that original server setting for xp_cmdshell would be left unchanged if tests were allowed.
+SELECT @allow_xpcmdshell = 1 -- 1 for enable 0 for disable
+
+-- Set @custompath below and set the custom desired path for .ps1 files. 
+-- 	If not, default location for .ps1 files is the Log folder.
+SELECT @custompath = NULL
+
+-- These values are gathered for when they are needed
+SELECT @langid = lcid FROM sys.syslanguages WHERE name = @@LANGUAGE
+SELECT @adhoc = CONVERT(bit, [value]) FROM sys.configurations WHERE [Name] = 'optimize for ad hoc workloads';
 SELECT @masterpid = principal_id FROM master.sys.database_principals (NOLOCK) WHERE sid = SUSER_SID()
 SELECT @instancename = CONVERT(VARCHAR(128),SERVERPROPERTY('InstanceName')) 
 SELECT @server = RTRIM(CONVERT(VARCHAR(128), SERVERPROPERTY('MachineName')))
@@ -51,20 +105,8 @@ SELECT @sqlmajorver = CONVERT(int, (@@microsoftversion / 0x1000000) & 0xff)
 SELECT @sqlminorver = CONVERT(int, (@@microsoftversion / 0x10000) & 0xff)
 SELECT @sqlbuild = CONVERT(int, @@microsoftversion & 0xffff)
 SELECT @clustered = CONVERT(bit,ISNULL(SERVERPROPERTY('IsClustered'),0))
-SELECT @dbScope = NULL -- (NULL = All DBs; '<database_name>')
-SELECT @ptochecks = 1 -- 1 for enable 0 for disable
-SELECT @bpool_consumer = 1 -- 1 for enable 0 for disable
-SELECT @allow_xpcmdshell = 1 -- 1 for enable 0 for disable
-SELECT @custompath = NULL
-SELECT @langid = lcid FROM sys.syslanguages WHERE name = @@LANGUAGE
-SELECT @diskfrag = 1
-SELECT @duration = 90
-SELECT @adhoc = CONVERT(bit, [value]) FROM sys.configurations WHERE [Name] = 'optimize for ad hoc workloads';
-SELECT @gen_scripts = 0 -- 1 for enable 0 for disable
-DECLARE @dbcmptlevel int
-SELECT @ixfrag = 1 --(1 = ON; 0 = OFF)
-SELECT @ixfragscanmode = 'LIMITED' --(Valid inputs are DEFAULT, NULL, LIMITED, SAMPLED, or DETAILED. The default (NULL) is LIMITED)
-SELECT @logdetail = 0 --(1 = ON; 0 = OFF)
+
+-- There are some variables that get passed from one check to another. This is easy in the stored procedure but won't work in the Notebook so we have to create a table in tempdb and read them from there
 
 IF NOT EXISTS (SELECT [object_id]
 	FROM tempdb.sys.objects (NOLOCK)
@@ -72,6 +114,7 @@ IF NOT EXISTS (SELECT [object_id]
 	BEGIN
 	CREATE TABLE tempdb.dbo.dbvars(VarName VarChar(256),VarValue VarChar(256))
 	END
+
 SELECT @ostype = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'ostype');
 SELECT @osver = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'osver');
 SELECT @affined_cpus = (SELECT VarValue FROM tempdb.dbo.dbvars WHERE VarName = 'affined_cpus');
@@ -88,12 +131,13 @@ BEGIN
 	SELECT @IsHadrEnabled = CASE WHEN SERVERPROPERTY('EngineEdition') = 8 THEN 1 ELSE CONVERT(tinyint, SERVERPROPERTY('IsHadrEnabled')) END;
 END
 
+-- The T-SQL for the Check starts below
 
 "@
 # We don't need the first or the last as they are only required for the sp
 $Cells = foreach ($Chunk in $SplitSQL[1..($SplitSQL.Length -2)]) {
 
-    if ($Chunk.Trim().StartsWith('#sponly#') -or $Chunk.Trim().StartsWith('-- #sponly#')) { 
+    if ($Chunk.Trim().StartsWith('--- #sponly#')) { 
         # Ignore this tag
     }
     elseif ($Chunk.trim().StartsWith('--')) {  
@@ -110,10 +154,11 @@ $Cells = foreach ($Chunk in $SplitSQL[1..($SplitSQL.Length -2)]) {
         catch {
             Write-Warning "Gah it went wrong"
         }
-
     }
 }
 
+# Create the notebook
 New-ADSWorkBook -Type SQL -Path .\BPCheck\DynamicBPCheck.ipynb -cells $Cells
 
+# Open the notebook
 azuredatastudio.cmd .\BPCheck\DynamicBPCheck.ipynb