In my earlier articles, I discussed & demonstrated all basic activities in Change Data Capture (CDC) in any SQL Server database. However, I did not discuss about the retention period of CDC data in the database. In most cases, we need to modify the retention period due to our business requirements. I came across this query many times, if we can modify retention period, if Yes, how ?
BY DEFAULT, CDC configure the data retention period of 3 days. In other words, CDC keeps all the data changes history for 3 days ONLY and the rest will be cleaned (deleted). In most of my clients, I configured CDC for few tables with the retention period of 10 days and few of them leave it as a default (3 days).
Let me explain how to view / modify the retention period of CDC from in few easy steps.
Step 1 – View the existing RETENTION period in CDC:
First of all, you should check the existing retention period of CDC. It is also important to know that CDC keeps the retention period in minutes. Given below script will show the retention period in minutes as well as days by using dbo.cdc_jobs (a change data capture system table).
--This script is compatible with SQL Server 2008 and above. USE msdb GO SELECT [retention] As [Retention period in minutes] ,[retention]/60/24 As [Retention period in days] FROM dbo.cdc_jobs WHERE job_type ='cleanup' GO --OUTPUT
Step 2 – Modify the RETENTION period in CDC :
As you can see in the above result that the retention period is 3 days (4320 minutes). Lets modify it to 10 days by using sys.sp_cdc_change_job (a system stored procedure of CDC).
--This script is compatible with SQL Server 2008 and above. USE AdventureWorks2012 GO DECLARE @New_retention_period_in_minutes AS SMALLINT DECLARE @New_retention_period_in_days AS TINYINT --Set the retention period for 10 days SET @New_retention_period_in_days = 10 --Convert 10 days into minutes SET @New_retention_period_in_minutes= @New_retention_period_in_days*60*24 --Select the total number of minutes in 10 days to check. SELECT @New_retention_period_in_minutes As [Retention period in minutes] --Update minutes in the CDC job EXECUTE sys.sp_cdc_change_job @job_type = N'cleanup', @retention = @New_retention_period_in_minutes; GO --OUTPUT
Step 3 – Verify the RETENTION period in CDC:
You need to execute the same script as Step 1 but the output would be different this time, as we have successfully updated the retention period to 10 days in Step 2.
--This script is compatible with SQL Server 2008 and above. USE msdb GO SELECT [retention] As [Retention period in minutes] ,[retention]/60/24 As [Retention period in days] FROM dbo.cdc_jobs WHERE job_type ='cleanup' GO --OUTPUT
An update of my blog is available at my twitter or you can like my Facebook page or subscribe via email by mentioning your email address in the ‘follow blog’ section.
Hi Imran,
I highly impressed and appreciated your good work.
I have couple of doubts regarding CDC.
1. If CDC enabled on a database, will it effect on database performance.
2. If yes, how to tune performance.
3. If RETENTION period reduces(no.of days), is it increases the performance?
Thanks in advance for your valuable support.
Pavan