When SQL Server 2008 launched several people focused on the Change Data Capture feature. I was one of them as the functionality provided allowed me to leave triggers and all their associated craziness behind (for the most part). Another similar feature that seemed to fly under the radar was Change Tracking. At first, you might see these two features and roll your eyes…another duplication of functionality by Microsoft. Not really. The two features answer very different questions when it comes to changing data in a database application.
Change Data Capture
This feature has been discussed pretty thoroughly on the the blog circuit, including this one. You can see my original post here for a full description of Change Data Capture.
Change Tracking
As the name implies, Change Tracking is more interested in tracking the fact that a change occurred, not so much in storing said change. This makes the feature more suited for applications that only need to synchronize or replicate data. Disconnected systems are an example of the application types that will benefit from Change Tracking.
Change Tracking is enabled initially at the database level via ALTER DATABASE syntax:
ALTER DATABASE AdventureWorks
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);
Both the CHANGE_RETENTION and AUTO_CLEANUP options are configurable to your needs.
Next, we must enable Change Tracking for each table that we want tracked. This is accomplished through ALTER TABLE syntax:
ALTER TABLE Sales.OrderDetail
ENABLE CHANGE_TRACKING
WITH (TRACK_COLUMNS_UPDATED = ON);
When TRACK_COLUMNS_UPDATED option is set to ON the Database Engine stores each column from the table that took part in the DML action. This can improve the synchronization activities of your application can specify only the update columns. Because the setting adds extra storage overhead it is off by default.
If at some point we no longer wish to track changes for an object or database we simply reverse our steps with the following ALTER statements:
ALTER TABLE Sales.OrderDetail
DISABLE CHANGE_TRACKING;
ALTER DATABASE AdventureWorks
SET CHANGE_TRACKING = OFF;
Key Differences Between Change Data Capture and Change Tracking
The implementation of the two features are quite different in some areas and need to be fully understood before employing them in your database. The table below lists what I consider the more important differences.
| Feature |
Change Data Capture |
Change Tracking |
| DML changes |
Yes |
Yes |
| Style of processing |
Asynchronous |
Synchronous |
| Historical data |
Yes |
No |
| DML change type |
Yes |
Yes |
| Requires SQL Agent |
Yes |
No |
| Requires snapshot isolation level |
No |
Recommended |
| Allows placement of tracking objects |
Yes |
No |
| Restrictions on DDL |
No |
Yes |
If you do your research and find the correct style of processing for your application you will greatly enjoy these two new features. I will be using Change Data Capture for my current client and will post a follow up blog about our experiences.
e2ec3c6a-c106-469a-a426-ea7fef00fb36|0|.0