forked from nate-hughes/dba-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9955f0
commit f2b07d6
Showing
27 changed files
with
442 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
DECLARE @DBName VARCHAR(64) = 'database_name'; | ||
|
||
-- AG SYNC STATE | ||
SELECT DB_NAME(database_id) as DatabaseName | ||
,synchronization_state_desc | ||
,database_state_desc | ||
FROM sys.dm_hadr_database_replica_states | ||
WHERE is_local=1 | ||
AND is_primary_replica=0 | ||
AND DB_NAME(database_id) = @DBName; | ||
|
||
-- REPLICA ROLLBACK | ||
SELECT object_name | ||
,counter_name | ||
,instance_name | ||
,cntr_value AS [log_kb_remaining] | ||
FROM sys.dm_os_performance_counters | ||
WHERE counter_name = 'log remaining for undo' | ||
AND instance_name LIKE @DBName + '%'; | ||
|
||
-- RECOVERY STATUS | ||
DECLARE @ErrorLog AS TABLE([LogDate] CHAR(24), [ProcessInfo] VARCHAR(64), [TEXT] VARCHAR(MAX)); | ||
|
||
INSERT INTO @ErrorLog | ||
EXEC master..sp_readerrorlog 0, 1, 'Recovery of database', @DBName; | ||
|
||
INSERT INTO @ErrorLog | ||
EXEC master..sp_readerrorlog 0, 1, 'Recovery completed', @DBName; | ||
|
||
SELECT TOP 1 | ||
@DBName AS [DBName] | ||
,[LogDate] | ||
,CASE | ||
WHEN SUBSTRING([TEXT],10,1) = 'c' THEN '100%' | ||
ELSE SUBSTRING([TEXT], CHARINDEX(') is ', [TEXT]) + 4,CHARINDEX(' complete (', [TEXT]) - CHARINDEX(') is ', [TEXT]) - 4) | ||
END AS PercentComplete | ||
,CASE | ||
WHEN SUBSTRING([TEXT],10,1) = 'c' THEN 0 | ||
ELSE CAST(SUBSTRING([TEXT], CHARINDEX('approximately', [TEXT]) + 13,CHARINDEX(' seconds remain', [TEXT]) - CHARINDEX('approximately', [TEXT]) - 13) AS FLOAT)/60.0 | ||
END AS MinutesRemaining | ||
,CASE | ||
WHEN SUBSTRING([TEXT],10,1) = 'c' THEN 0 | ||
ELSE CAST(SUBSTRING([TEXT], CHARINDEX('approximately', [TEXT]) + 13,CHARINDEX(' seconds remain', [TEXT]) - CHARINDEX('approximately', [TEXT]) - 13) AS FLOAT)/60.0/60.0 | ||
END AS HoursRemaining | ||
,[TEXT] | ||
FROM @ErrorLog | ||
ORDER BY CAST([LogDate] as datetime) DESC | ||
,[MinutesRemaining]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Migrating SQL Server to Amazon RDS using native backup and restore | ||
https://aws.amazon.com/blogs/database/migrating-sql-server-to-amazon-rds-using-native-backup-and-restore/ | ||
*/ | ||
|
||
USE master; | ||
GO | ||
|
||
EXEC msdb.dbo.rds_restore_database | ||
@restore_db_name='DatabaseName', | ||
@s3_arn_to_restore_from='arn:aws:s3:::bucketname/sqlserverbackups/DatabaseName_full.bak', | ||
@with_norecovery=1, | ||
@type='FULL'; | ||
GO | ||
|
||
SELECT * FROM msdb.dbo.rds_fn_task_status(NULL,0); | ||
GO | ||
|
||
EXEC msdb.dbo.rds_restore_database | ||
@restore_db_name='DatabaseName', | ||
@s3_arn_to_restore_from='arn:aws:s3:::bucketname/sqlserverbackups/DatabaseName_diff.bak', | ||
@type='DIFFERENTIAL', | ||
@with_norecovery=1; | ||
GO | ||
|
||
SELECT * FROM msdb.dbo.rds_fn_task_status(NULL,0); | ||
GO | ||
|
||
EXEC msdb.dbo.rds_restore_log | ||
@restore_db_name='DatabaseName', | ||
@s3_arn_to_restore_from='arn:aws:s3:::bucketname/sqlserverbackup/DatabaseName_log.trn', | ||
@with_norecovery=0; | ||
go | ||
|
||
SELECT * FROM msdb.dbo.rds_fn_task_status(NULL,0); | ||
GO | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
USE [DATABASE_NAME]; | ||
GO | ||
SET NOCOUNT ON; | ||
|
||
DECLARE @Rows INT | ||
,@BatchSize INT = 4000 | ||
,@Completed INT = 0 | ||
,@Total INT; | ||
|
||
-- \/\/ Populate temp table with PK column(s) \/\/ -- | ||
SELECT [SOME_ID] | ||
INTO #temp_ids | ||
FROM [TABLE_NAME] | ||
WHERE [SOME_COLUMN] IS NULL; | ||
|
||
SELECT @Total = COUNT(*) FROM #temp_ids; | ||
|
||
CREATE TABLE #temp_upd (Id UNIQUEIDENTIFIER); | ||
|
||
WHILE EXISTS (SELECT 1 FROM #temp_ids) | ||
BEGIN | ||
DELETE TOP (@BatchSize) | ||
FROM #temp_ids | ||
OUTPUT deleted.Id INTO #temp_upd; | ||
|
||
UPDATE t | ||
SET [SOME_COLUMN] = 'Some Value' | ||
FROM [TABLE_NAME] t | ||
JOIN #temp_upd tmp ON t.[SOME_ID] = tmp.Id; | ||
|
||
SET @Rows = @@ROWCOUNT; | ||
SET @Completed = @Completed + @Rows; | ||
|
||
PRINT 'Completed ' + cast(@Completed as varchar(10)) + '/' + cast(@Total as varchar(10)); | ||
|
||
TRUNCATE TABLE #temp_upd; | ||
END; | ||
|
||
DROP TABLE IF EXISTS #temp_upd; | ||
DROP TABLE IF EXISTS #temp_ids; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
SELECT d.name AS [database] | ||
,d.is_encrypted | ||
,dek.encryption_state | ||
,dek.percent_complete | ||
,dek.key_algorithm | ||
,dek.key_length | ||
FROM master.sys.databases d | ||
LEFT JOIN master.sys.dm_database_encryption_keys dek ON d.database_id = dek.database_id | ||
ORDER BY d.name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
USE MASTER | ||
GO | ||
|
||
DECLARE @Spid INT | ||
DECLARE @ExecSQL VARCHAR(255) | ||
|
||
DECLARE KillCursor CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY | ||
FOR | ||
SELECT DISTINCT SPID, * | ||
FROM MASTER..SysProcesses | ||
WHERE DBID = DB_ID('DatabaseName') | ||
|
||
OPEN KillCursor | ||
|
||
-- Grab the first SPID | ||
FETCH NEXT | ||
FROM KillCursor | ||
INTO @Spid | ||
|
||
WHILE @@FETCH_STATUS = 0 | ||
BEGIN | ||
SET @ExecSQL = 'KILL ' + CAST(@Spid AS VARCHAR(50)) | ||
|
||
EXEC (@ExecSQL) | ||
|
||
-- Pull the next SPID | ||
FETCH NEXT | ||
FROM KillCursor | ||
INTO @Spid | ||
END | ||
|
||
CLOSE KillCursor | ||
|
||
DEALLOCATE KillCursor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
USE [Database] | ||
GO | ||
|
||
DECLARE @StartSize INT = 102400 -- SET START SIZE OF THE DATABASE FILE (MB) | ||
,@TargetSize INT = 20480 -- SET END SIZE OF THE DATABASE FILE (MB) | ||
; | ||
|
||
WHILE @StartSize > @TargetSize | ||
BEGIN | ||
SET @StartSize = @StartSize - 10240; | ||
DBCC SHRINKFILE (N'FileName' , @StartSize); -- logical name | ||
END; | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
DECLARE @SPID INT = <SPID>; | ||
|
||
SELECT command | ||
,percent_complete | ||
,estimated_completion_time / 1000 AS estimated_completion_time_sec | ||
FROM sys.dm_exec_requests | ||
WHERE session_id = @SPID; | ||
|
||
SELECT | ||
node_id, | ||
physical_operator_name, | ||
SUM(row_count) row_count, | ||
SUM(estimate_row_count) AS estimate_row_count, | ||
CAST(SUM(row_count)*100 AS float)/SUM(estimate_row_count) as estimate_percent_complete | ||
FROM sys.dm_exec_query_profiles | ||
WHERE session_id=@SPID | ||
GROUP BY node_id,physical_operator_name | ||
ORDER BY node_id desc; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.