Friday, September 28, 2018

AX 2012 - Change tracking huge performance cost - How to disable on SQL Level

I have seen one of the AOS instance, where DB size was grown very huge and performance was a big issue. On investigation, it was found that SQL Production Db was enabled for change tracking for almost 15 days for all the tables. This was disastrous when you have not selectively enabled for necessary tables, but rather for all the tables without any usefulness. Imagine a production database for a retail industry where thousands of transactions daily are inserted in the db.  

However, you cannot simply disable on DB level as shown below, rather you need to run SQL script on production database to disable change tracking firstly on each table level.


Following SQL script is useful for disabling each table level change tracking. However, please bear in mind that firstly stop the AOS prior to running the script, otherwise, the consequence might be DB may go to Recovery state which will take hours to recover.

SQL script to disable change tracking is as follows:

Use [Production DB name];
GO

DECLARE @SQL NVARCHAR(MAX) = '';
SELECT @SQL = @SQL + 'ALTER TABLE ' + s.name + '.' + t.name + 
             ' Disable Change_tracking;' + CHAR(10)
FROM sys.change_tracking_tables ct JOIN sys.tables t 
 ON ct.object_id= t.object_id JOIN sys.schemas s
 ON t.schema_id= s.schema_id;
EXEC sp_executesql  @SQL;