Skip to content

Commit

Permalink
latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nate-hughes committed Dec 13, 2021
1 parent 1b0e298 commit a06d068
Show file tree
Hide file tree
Showing 7 changed files with 5,695 additions and 5,312 deletions.
30 changes: 30 additions & 0 deletions alwayson-failover-database-status.sql
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);
96 changes: 96 additions & 0 deletions cdc-master.sql
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

25 changes: 25 additions & 0 deletions cdc-online-schema-updates.sql
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

122 changes: 61 additions & 61 deletions index-get-detail-by-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GO

DECLARE @DBId INT = DB_ID()
,@SchemaName sysname
,@TblName sysname = N'[dbo].[FactLoanResponseAttribute]'
,@TblName sysname = N'[dbo].[ApplicationUnderwriting]'
,@TblId INT
,@MinFragmentation REAL = 5.0 -- Defaulted to 5% as recommended by MS in BOL
,@MinPageCount INT = 1000 -- Defaulted to 1000 pages as recommended by MS in BOL
Expand Down Expand Up @@ -70,68 +70,68 @@ ORDER BY i.index_id
OPTION (MAXDOP 2);
/****** INDEX CATALOG BLOCK END ******/

/****** INDEX SIZE BLOCK START ******/
DECLARE @tmp_compression TABLE (
object_name NVARCHAR(128)
,schema_name NVARCHAR(128)
,index_id INT PRIMARY KEY CLUSTERED
,partition_number INT
,size_with_current_compression_setting BIGINT
,size_with_requested_compression_setting BIGINT
,sample_size_with_current_compression_setting BIGINT
,sample_size_with_requested_compression_setting BIGINT
);
--/****** INDEX SIZE BLOCK START ******/
--DECLARE @tmp_compression TABLE (
-- object_name NVARCHAR(128)
-- ,schema_name NVARCHAR(128)
-- ,index_id INT PRIMARY KEY CLUSTERED
-- ,partition_number INT
-- ,size_with_current_compression_setting BIGINT
-- ,size_with_requested_compression_setting BIGINT
-- ,sample_size_with_current_compression_setting BIGINT
-- ,sample_size_with_requested_compression_setting BIGINT
--);

IF @EstimateCompression = 1
INSERT INTO @tmp_compression (
object_name
,schema_name
,index_id
,partition_number
,size_with_current_compression_setting
,size_with_requested_compression_setting
,sample_size_with_current_compression_setting
,sample_size_with_requested_compression_setting
)
EXEC sys.sp_estimate_data_compression_savings
@schema_name = @SchemaName
,@object_name = @TblName
,@index_id = NULL
,@partition_number = NULL
,@data_compression = @CompressionType;
--IF @EstimateCompression = 1
-- INSERT INTO @tmp_compression (
-- object_name
-- ,schema_name
-- ,index_id
-- ,partition_number
-- ,size_with_current_compression_setting
-- ,size_with_requested_compression_setting
-- ,sample_size_with_current_compression_setting
-- ,sample_size_with_requested_compression_setting
-- )
-- EXEC sys.sp_estimate_data_compression_savings
-- @schema_name = @SchemaName
-- ,@object_name = @TblName
-- ,@index_id = NULL
-- ,@partition_number = NULL
-- ,@data_compression = @CompressionType;

SELECT 'SIZE:' AS Info;
SELECT '[' + DB_NAME() + '].[' + OBJECT_SCHEMA_NAME(@TblId, @DBId) + '].[' + OBJECT_NAME(@TblId, @DBId) + ']' AS Object
,i.name AS [Index]
,i.type_desc AS [Index Type]
,ps.alloc_unit_type_desc AS [Allocation Type]
,CONVERT(NUMERIC(9, 2), ps.page_count * 8 * /*convert to MB*/ 0.000976562) AS [Index Size (MB)]
,ps.page_count AS Pages
,CONVERT(NUMERIC(5, 2), ps.avg_page_space_used_in_percent) AS [Avg Page Space Used (%)]
,ps.record_count AS Records
,ps.avg_record_size_in_bytes AS [Avg Record Size (bytes)]
,ps.compressed_page_count AS [Compressed Pages]
,CONVERT(NUMERIC(5, 2), tmp.size_with_requested_compression_setting * /*convert to MB*/ 0.000976562) AS [Est Compressed Size (MB)]
,'ALTER INDEX [' + i.name + +'] ON ' + '[' + OBJECT_SCHEMA_NAME(@TblId, @DBId) + '].['
+ OBJECT_NAME(@TblId, @DBId) + ']' + ' REBUILD WITH ( FILLFACTOR = '
+ CONVERT( CHAR(3), CASE WHEN i.fill_factor = 0 THEN 100
ELSE i.fill_factor
END
) + ', ONLINE=ON, SORT_IN_TEMPDB=ON );' + ' UPDATE STATISTICS [' + OBJECT_SCHEMA_NAME(@TblId, @DBId)
+ '].[' + OBJECT_NAME(@TblId, @DBId) + '] [' + i.name + +'];' AS [Rebuild Script]
FROM sys.dm_db_index_physical_stats(@DBId, @TblId, NULL, NULL, 'SAMPLED') AS ps
INNER JOIN sys.indexes i
ON ps.object_id = i.object_id
AND ps.index_id = i.index_id
LEFT OUTER JOIN @tmp_compression tmp
ON i.index_id = tmp.index_id
WHERE i.object_id = @TblId
AND i.is_disabled = 0
AND i.is_hypothetical = 0
ORDER BY i.index_id
,i.name
OPTION (MAXDOP 2);
/****** INDEX SIZE BLOCK END ******/
--SELECT 'SIZE:' AS Info;
--SELECT '[' + DB_NAME() + '].[' + OBJECT_SCHEMA_NAME(@TblId, @DBId) + '].[' + OBJECT_NAME(@TblId, @DBId) + ']' AS Object
-- ,i.name AS [Index]
-- ,i.type_desc AS [Index Type]
-- ,ps.alloc_unit_type_desc AS [Allocation Type]
-- ,CONVERT(NUMERIC(9, 2), ps.page_count * 8 * /*convert to MB*/ 0.000976562) AS [Index Size (MB)]
-- ,ps.page_count AS Pages
-- ,CONVERT(NUMERIC(5, 2), ps.avg_page_space_used_in_percent) AS [Avg Page Space Used (%)]
-- ,ps.record_count AS Records
-- ,ps.avg_record_size_in_bytes AS [Avg Record Size (bytes)]
-- ,ps.compressed_page_count AS [Compressed Pages]
-- ,CONVERT(NUMERIC(5, 2), tmp.size_with_requested_compression_setting * /*convert to MB*/ 0.000976562) AS [Est Compressed Size (MB)]
-- ,'ALTER INDEX [' + i.name + +'] ON ' + '[' + OBJECT_SCHEMA_NAME(@TblId, @DBId) + '].['
-- + OBJECT_NAME(@TblId, @DBId) + ']' + ' REBUILD WITH ( FILLFACTOR = '
-- + CONVERT( CHAR(3), CASE WHEN i.fill_factor = 0 THEN 100
-- ELSE i.fill_factor
-- END
-- ) + ', ONLINE=ON, SORT_IN_TEMPDB=ON );' + ' UPDATE STATISTICS [' + OBJECT_SCHEMA_NAME(@TblId, @DBId)
-- + '].[' + OBJECT_NAME(@TblId, @DBId) + '] [' + i.name + +'];' AS [Rebuild Script]
--FROM sys.dm_db_index_physical_stats(@DBId, @TblId, NULL, NULL, 'SAMPLED') AS ps
-- INNER JOIN sys.indexes i
-- ON ps.object_id = i.object_id
-- AND ps.index_id = i.index_id
-- LEFT OUTER JOIN @tmp_compression tmp
-- ON i.index_id = tmp.index_id
--WHERE i.object_id = @TblId
--AND i.is_disabled = 0
--AND i.is_hypothetical = 0
--ORDER BY i.index_id
-- ,i.name
--OPTION (MAXDOP 2);
--/****** INDEX SIZE BLOCK END ******/

/****** POSSIBLE MISSING INDEXES BLOCK START ******/
SELECT 'POSSIBLE MISSING (since last restart):' AS Info;
Expand Down
Loading

0 comments on commit a06d068

Please sign in to comment.