Skip to content

Commit 108444c

Browse files
committed
Modified Scripts fro Replication
Modified Scripts fro Replication
1 parent bd1be7c commit 108444c

File tree

61 files changed

+1783
-101
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1783
-101
lines changed
24 KB
Binary file not shown.

Baselining/Baselining.ssmssqlproj

+6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@
199199
<AssociatedConnUserName />
200200
<FullPath>sp_Blitz Views Extra.sql</FullPath>
201201
</FileNode>
202+
<FileNode Name="SQLQuery1.sql">
203+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:(local):True</AssociatedConnectionMoniker>
204+
<AssociatedConnSrvName>(local)</AssociatedConnSrvName>
205+
<AssociatedConnUserName />
206+
<FullPath>SQLQuery1.sql</FullPath>
207+
</FileNode>
202208
<FileNode Name="StackOverflow-TopQueries.sql">
203209
<AssociatedConnectionMoniker />
204210
<AssociatedConnSrvName />

Baselining/SQLQuery1.sql

Whitespace-only changes.

BlitzQueries/BlitzQueries.ssmssqlproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@
182182
<FullPath>SQL Server 2012 Diagnostic Information Queries.sql</FullPath>
183183
</FileNode>
184184
<FileNode Name="SQL Server 2014 Diagnostic Information Queries.sql">
185-
<AssociatedConnectionMoniker />
186-
<AssociatedConnSrvName />
185+
<AssociatedConnectionMoniker>8c91a03d-f9b4-46c0-a305-b5dcc79ff907:LOCALHOST:True</AssociatedConnectionMoniker>
186+
<AssociatedConnSrvName>LOCALHOST</AssociatedConnSrvName>
187187
<AssociatedConnUserName />
188188
<FullPath>SQL Server 2014 Diagnostic Information Queries.sql</FullPath>
189189
</FileNode>

BlitzQueries/Other queries.sql

+30-4
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,30 @@ WHERE forwarded_record_count > 0
467467
go
468468

469469

470+
/* Get procedure with RECOMPILE HINT */
471+
SET QUOTED_IDENTIFIER OFF
472+
if OBJECT_ID('tempdb..#procs_with_compile_hint') is not null drop table #procs_with_compile_hint;
473+
create table #procs_with_compile_hint (DB nvarchar(256), OBJ_NAME nvarchar(256), OBJ_TYPE nvarchar(120), [definition] nvarchar(2000));
474+
insert #procs_with_compile_hint
475+
EXEC sp_MSforeachdb "
476+
USE [?];
477+
478+
begin try
479+
SELECT DB_NAME() AS [DB], o.name AS [OBJ_NAME], o.type_desc AS [OBJ_TYPE], t.definition
480+
FROM sys.sql_modules t
481+
INNER JOIN sys.objects o
482+
ON t.object_id = o.object_id
483+
WHERE t.definition LIKE '%RECOMPILE%'
484+
OR t.definition LIKE '%recompile%';
485+
end try
486+
begin catch
487+
print 'Error for '+QUOTENAME(DB_NAME());
488+
end catch
489+
";
490+
select * from #procs_with_compile_hint
491+
go
492+
493+
470494
/* Find Forwarded Records using Cursor Method for VLDBs */
471495
SET NOCOUNT ON;
472496
IF OBJECT_ID('tempdb..#objects') IS NOT NULL
@@ -826,7 +850,10 @@ EXEC master..xp_readerrorlog 0,1, N'virtual log files which is excessive.'
826850

827851
-- Foreign Keys Not Trusted.
828852
-- https://BrentOzar.com/go/trust
829-
EXEC sp_msForEachDB '
853+
if OBJECT_ID('tempdb..#foreign_keys') is not null drop table #foreign_keys;
854+
create table #foreign_keys (id int identity(1,1) not null, dbName nvarchar(256), TableName nvarchar(500), keyname nvarchar(256));
855+
insert #foreign_keys
856+
EXEC sp_MSforeachdb '
830857
USE [?];
831858
SELECT *
832859
FROM (values (DB_NAME())) as DBs(dbName)
@@ -839,10 +866,9 @@ LEFT JOIN
839866
WHERE i.is_not_trusted = 1 AND i.is_not_for_replication = 0
840867
) AS r
841868
ON 1 = 1
869+
WHERE TableName is not null
842870
';
843-
/*
844-
The [SRA] database has foreign keys that were probably disabled, data was changed, and then the key was enabled again. Simply enabling the key is not enough for the optimizer to use this key - we have to alter the table using the WITH CHECK CHECK CONSTRAINT parameter. (https://www.brentozar.com/blitz/foreign-key-trusted/ ). It turns out this can have a huge performance impact on queries, too, because SQL Server won’t use untrusted constraints to build better execution plans.
845-
*/
871+
select * from #foreign_keys
846872

847873
-- Disk Latency Logs
848874
$serverName = 'dbsep0456';

Blocking Alert/BlockingTree-Live-WhoIsActive.sql

+47-28
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ USE tempdb;
22

33
DECLARE @s VARCHAR(MAX), @t VARCHAR(MAX);
44
DECLARE @tableName varchar(255);
5+
DECLARE @start_loop bit = 0;
56
DECLARE @DeleteFlag tinyint; /* 0 = drop, 1 = truncate, 2 = No Action */
67
SET @DeleteFlag = 0;
7-
SET @tableName = 'tempdb.dbo.WhoIsActive_ResultSet';
8+
SET @tableName = 'dbo.WhoIsActive';
89

910
EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @find_block_leaders=1 --,@get_full_inner_text=1
10-
,@get_transaction_info=1 ,@get_locks = 1
11-
,@get_task_info=2, @get_avg_time=1, @get_additional_info=1
12-
,@get_plans=1 /* 1 = current query, 2 = entire batch */
11+
,@get_locks = 1
12+
--,@get_transaction_info=1
13+
--,@get_task_info=2, @get_avg_time=1, @get_additional_info=1
14+
--,@get_plans=1 /* 1 = current query, 2 = entire batch */
1315
,@return_schema = 1, @schema = @s OUTPUT
1416

1517
SET @s = REPLACE(@s, '<table_name>', @tableName)
@@ -23,71 +25,88 @@ IF @DeleteFlag = 2
2325

2426
EXEC(@t);
2527
PRINT @t;
26-
SET @s = 'IF OBJECT_ID('''+@tableName+''') IS NULL BEGIN '+@s+'; CREATE CLUSTERED INDEX [CI_WhoIsActive_ResultSet] ON '+@tableName+' ( [collection_time] ASC, session_id ); END'
28+
SET @s = 'IF OBJECT_ID('''+@tableName+''') IS NULL BEGIN '+@s+'; CREATE CLUSTERED INDEX [CI_WhoIsActive] ON '+@tableName+' ( [collection_time] ASC, session_id ); END'
2729
EXEC(@s);
2830

31+
declare @currrent_date datetime2 = getdate();
32+
while (@start_loop = 1 and @currrent_date > DATEADD(HOUR,-2,GETDATE()))
33+
begin
34+
EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @find_block_leaders=1 --,@get_full_inner_text=1
35+
,@get_locks = 1
36+
--,@get_transaction_info=1
37+
--,@get_task_info=2, @get_avg_time=1, @get_additional_info=1
38+
--,@get_plans=1 /* 1 = current query, 2 = entire batch */
39+
,@destination_table = @tableName;
40+
41+
-- Delete records when No Blocking was Found
42+
delete from dbo.WhoIsActive
43+
where collection_time in (select r.collection_time from tempdb.dbo.WhoIsActive as r
44+
group by r.collection_time having count(r.blocking_session_id) = 0);
45+
46+
waitfor delay '00:01:00'
47+
end
48+
--select * from tempdb.dbo.WhoIsActive
49+
--where [blocking_session_id] is not null
50+
--or [blocked_session_count] > 0
2951

30-
EXEC sp_WhoIsActive @sort_order = '[start_time] ASC', @get_outer_command=1, @find_block_leaders=1 --,@get_full_inner_text=1
31-
,@get_transaction_info=1 ,@get_locks = 1
32-
,@get_task_info=2, @get_avg_time=1, @get_additional_info=1
33-
,@get_plans=1 /* 1 = current query, 2 = entire batch */
34-
,@destination_table = @tableName;
3552

36-
-- Delete records when No Blocking was Found
37-
delete from tempdb.dbo.WhoIsActive_ResultSet
38-
where collection_time in (select r.collection_time from tempdb.dbo.WhoIsActive_ResultSet as r
39-
group by r.collection_time having count(r.blocking_session_id) = 0);
4053

54+
DECLARE @hour_filter int = 2;
4155
;WITH T_BLOCKERS AS
4256
(
4357
-- Find block Leaders
4458
SELECT r.[dd hh:mm:ss.mss], r.[session_id], r.[sql_text],
4559
[batch_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE(r.[sql_command],r.[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'<?query --',''),'--?>',''),
4660
r.[sql_command], r.[login_name], r.[wait_info], r.[tempdb_allocations], r.[tempdb_current],
4761
r.[blocking_session_id], r.[blocked_session_count], r.[reads], r.[writes], r.[physical_reads], r.[CPU], r.[used_memory], r.[status], r.[open_tran_count],
48-
r.[percent_complete], r.[host_name], r.[database_name], r.[program_name], r.locks, r.[start_time], r.[login_time], r.[request_id], r.[collection_time]
62+
r.[percent_complete], r.[host_name], r.[database_name], r.[program_name], r.[start_time], r.[login_time], r.[request_id], r.[collection_time] --,locks
4963
,[LEVEL] = CAST (REPLICATE ('0', 4-LEN (CAST (r.session_id AS VARCHAR))) + CAST (r.session_id AS VARCHAR) AS VARCHAR (1000))
50-
FROM tempdb.dbo.WhoIsActive_ResultSet AS r
64+
FROM dbo.WhoIsActive AS r with (nolock)
5165
WHERE (ISNULL(r.blocking_session_id,0) = 0 OR ISNULL(r.blocking_session_id,0) = r.session_id)
52-
AND EXISTS (SELECT * FROM tempdb.dbo.WhoIsActive_ResultSet AS R2 WHERE R2.collection_time = r.collection_time AND ISNULL(R2.blocking_session_id,0) = r.session_id AND ISNULL(R2.blocking_session_id,0) <> R2.session_id)
66+
AND EXISTS (SELECT * FROM dbo.WhoIsActive AS R2 WHERE R2.collection_time = r.collection_time AND ISNULL(R2.blocking_session_id,0) = r.session_id AND ISNULL(R2.blocking_session_id,0) <> R2.session_id)
5367
--
5468
UNION ALL
5569
--
5670
SELECT r.[dd hh:mm:ss.mss], r.[session_id], r.[sql_text],
5771
[batch_text] = REPLACE(REPLACE(REPLACE(REPLACE(CAST(COALESCE(r.[sql_command],r.[sql_text]) AS VARCHAR(MAX)),char(13),''),CHAR(10),''),'<?query --',''),'--?>',''),
5872
r.[sql_command], r.[login_name], r.[wait_info], r.[tempdb_allocations], r.[tempdb_current],
5973
r.[blocking_session_id], r.[blocked_session_count], r.[reads], r.[writes], r.[physical_reads], r.[CPU], r.[used_memory], r.[status], r.[open_tran_count],
60-
r.[percent_complete], r.[host_name], r.[database_name], r.[program_name], r.locks, r.[start_time], r.[login_time], r.[request_id], r.[collection_time]
74+
r.[percent_complete], r.[host_name], r.[database_name], r.[program_name], r.[start_time], r.[login_time], r.[request_id], r.[collection_time] --,locks
6175
,CAST (B.LEVEL + RIGHT (CAST ((1000 + r.session_id) AS VARCHAR (100)), 4) AS VARCHAR (1000)) AS LEVEL
62-
FROM tempdb.dbo.WhoIsActive_ResultSet AS r
76+
FROM dbo.WhoIsActive AS r with (nolock)
6377
INNER JOIN
6478
T_BLOCKERS AS B
6579
ON r.collection_time = B.collection_time
6680
AND r.blocking_session_id = B.session_id
6781
WHERE r.blocking_session_id <> r.session_id
6882
)
6983
--select * from T_BLOCKERS
70-
SELECT [BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1)
84+
85+
SELECT r.[collection_time],
86+
[BLOCKING_TREE] = N' ' + REPLICATE (N'| ', LEN (LEVEL)/4 - 1)
7187
+ CASE WHEN (LEN(LEVEL)/4 - 1) = 0
7288
THEN 'HEAD - '
7389
ELSE '|------ '
7490
END
7591
+ CAST (r.session_id AS NVARCHAR (10)) + N' ' + (CASE WHEN LEFT(r.[batch_text],1) = '(' THEN SUBSTRING(r.[batch_text],CHARINDEX('exec',r.[batch_text]),LEN(r.[batch_text])) ELSE r.[batch_text] END),
76-
r.[dd hh:mm:ss.mss], r.[wait_info], r.[blocked_session_count], r.[blocking_session_id], --r.[sql_text], r.[sql_command],
77-
r.[login_name], r.[host_name], r.[database_name], r.[program_name], r.locks, r.[tempdb_allocations], r.[tempdb_current],
92+
r.[dd hh:mm:ss.mss], r.[wait_info], r.[blocked_session_count], r.[blocking_session_id], r.[sql_text], r.[sql_command],
93+
r.[login_name], r.[host_name], r.[database_name], r.[program_name], r.[tempdb_allocations], r.[tempdb_current],
7894
r.[reads], r.[writes], r.[physical_reads], r.[CPU], r.[used_memory], r.[status], r.[open_tran_count],
79-
r.[percent_complete], r.[start_time], r.[login_time], r.[request_id], [sql_command]
80-
,r.[collection_time]
95+
r.[percent_complete], r.[start_time], r.[login_time], r.[request_id] --,locks
8196
FROM T_BLOCKERS AS r
82-
ORDER BY collection_time, LEVEL ASC;
97+
WHERE [collection_time] >= DATEADD(HOUR,-@hour_filter,getdate())
98+
ORDER BY collection_time desc, LEVEL ASC
99+
option ( MaxRecursion 0 );
83100

84101
/*
85102
select @@servername as srvName, r.login_name, r.program_name, r.database_name, count(r.session_id) as session_counts
86-
from tempdb.dbo.WhoIsActive_ResultSet AS r
103+
from tempdb.dbo.WhoIsActive AS r
87104
group by r.login_name, r.program_name, r.database_name
88-
having count(r.session_id) > (select count(distinct [collection_time]) from tempdb.dbo.WhoIsActive_ResultSet as r)
105+
having count(r.session_id) > (select count(distinct [collection_time]) from tempdb.dbo.WhoIsActive as r)
89106
order by session_counts desc
90107
go
91-
92108
*/
93109

110+
111+
112+
--select * from tempdb.dbo.WhoIsActive

ErrorLogs/QRY-Search-ErrorLog.sql

+28-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
-- Check-SQLServerAvailability
2+
declare @time datetime;
3+
select @time = d.create_date
4+
FROM sys.databases as d
5+
WHERE d.name = 'tempdb';
6+
SELECT @@servername as srv, @time as server_uptime, DATEDIFF(MINUTE,@time,GETDATE()) AS [up_time(minutes)], DATEDIFF(HOUR,@time,GETDATE()) AS [up_time(hours)];
17

2-
declare @start_time datetime, @end_time datetime;
3-
set @start_time = '2020-08-22 03:00:00.000' -- August 22, 2020 05:16:00
4-
--set @start_time = DATEADD(minute,-120,getdate());
5-
set @end_time = '2020-08-22 03:30:00.000';
6-
--set @end_time = DATEADD(minute,30,@start_time)
8+
declare @start_time datetime, @end_time datetime, @err_msg_1 nvarchar(256) = null, @err_msg_2 nvarchar(256) = null;
9+
--set @start_time = '2020-09-19 03:20:00.000' -- August 22, 2020 05:16:00
10+
--set @time = DATEADD(minute,-60*12,getdate());
11+
set @start_time = DATEADD(minute,-20,@time);
12+
--set @end_time = '2020-08-22 03:30:00.000';
13+
set @end_time = GETDATE()
14+
--set @end_time = DATEADD(minute,60*3,@start_time)
15+
--set @err_msg_1 = 'has been rejected'
16+
--set @err_msg_2 = '';
717

18+
--EXEC master.dbo.xp_enumerrorlogs
819
declare @NumErrorLogs int;
920
exec master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE',
1021
N'Software\Microsoft\MSSQLServer\MSSQLServer',
@@ -17,14 +28,18 @@ declare @counter int = isnull(@NumErrorLogs,6);
1728
while @counter >= 0
1829
begin
1930
insert #errorlog
20-
EXEC master.dbo.xp_readerrorlog @counter, 1, NULL, NULL, @start_time, @end_time, "asc";
31+
EXEC master.dbo.xp_readerrorlog @counter, 1, @err_msg_1, @err_msg_2, @start_time, @end_time, "asc";
2132
set @counter -= 1;
2233

2334
if exists (select * from #errorlog where LogDate > @end_time)
2435
break;
2536
end
2637

27-
select ROW_NUMBER()over(order by LogDate asc) as id,*
38+
39+
select ROW_NUMBER()over(order by LogDate asc) as id,
40+
getdate() as [now],
41+
--master.dbo.time2duration(LogDate,'datetime') as [Log-Duration],
42+
*
2843
from #errorlog as e
2944
where 1 = 1
3045
--and e.Text like '%pricing%'
@@ -33,10 +48,15 @@ where 1 = 1
3348
--and e.Text not like 'DbMgrPartnerCommitPolicy::SetSyncState:%'
3449
--and e.Text not like 'SQL Server blocked access to procedure%'
3550
--and e.Text not like 'Login failed for user %'
51+
--and e.Text not like 'I/O is frozen on database%'
52+
--and e.Text not like 'I/O was resumed on database%'
53+
--and e.Text not like 'Process ID % was killed by hostname xx, host process ID %'
3654
--and e.Text not like 'AlwaysOn Availability Groups connection with secondary database terminated for primary database %'
3755
--and e.Text not like 'AlwaysOn Availability Groups connection with secondary database established for primary database %'
3856
--and e.Text like '%ALTER DATABASE%'
39-
order by LogDate asc
57+
--and e.Text like '%inout%'
58+
--and e.Text like '%rejected due to breached'
59+
order by id desc
4060

4161
/*
4262
select create_date

PowerShell Commands/Common-Commands.sql

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ Get-ChildItem -Path 'F:\' -Recurse -Force -ErrorAction SilentlyContinue |
3131
Sort-Object -Property SizeBytes -Descending | Out-GridView
3232

3333
-- 9) Check if -Verbose switch is used
34-
$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
34+
$Verbose = $false;
35+
if($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent -or [String]::IsNullOrEmpty($MyInvocation.PSScriptRoot)) {
36+
$Verbose = $true;
37+
}
38+
39+
if($Verbose) {
40+
Write-Host ("{0,-8} {1} - {2}" -f 'INFO:', "$(Get-Date)", "Extract 'Finding' & 'Create TSQL' from Index Summary to match with Non-Prod");
41+
}
3542

3643
-- 10) Check if Module is installed
3744
if (Get-Module -ListAvailable -Name SqlServer) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
$threads = 4;
2+
$server_count = $tsql_Prod_Servers_Result.Count; # List of objects to iterate through
3+
$counter = 0;
4+
$jobs = @();
5+
$server_count = 10 # Remove this line
6+
do {
7+
$running_job_count = (Get-Job -Name "BlitzIndex-*" | ? {$_.State -eq 'Running'}).count;
8+
if($running_job_count -lt $threads)
9+
{
10+
$srvObj = $tsql_Prod_Servers_Result[$counter];
11+
$ServerName = $srvObj.FriendlyName;
12+
$jobs += Start-Job -Name "BlitzIndex-$ServerName" -ScriptBlock $ScriptBlock;
13+
$counter = $counter + 1;
14+
Write-Host "Started job for $ServerName";
15+
}
16+
else {
17+
Start-Sleep -Seconds 10;
18+
}
19+
}while($counter -lt $server_count);
20+
21+
$jobs | Wait-Job | Out-Null;
22+
23+
$failed_jobs = $jobs | Where-Object {$this.State -eq 'Failed'};
24+
$passed_jobs = $jobs | Where-Object {$this.State -ne 'Failed'};
25+
26+
$out = $passed_jobs | Receive-Job;
27+
$jobs | Remove-Job;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# https://github.com/proxb/PoshRSJob
2+
Import-Module PoshRSJob;
3+
$Test = 'test'
4+
$Something = 1..10
5+
1..5|start-rsjob -Name {$_} -ScriptBlock {
6+
[pscustomobject]@{
7+
Result=($_*2)
8+
Test=$Using:Test
9+
Something=$Using:Something
10+
}
11+
}
12+
Get-RSjob | Receive-RSJob
13+
14+
15+
16+
17+
# This shows the streaming aspect with Wait-RSJob
18+
1..10|Start-RSJob {
19+
if (1 -BAND $_){
20+
"First ($_)"
21+
}Else{
22+
Start-sleep -seconds 2
23+
"Last ($_)"
24+
}
25+
}|Wait-RSJob|Receive-RSJob|ForEach{"I am $($_)"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
$Scriptblock = {
2+
param($Name)
3+
New-Item -Name $Name -ItemType File
4+
}
5+
6+
$MaxThreads = 5
7+
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads)
8+
$RunspacePool.Open()
9+
$Jobs = @()
10+
11+
1..10 | Foreach-Object {
12+
$PowerShell = [powershell]::Create()
13+
$PowerShell.RunspacePool = $RunspacePool
14+
$PowerShell.AddScript($ScriptBlock).AddArgument($_)
15+
$Jobs += $PowerShell.BeginInvoke()
16+
}
17+
18+
while ($Jobs.IsCompleted -contains $false) {
19+
Start-Sleep 1
20+
}

0 commit comments

Comments
 (0)