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
1b0e298
commit a06d068
Showing
7 changed files
with
5,695 additions
and
5,312 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Always On Secondary Database in Reverting/In Recovery | ||
https://bahtisametcoban.home.blog/2019/02/07/always-on-secondary-database-in-reverting-in-recovery/ | ||
*/ | ||
|
||
DEcLARE @DatabaseName SYSNAME = NULL; | ||
|
||
/* | ||
There will be 3 phases of secondary database replica state during undo process: | ||
Synchronization State: “NOT SYNCHRONIZING” ; Database State: ONLINE | ||
Synchronization State: “NOT SYNCHRONIZING” ; Database State: RECOVERING | ||
Synchronization State: “REVERTING” ; Database State: RECOVERING | ||
*/ | ||
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 (@DatabaseName IS NULL OR DB_NAME(database_id) = @DatabaseName); | ||
|
||
-- SQLServer:Database Replica: Log remaining for undo | ||
SELECT [object_name] | ||
,[counter_name] | ||
,[cntr_value] -- the amount of log in kb remaining to complete the undo phase | ||
,instance_name | ||
FROM sys.dm_os_performance_counters | ||
WHERE [object_name] LIKE '%Database Replica%' | ||
AND [counter_name] = 'Log remaining for undo' | ||
AND (@DatabaseName IS NULL OR [instance_name] = @DatabaseName); |
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,96 @@ | ||
/* Enable CDC for Database | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-enable-db-transact-sql?view=sql-server-ver15 | ||
*/ | ||
USE MarletteUAT; | ||
GO | ||
EXEC sys.sp_cdc_enable_db; | ||
GO | ||
|
||
/* Enable CDC for Table(s) | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-enable-table-transact-sql?view=sql-server-ver15 | ||
*/ | ||
EXEC sys.sp_cdc_enable_table | ||
@source_schema = N'schemaname' | ||
,@source_name = N'tablename' | ||
--,@capture_instance = N'schemaname_tablename' | ||
,@role_name = N'cdc_admin'; | ||
GO | ||
|
||
/* View CDC configuration | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-enable-table-transact-sql?view=sql-server-ver15 | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-help-jobs-transact-sql?view=sql-server-ver15 | ||
*/ | ||
EXEC sys.sp_cdc_help_change_data_capture; | ||
GO | ||
EXEC sys.sp_cdc_help_jobs; | ||
GO | ||
|
||
SELECT * FROM [cdc].[captured_columns]; | ||
SELECT * FROM [cdc].[change_tables]; | ||
SELECT * FROM [cdc].[ddl_history]; | ||
SELECT * FROM [cdc].[index_columns]; | ||
SELECT * FROM [cdc].[lsn_time_mapping]; | ||
SELECT * FROM [msdb].[dbo].[cdc_jobs]; | ||
GO | ||
|
||
/* Update CDC SQL Agent jobs | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-change-job-transact-sql?view=sql-server-ver15 | ||
cdc.{DatabaseName}_capture | ||
cdc.{DatabaseName}_cleanup | ||
*/ | ||
-- change capture interval to 5 seconds (default) | ||
EXECUTE sys.sp_cdc_change_job | ||
@job_type = N'capture' | ||
,@pollinginterval = 5; -- in seconds | ||
GO | ||
-- job must be restarted before new settings take effect | ||
EXEC sys.sp_cdc_stop_job @job_type = 'capture'; | ||
EXEC sys.sp_cdc_start_job @job_type = 'capture'; | ||
GO | ||
-- change data retention to 3 days (default) | ||
EXECUTE sys.sp_cdc_change_job | ||
@job_type = N'cleanup' | ||
,@retention = 4320; -- in minutes | ||
GO | ||
-- job must be restarted before new settings take effect | ||
EXEC sys.sp_cdc_stop_job @job_type = 'cleanup'; | ||
EXEC sys.sp_cdc_start_job @job_type = 'cleanup'; | ||
GO | ||
|
||
/* View CDC captured data | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-functions/cdc-fn-cdc-get-all-changes-capture-instance-transact-sql?view=sql-server-ver15 | ||
The _$operation column tells us what operation was logged: | ||
1 = delete | ||
2 = insert | ||
3 = update (captured column values are those before the update operation) | ||
4 = update (captured column values are those after the update operation) | ||
*/ | ||
DECLARE @from_lsn binary(10) = sys.fn_cdc_get_min_lsn('capture_instance') | ||
,@to_lsn binary(10) = sys.fn_cdc_get_max_lsn(); | ||
|
||
SELECT * | ||
FROM cdc.fn_cdc_get_all_changes_sst_funding_archive (@from_lsn, @to_lsn, N'all'); | ||
GO | ||
|
||
SELECT * | ||
FROM [cdc].[sst_funding_archive_CT]; | ||
GO | ||
|
||
/* Disable CDC for Table(s) | ||
Drops the CDC change table and system functions associated with the specified source table and capture instance. | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-disable-table-transact-sql?view=sql-server-ver15 | ||
*/ | ||
EXECUTE sys.sp_cdc_disable_table | ||
@source_schema = N'schemaname', | ||
@source_name = N'tablename', | ||
@capture_instance = N'capture_instance'; | ||
GO | ||
|
||
/* Disable CDC for Database | ||
https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sys-sp-cdc-disable-db-transact-sql?view=sql-server-ver15 | ||
Disables CDC for all currently enabled tables in the database. All system objects related to CDC are dropped. | ||
*/ | ||
EXECUTE sp_cdc_disable_db; | ||
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,25 @@ | ||
/* | ||
Debezium connector for SQL Server | ||
https://debezium.io/documentation/reference/connectors/sqlserver.html#online-schema-updates | ||
*/ | ||
|
||
/* Online schema updates */ | ||
-- 1. Apply all changes to the source table schema. | ||
ALTER TABLE [schemaname].[tablename] ADD [columnname] VARCHAR(50) NULL; | ||
|
||
-- 2.Create a new capture table for the update source table by running the sys.sp_cdc_enable_table stored procedure with a unique value for the parameter @capture_instance. | ||
EXEC sys.sp_cdc_enable_table | ||
@source_schema = 'schemaname' | ||
,@source_name = 'tablename' | ||
,@role_name = 'cdc_admin' | ||
,@capture_instance = 'schemaname_tablename_v2'; | ||
GO | ||
|
||
-- 3. When Debezium starts streaming from the new capture table, you can drop the old capture table by running the sys.sp_cdc_disable_table stored procedure with the parameter | ||
-- @capture_instance set to the old capture instance name. | ||
EXEC sys.sp_cdc_disable_table | ||
@source_schema = 'schemaname' | ||
,@source_name = 'tablename' | ||
,@capture_instance = 'schemaname_tablename'; | ||
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
Oops, something went wrong.