Change Tracking
This lesson explains how Change Tracking records changed rows, how to query changes and its limits.
Learning outcomes
After completing this lesson, you will be able to:
- Describe what Change Tracking records.
- Explain how CHANGETABLE and version numbers support synchronization.
- Identify the limits of Change Tracking.
How Change Tracking works
Change Tracking records, synchronously with each DML statement, the primary key of each changed row, the operation (insert, update or delete) and, optionally, which columns were updated, with a database-wide version number. It does not record the changed values or how many times a row changed; to get current values, a client joins the change information back to the table. Tables must have a primary key. Overhead is low, which makes Change Tracking a good fit for synchronization, cache invalidation and re-embedding changed rows.
ALTER DATABASE CURRENT SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);
ALTER TABLE Catalog.Product ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ON);What Change Tracking records
Querying changes
A client stores the version it last synchronized. CHANGETABLE(CHANGES table, @last_version) returns the keys changed since then with SYS_CHANGE_OPERATION (I, U or D) and SYS_CHANGE_VERSION, and CHANGE_TRACKING_CURRENT_VERSION() gives the version to store for the next run. If the stored version is older than CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID('table')), cleanup has removed the needed history and the client must resynchronize fully. Snapshot isolation around the read keeps the version and data consistent.
DECLARE @last bigint = 1500; -- stored from the previous sync
SELECT ct.ProductID, ct.SYS_CHANGE_OPERATION, p.Name, p.Description
FROM CHANGETABLE(CHANGES Catalog.Product, @last) AS ct
LEFT JOIN Catalog.Product AS p ON p.ProductID = ct.ProductID; -- NULL for deleted rows
SELECT CHANGE_TRACKING_CURRENT_VERSION() AS NextSyncVersion;An incremental sync
Uses and limits
Change Tracking underpins the Azure Functions SQL trigger binding, offline and mobile synchronization, and incremental jobs such as refreshing embeddings for changed products. Its limits are that it provides no history of intermediate values, requires a primary key, and needs clients to sync within the retention period. When old and new values are needed, CDC is the right feature.
Re-embedding changed products
A job must regenerate embeddings every 10 minutes for products whose descriptions were inserted or updated, and remove embeddings of deleted products. It does not need previous description values, and overhead on the busy product table must be minimal. Which feature should the job use?
Enable Change Tracking on the Product table with TRACK_COLUMNS_UPDATED, and have the job read CHANGETABLE(CHANGES ...) since its stored version, re-embed inserted and updated rows (optionally only when the description column changed, using CHANGE_TRACKING_IS_COLUMN_IN_MASK), and delete embeddings for D operations. CDC would also work but captures full before and after values that the job does not need, and a trigger would add work to every write.
What must a client do if its last sync version is older than CHANGE_TRACKING_MIN_VALID_VERSION?
Show answer
Resynchronize fully, because the change history it needs was cleaned up.
Exam Tips
Remember for the exam
- Which rows changed since the last sync, low overhead: Change Tracking.
- Change Tracking stores keys and operations, not values; join back for current data.
- Requires a primary key; sync within the retention period or resynchronize.
- Query with CHANGETABLE(CHANGES ...) and CHANGE_TRACKING_CURRENT_VERSION().