This blog post is intended to give you some idea on how Change Tracking is used when exporting data through entities in Data Management.
It is not
going to explain the basic principles of SQL Server Change Tracking (see About Change Tracking) and how the simple task of
configuring the Data Management export projects, which I assume you either
already know or can read about elsewhere (see Enable Change Tracking for entities).
I will try
to explain what happens behind the scenes when the configuration is enabled or
changed, and when data is exported.
Tables configured with Change Tracking
sct1.name AS CT_schema,
sot1.name AS CT_table,
ps1.row_count AS CT_rows,
ps1.reserved_page_count*8./1024. AS CT_reserved_MB,
sct2.name AS tracked_schema,
sot2.name AS tracked_name,
ps2.row_count AS tracked_rows,
ps2.reserved_page_count*8./1024. AS tracked_base_table_MB,
change_tracking_min_valid_version(sot2.object_id) AS min_valid_version
JOIN sys.schemas AS sct1 ON sot1.schema_id=sct1.schema_id
JOIN sys.dm_db_partition_stats ps1 ON it.object_id = ps1. object_id AND ps1.index_id in (0,1)
LEFT JOIN sys.objects sot2 ON it.parent_object_id=sot2.object_id
LEFT JOIN sys.schemas AS sct2 ON sot2.schema_id=sct2.schema_id
LEFT JOIN sys.dm_db_partition_stats ps2 ON sot2.object_id = ps2. object_id AND ps2.index_id in (0,1)
WHERE it.internal_type IN (209, 210)
ORDER BY Tracked_Name
This script lists all tables in the current database which have been activated for change tracking. We see that only the CustGroup table is currently enabled.
- Syscommittab holds information on all changes on a high level and a pointer to the details in the side tables
- Side tables with the naming conversion “change_tracking_[unique ID]”
Change Tracking level on data entities
Enabling a
data entity for Change Tracking enables the individual tables in the entity
based on the Change Tracking level.
Consider
the data entity “Fleet Management Rentals” (FMRentalEntity) which consist of
three tables: FMRental, FMCustomer and FMVehicle
Primary table
Entire
entity
Enabling
Change Tracking for Entire entity enables all tables of the entity data source.
Custom
Enabling
Change Tracking for Custom query enables the tables of the entity data source
that are added by code to the default Change Tracking query.
The sample
below adds the two tables FMRental and FMCustomer to the query, making them the
ones that are change tracked. This means only changes to these two tables will trigger
and export of the FMRental entity records.
Disabling change tracking
Change Tracking on incremental export
Adding a
data entity to a Data Management export project for the first time with Default
Refresh Type = Incremental and running the initial export, creates a record in
the table DMFEntityDbSyncVersion. This record holds the latest synchronization
version for the current data entity in the Definition group in each legal
entity. So, the key is: Definition group, Data area id, Data entity name.
After each export,
the synchronization version number is set to the current database Change
Tracking version.
So, next time the export is run, the current synchronization version for the data entity is validated against the change tracking side table versions for each table in the entity data source marked for Change Tracking. If records exist with higher version numbers they are considered changed and will be exported.
Querying current tracked changes
After
creating two new rentals in the FMRental table we can check out what is
expected to be exported in the next run by issuing the following script:
SELECT @last_sync_version AS LAST_SYNC_VERSION,SYS_CHANGE_VERSION, FMRental.* FROM FMRental
First the @last_sync_version variable is initialized with the current synchronization version that the definition group has. This is used to compare against the current tracked changes version number.
Again, the definition group is marked with the current highest change tracking number, so the same records are not exported next time.