Microsoft provides excellent documentation on change tracking here. Microsoft has done a good job of retaining a lot of documentation for versions of SQL Server prior to 2014, but there are some gaps. Change tracking happily hasn't changed much since it was introduced in SQL Server 2008, so most of this information is valid for SQL Server 2012, 2008R2, and 2008.
SQL Server 2014 SP2 does introduce a documented stored procedure that you can invoke to clean up the internal change table (and don't worry, we'll cover this in due course!), but that's about it as far as documented functional changes go (note: if any of you hardcore internals folks know of under-the-hood changes between versions, even if they work the same on the surface, please let me know! I love that kind of detail!). That said, there's always the chance that future versions will introduce differences, so keep that in mind.
Anyway, moving forward! Here is a quick overview of change tracking:
- The change source is an in-memory rowstore, flushed to disk with each checkpoint
- It answers two questions:
- Has a row changed?
- What rows have changed?
- The only values from the source table stored in the change table are the primary key values for the changed rows
- You have to use the built-in functions to get at the change data
- The database you want to track changes in must have a compatibility level of at least 90
- Filed under 'ways the database engine will let you score an own goal', SQL Server will still let you enable change tracking on a database if the compatibility level is lower than 90, but the functions used to retrieve change data will give you an error
Change tracking is a fairly simple tool to set up and use. In order to enable change tracking, you must first enable it at the database level via an ALTER DATABASE statement, and then at the table level via an ALTER TABLE statement.
Let's begin at the database level. The ALTER DATABASE statement looks something like...
There aren't a lot of parameters here. You can set change tracking on or off, you can specify your retention period, and you can specify whether to enable auto-cleanup or not.
For the retention period, you have the choice of DAYS, HOURS, or MINUTES. If you don't specify retention, SQL Server will configure change tracking with a retention period of 2 days. If you provide a number but don't specify the interval, SQL Server will default to days. The minimum retention period is 1 minute.
For auto-cleanup, if you specify OFF SQL Server will not automatically clean up change tracking data. If you don't specify auto-cleanup, SQL Server will default to ON. Unless you want to be responsible for cleaning up after change tracking, ON is your best bet.
To see which databases have change tracking enabled, and to look at their configuration, query the sys.change_tracking_databases catalog view:
It covers the basics, but it could be more informative. Kendra Little has a nicer script:
For our simple example, it gives us:
By joining on sys.databases to provide the database name and omitting the retention_period_units in favor of the description, Kendra's script gives us much more readable results. You'll notice that this script omits the max_cleanup_version column. We'll come back to that, and when it's useful, in a future post.
As an aside, while it can sometimes be a pain to script for readability, it almost always pays off in the long run.
Disabling change tracking is similarly simple. As with enabling change tracking, you disable it with a very straightforward ALTER DATABASE command:
You will need to disable change tracking on all tables before disabling it at the database level, but as we haven't looked at the table level commands yet we'll just pretend we've already done that.
You can change the database-level settings for change tracking with a similar ALTER DATABASE script. Just omit the = ON or OFF:
You can modify either or both the retention period and the auto-cleanup setting.
That wraps it up for this post! Join me next time when we look at enabling change tracking at the table level!