question

sunilreddyat avatar image
sunilreddyat asked

Enabling and Disabling CDC

Hi I want disable CDC feature on a table and enable it after doing some bulk DML operations(To gain performance). As CDC drops change table and function related to that table, I want the change table to be exists even if I disable the CDC on that particular table. Can you suggest me a good approach. I tried to edit the disable stored proc but in vain. Than, I tried to load the tracked records into a temp table while disabling the CDC and loading into change table after enabling it again. I need a better approach as I have lot of tables to do like that..
sql-server-2008change-data-capture
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sunilreddyat avatar image sunilreddyat commented ·
the other way is to stop the execution of 'capture' job while running bulk DML operations and start the 'capture' job after the process. But after re-enabling the 'capture' job, it will track all the transactional logs during the bulk operations also...can we customize the 'Capture' job and make not to track the previous changes when the job is re-started again...
0 Likes 0 ·
WilliamD avatar image WilliamD commented ·
Again - CDC is designed to track **all** changes - trying to modify that system to fit your needs is **not** the way to achieve your goal. Read through my original answer again to see what you should do.
0 Likes 0 ·
Usman Butt avatar image
Usman Butt answered
One simple solution could be to take the backup, which already should be the case as you are doing some bulk DML operations, and then restore it as another Database. After the bulk operation, enable CDC and follow what were you doing before. Since the table names would be the same, it would not be that much difficult to load the data back. For your script demand, below is a preliminary script, which you should test thoroughly before using. At the moment this only will print the INSERTION commands. My assumption is that you have taken the backup and restored it as another DB with option "`KEEP_CDC`" ON. Moreover, it is assumed that there are no columns dropped from CDC while re-enabling (An addition of a column will be catered hopefully). You need to run this script on the restored DB and need to act according to the comments made in the script. Furthermore, this does not handle the tables **cdc.ddl_history** and **cdc.index_columns**, which I believe you can handle manually. If you want to stick to the staging table approach on the same DB, then you can tweak it easily. One suggestion could be to CREATE the staging tables by appending a Prefix/Post fix to the original tracking tables (not tracked ones). Hopefully, it will help you. **SCRIPT** SET NOCOUNT ON ; CREATE TABLE #cdc_tables -- TO CAPTURE THE OUTPUT OF sp_cdc_help_change_data_capture ( source_schema SYSNAME ,source_table SYSNAME ,capture_instance SYSNAME ,object_id INT ,source_object_id INT ,start_lsn BINARY(10) ,end_lsn BINARY(10) ,supports_net_changes BIT ,has_drop_pending BIT ,role_name SYSNAME NULL ,index_name SYSNAME NULL ,filegroup_name SYSNAME NULL ,create_date DATETIME ,index_column_list NVARCHAR(MAX) NULL ,captured_column_list NVARCHAR(MAX) NULL ) DECLARE @DSQL NVARCHAR(MAX) , @capture_instance SYSNAME , @captured_column_list NVARCHAR(MAX) INSERT [#cdc_tables] -- INSERTION EXEC sys.sp_cdc_help_change_data_capture DECLARE CDCCursor CURSOR FAST_FORWARD FOR SELECT [capture_instance], captured_column_list FROM [#cdc_tables] AS CT OPEN CDCCursor FETCH NEXT FROM CDCCursor INTO @capture_instance, @captured_column_list WHILE @@FETCH_STATUS = 0 BEGIN /*== A SIMPLE APPROACH WITH NO COLUMN NAMES COULD BE ====*/ --'INSERT INTO YOURPRODUCTIONDBNAME.cdc.'+ QUOTENAME([capture_instance] + '_CT') + ' SELECT * FROM cdc.'+ QUOTENAME([capture_instance] + '_CT') -- CHANGE NAME HERE IF USING STAGING TABLES /*== USE THIS IF YOU FEEL COMFORTABLE WITH IT. I DID NOT TEST IT*/ /*== COLUMN NAMES ARE SPECIFIED IN THIS BELOW APPROACH. ONE THING GOOD WITH IT IS IF YOU HAVE ADDED MORE COLUMNS FOR CHANGE TRACKING THIS HOPEFULLY WILL NOT FAIL. BUT IF SOME COLUMNS ARE REMOVED, THEN IT WILL NOT CATER ==*/ SET @DSQL = 'INSERT INTO '+ 'YOURPRODUCTIONDBNAME' + -- PUT YOUR PRODUCTION DB NAME HERE '.cdc.' + QUOTENAME(@capture_instance + '_CT') + ' ([__$start_lsn], [__$end_lsn], [__$seqval], [__$operation],[__$update_mask], ' -- THESE ARE THE COLUMNS UNIVERSAL IN ALL TRACKING TABLES + @captured_column_list + ')' + ' SELECT [__$start_lsn], [__$end_lsn], [__$seqval], [__$operation],[__$update_mask], ' -- AGAIN UNIVERSAL COLUMNS IN ALL TRACKING TABLES + @captured_column_list + ' FROM ' + QUOTENAME(DB_NAME()) + '.cdc.' + QUOTENAME(@capture_instance + '_CT') -- CHANGE NAME HERE IF USING STAGING TABLES PRINT @DSQL --EXEC (@DSQL) -- UNCOMMENT IF YOU WANT TO EXECUTE FETCH NEXT FROM CDCCursor INTO @capture_instance, @captured_column_list END DROP TABLE [#cdc_tables] CLOSE CDCCursor DEALLOCATE CDCCursor
10 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

sunilreddyat avatar image sunilreddyat commented ·
Hi usman, am doing bulk DML operation using SSIS packages which are scheduled, after executing the packages the DB will be used by the web Application for which CDC is must. So in this case I can't take DB backup...
0 Likes 0 ·
Usman Butt avatar image Usman Butt commented ·
Don't tell me you have scheduled SSIS packages and you cannot schedule a backup before :)
0 Likes 0 ·
sunilreddyat avatar image sunilreddyat commented ·
Plz, I hav scheduled SSIS packages, so backup is nt the right option I think..
0 Likes 0 ·
Usman Butt avatar image Usman Butt commented ·
Can you please specify what is the problem? Any environment specific constraints?
0 Likes 0 ·
sunilreddyat avatar image sunilreddyat commented ·
I jus enabled CDC on my DB to track DML operations from web application, I also created SSIS packages for batch processing. during the execution of SSIS packages I want to disable CDC for performance gain and after execution of SSiS packages finish I need to enable again. But I don't want to loose change table data..
0 Likes 0 ·
Show more comments
WilliamD avatar image
WilliamD answered
CDC is not designed to do what you are wanting. You can transfer the CDC data into permanent user tables for future reference and offer that up to the web application. This way you can disable CDC for the data load and re-enable it afterwards and lose no data. You will then have to devise a system to periodically transfer the CDC data into the permanent tables and clean these tables up (if necessary).
6 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image WilliamD commented ·
It is not a suggested method for making the system do what you want. CDC is built using system objects. Run an SP install or hotfix and their behaviour may have been changed by MS without your knowledge. That could then break your modified version of CDC. I strongly advise against doing something like that. If you are already considering changing system objects, you are already digging deep enough to implement the solution I suggested - which is far more stable than modifying system code supplied by MS.
1 Like 1 ·
sunilreddyat avatar image sunilreddyat commented ·
yeah,previously am following the second option, but I want a better approach than this...
0 Likes 0 ·
WilliamD avatar image WilliamD commented ·
As far as I am aware, this is the only way of keeping the CDC data between deactivating and activating this feature.
0 Likes 0 ·
sunilreddyat avatar image sunilreddyat commented ·
is there any chance by editing the change table /system procedure(which disable's the CDC feature)
0 Likes 0 ·
sunilreddyat avatar image sunilreddyat commented ·
I thought that too...atlast I found no way other than taking the records of change_table into temp table and disable the CDC... After enabling CDC i need to reload those records again into Change_table from Temp table
0 Likes 0 ·
Usman Butt avatar image Usman Butt commented ·
@WilliamD +1. You have point out the most critical factor. This is where I think a backup/restore would be a better option.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.