Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHANGE DATA CAPTURE & CHANGE TRACKING DEEP DIVE W. Kevin Hazzard LinchpinPeople.com Group Principal.

Similar presentations


Presentation on theme: "CHANGE DATA CAPTURE & CHANGE TRACKING DEEP DIVE W. Kevin Hazzard LinchpinPeople.com Group Principal."— Presentation transcript:

1 CHANGE DATA CAPTURE & CHANGE TRACKING DEEP DIVE W. Kevin Hazzard LinchpinPeople.com Group Principal

2 CHANGE DATA CAPTURE Turn it on EXECUTE sys.sp_cdc_enable_db; Turn it on EXECUTE sys.sp_cdc_enable_db; Which generates for the whole database Which generates for the whole database A capture job A capture job A cleanup job A cleanup job Then include at least one table with EXECUTE sys.sp_cdc_enable_table …; Then include at least one table with EXECUTE sys.sp_cdc_enable_table …; Which creates a capture instance containing Which creates a capture instance containing A change table A change table An all-changes function An all-changes function A net-changes function (optional) A net-changes function (optional) transactions warehouse sources changes capture job archive sector ETL analysis data mart ETL

3 CAPTURE INSTANCE Contains Contains The capture table The capture table cdc.fn_cdc_get_all_changes_ function cdc.fn_cdc_get_all_changes_ function cdc.fn_cdc_get_net_changes_ function (optional) cdc.fn_cdc_get_net_changes_ function (optional) Maximum two per source table Maximum two per source table

4 @capture_instance

5 DEMONSTRATION Enabling Change Data Capture (CDC) Creating a CDC Capture Instance Querying CDC Tables as Things Change

6 SQL SERVER AGENT JOBS cdc.Capture_capture cdc.Capture_capture Starts with SQL Agent Starts with SQL Agent RAISERROR(22801, 10, -1); RAISERROR(22801, 10, -1); EXEC sys.sp_MScdc_capture_job; EXEC sys.sp_MScdc_capture_job; Just a wrapper for sys.sp_cdc_scan Just a wrapper for sys.sp_cdc_scan cdc.Capture_cleanup cdc.Capture_cleanup Runs at 02:00 daily Runs at 02:00 daily EXEC sys.sp_MScdc_cleanup_job; EXEC sys.sp_MScdc_cleanup_job;

7 CDC DATA RETENTION Based on LSN Validity Intervals Based on LSN Validity Intervals Database Database Capture Instance Capture Instance The cleanup job deletes CDC data acording to retention policy SELECT * FROM msdb.dbo.cdc_jobs WHERE job_type = N'cleanup'; The cleanup job deletes CDC data acording to retention policy SELECT * FROM msdb.dbo.cdc_jobs WHERE job_type = N'cleanup'; You can start the cleanup job manually EXEC = N'cleanup'; You can start the cleanup job manually EXEC = N'cleanup';

8 HANDLING SCHEMA CHANGES Deleted and new columns are handled well Deleted and new columns are handled well Modified columns require specific steps: Modified columns require specific steps: Stop the capture and cleanup jobs Stop the capture and cleanup jobs Change the schema as necessary Change the schema as necessary Generate new capture instances for all modified tables Generate new capture instances for all modified tables Process all data in the old and new capture instances Process all data in the old and new capture instances Manually run the cleanup job Manually run the cleanup job Delete the old capture instance Delete the old capture instance Turn the capture and cleanup jobs back on Turn the capture and cleanup jobs back on

9 CHANGE TRACKING Turn it on for one database ALTER DATABASE SET CHANGE_TRACKING = ON; Turn it on for one database ALTER DATABASE SET CHANGE_TRACKING = ON; Then enable a table ALTER TABLE ENABLE CHANGE_TRACKING; Then enable a table ALTER TABLE ENABLE CHANGE_TRACKING; transactions warehouse sources archive sector ETL sync

10 CHANGE TRACKING TABLES & FUNCTIONS sys.change_tracking_databases sys.change_tracking_databases sys.change_tracking_tables sys.change_tracking_tables CHANGETABLE(CHANGES) CHANGETABLE(CHANGES) CHANGETABLE(VERSION) CHANGETABLE(VERSION) CHANGE_TRACKING_CURRENT_VERSION() CHANGE_TRACKING_CURRENT_VERSION() CHANGE_TRACKING_MIN_VALID_VERSION() CHANGE_TRACKING_MIN_VALID_VERSION() CHANGE_TRACKING_IS_COLUMN_IN_MASK() CHANGE_TRACKING_IS_COLUMN_IN_MASK() WITH CHANGE_TRACKING_CONTEXT WITH CHANGE_TRACKING_CONTEXT

11 DEMONSTRATION Enabling Change Tracking (CT) for a Database Enabling CT on a Table Querying CT Tables and Functions as Things Change

12 HANDLING SCHEMA CHANGES No modifications to the primary key are allowed including related indexes No modifications to the primary key are allowed including related indexes Dropping columns is OK but they may still appear in the change data Dropping columns is OK but they may still appear in the change data When adding columns, changes are tracked but the metadata change is not reported When adding columns, changes are tracked but the metadata change is not reported Switching partitions will fail on change tracked changes Switching partitions will fail on change tracked changes Data type changes are not tracked Data type changes are not tracked

13 WHICH ONE IS RIGHT FOR ME? Change Data Capture Works in Enterprise Edition only Stores every discrete change Storage intensive Good for auditing Requires SQL Server agent No special serialization required Change Tracking Works in all versions of SQL Server Returns differences from current Storage light Good for device synchronization No job agent required Operates best with snapshot isolation


Download ppt "CHANGE DATA CAPTURE & CHANGE TRACKING DEEP DIVE W. Kevin Hazzard LinchpinPeople.com Group Principal."

Similar presentations


Ads by Google