Change Tracking Cleanup–Part 1

Published Mar 23 2019 02:55 PM 3,208 Views
Microsoft
First published on MSDN on Oct 19, 2016

Part 2 of the series is available here.

Change tracking is a lightweight solution that provides an efficient change tracking mechanism for applications which was introduced in SQL Server 2008. We recently had a number of customers ask us about how Change Tracking Cleanup works and how they can troubleshoot further if the cleanup is not working as expected. In the first part of this blog post, I will explain how Change Tracking cleanup works and what " information " is cleaned up by the automatic cleanup task. I will also touch upon what enhancements were shipped in SQL Server 2014 and above to help cleanup more efficiently.

Change Tracking cleanup is invoked automatically every 30 minutes. The default retention period is 2 days. An example of setting the automatic cleanup for Change Tracking information is shown below.

ALTER DATABASE <DBNAME> SET CHANGE_TRACKING = ON  (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)

Automatic Cleanup

Each table that is enabled for Change Tracking has an internal table (a.k.a. side table with the naming convention: change_tracking_<#> ) which is used by Change Tracking functions to determine the change version and the rows that have changed since a particular version. Every time the automatic cleanup thread wakes up, it scans all the user databases on the SQL Server instance to identify the change tracking enabled databases. Based on the retention period setting of the database, each side table is purged of its expired records. The automatic cleanup removes rows from the on-disk tables based on the retention period defined for the database.

Change tracking information is stored for all tables (enabled for Change Tracking) in a database in an in-memory rowstore ( syscommittable ). This in-memory rowstore is flushed every checkpoint to the on-disk table ( syscommittab ). Rows from the syscommittab internal table are removed during every checkpoint.

Manual Cleanup

In SQL Server 2014 Service Pack 2 and above, we provided a new Stored Procedure, sp_flush_CT_internal_table_on_demand , to assist with Change Tracking cleanup. KB3173157 has more details. This stored procedure accepts a table name as parameter and will attempt to cleanup records from the corresponding change tracking internal table.  During the course of the deletion, it will print some verbose in the output window about the progress of deletion.

In case you want to automate the cleanup for all tables, you can use a while loop to execute this stored procedure against all the tables or tables that receive a high number of changes to prevent automatic cleanup from lagging in cleaning up records from the Change Tracking internal tables. A sample manual cleanup T-SQL script is available on the tigertoolbox GitHub repo: ChangeTrackingCleanup.sql (see screenshot below).

- Amit Banerjee ( @banerjeeamit )

Sr. Program Manager

%3CLINGO-SUB%20id%3D%22lingo-sub-384861%22%20slang%3D%22en-US%22%3EChange%20Tracking%20Cleanup%E2%80%93Part%201%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-384861%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Oct%2019%2C%202016%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fblogs.msdn.microsoft.com%2Fsql_server_team%2Fchange-tracking-cleanup-part-2%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20%3CEM%3E%20Part%202%20%3C%2FEM%3E%20%3C%2FA%3E%20%3CEM%3E%20of%20the%20series%20is%20available%20here.%20%3C%2FEM%3E%3C%2FP%3E%0A%20%20%3CP%3EChange%20tracking%20is%20a%20lightweight%20solution%20that%20provides%20an%20efficient%20change%20tracking%20mechanism%20for%20applications%20which%20was%20introduced%20in%20SQL%20Server%202008.%20We%20recently%20had%20a%20number%20of%20customers%20ask%20us%20about%20how%20Change%20Tracking%20Cleanup%20works%20and%20how%20they%20can%20troubleshoot%20further%20if%20the%20cleanup%20is%20not%20working%20as%20expected.%20In%20the%20first%20part%20of%20this%20blog%20post%2C%20I%20will%20explain%20how%20Change%20Tracking%20cleanup%20works%20and%20what%20%22%20%3CEM%3E%20information%20%3C%2FEM%3E%20%22%20is%20cleaned%20up%20by%20the%20automatic%20cleanup%20task.%20I%20will%20also%20touch%20upon%20what%20enhancements%20were%20shipped%20in%20SQL%20Server%202014%20and%20above%20to%20help%20cleanup%20more%20efficiently.%3C%2FP%3E%0A%20%20%3CP%3EChange%20Tracking%20cleanup%20is%20invoked%20automatically%20every%2030%20minutes.%20The%20default%20retention%20period%20is%202%20days.%20An%20example%20of%20setting%20the%20automatic%20cleanup%20for%20Change%20Tracking%20information%20is%20shown%20below.%3C%2FP%3E%0A%20%20%3CP%3E%3CEM%3E%20%3CSTRONG%3E%20ALTER%20DATABASE%20%3CDBNAME%3E%20SET%20CHANGE_TRACKING%20%3D%20ON%26nbsp%3B%20(CHANGE_RETENTION%20%3D%202%20DAYS%2C%20AUTO_CLEANUP%20%3D%20ON)%20%3C%2FDBNAME%3E%3C%2FSTRONG%3E%20%3C%2FEM%3E%3C%2FP%3EAutomatic%20Cleanup%3CP%3EEach%20table%20that%20is%20enabled%20for%20Change%20Tracking%20has%20an%20internal%20table%20(a.k.a.%20%3CSTRONG%3Eside%20table%20%3C%2FSTRONG%3E%20with%20the%20naming%20convention%3A%20%3CSTRONG%3E%20change_tracking_%26lt%3B%23%26gt%3B%20%3C%2FSTRONG%3E%20)%20which%20is%20used%20by%20Change%20Tracking%20functions%20to%20determine%20the%20change%20version%20and%20the%20rows%20that%20have%20changed%20since%20a%20particular%20version.%20Every%20time%20the%20automatic%20cleanup%20thread%20wakes%20up%2C%20it%20scans%20all%20the%20user%20databases%20on%20the%20SQL%20Server%20instance%20to%20identify%20the%20change%20tracking%20enabled%20databases.%20Based%20on%20the%20retention%20period%20setting%20of%20the%20database%2C%20each%20side%20table%20is%20purged%20of%20its%20expired%20records.%20The%20automatic%20cleanup%20removes%20rows%20from%20the%20on-disk%20tables%20based%20on%20the%20retention%20period%20defined%20for%20the%20database.%3C%2FP%3E%0A%20%20%3CP%3EChange%20tracking%20information%20is%20stored%20for%20all%20tables%20(enabled%20for%20Change%20Tracking)%20in%20a%20database%20in%20an%20in-memory%20rowstore%20(%20%3CSTRONG%3E%20syscommittable%20%3C%2FSTRONG%3E%20).%20This%20in-memory%20rowstore%20is%20flushed%20every%20checkpoint%20to%20the%20on-disk%20table%20(%20%3CSTRONG%3E%20syscommittab%20%3C%2FSTRONG%3E%20).%20Rows%20from%20the%20syscommittab%20internal%20table%20are%20removed%20during%20every%20checkpoint.%3C%2FP%3EManual%20Cleanup%3CP%3EIn%20SQL%20Server%202014%20Service%20Pack%202%20and%20above%2C%20we%20provided%20a%20new%20Stored%20Procedure%2C%20%3CSTRONG%3E%20sp_flush_CT_internal_table_on_demand%20%3C%2FSTRONG%3E%20%2C%20to%20assist%20with%20Change%20Tracking%20cleanup.%20%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Fkb%2F3173157%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EKB3173157%20%3C%2FA%3E%20has%20more%20details.%20This%20stored%20procedure%20accepts%20a%20table%20name%20as%20parameter%20and%20will%20attempt%20to%20cleanup%20records%20from%20the%20corresponding%20change%20tracking%20internal%20table.%26nbsp%3B%20During%20the%20course%20of%20the%20deletion%2C%20it%20will%20print%20some%20verbose%20in%20the%20output%20window%20about%20the%20progress%20of%20deletion.%3C%2FP%3E%0A%20%20%3CP%3EIn%20case%20you%20want%20to%20automate%20the%20cleanup%20for%20all%20tables%2C%20you%20can%20use%20a%20while%20loop%20to%20execute%20this%20stored%20procedure%20against%20all%20the%20tables%20or%20tables%20that%20receive%20a%20high%20number%20of%20changes%20to%20prevent%20automatic%20cleanup%20from%20lagging%20in%20cleaning%20up%20records%20from%20the%20Change%20Tracking%20internal%20tables.%20A%20sample%20manual%20cleanup%20T-SQL%20script%20is%20available%20on%20the%20%3CSTRONG%3E%20tigertoolbox%20%3C%2FSTRONG%3E%20GitHub%20repo%3A%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMicrosoft%2Ftigertoolbox%2Fblob%2Fmaster%2Fchange-tracking%2FChangeTrackingCleanup.sql%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20ChangeTrackingCleanup.sql%20%3C%2FA%3E%20(see%20screenshot%20below).%3C%2FP%3E%0A%20%20%3CP%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F98109i1BBD67878C417A12%22%20%2F%3E%3C%2FP%3E%0A%20%20%3CP%3E-%20Amit%20Banerjee%20(%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fbanerjeeamit%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20%40banerjeeamit%20%3C%2FA%3E%20)%3C%2FP%3E%0A%20%20%3CP%3ESr.%20Program%20Manager%3C%2FP%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-384861%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Oct%2019%2C%202016%20Part%202%20of%20the%20series%20is%20available%20here.%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-384861%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESQLServerTiger%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Mar 23 2019 02:55 PM
Updated by: