Change Tracking Automatic/Manual Cleanup Index

Published May 16 2022 07:49 AM 1,096 Views
Microsoft

Howdy!

 

It isn't uncommon for us to receive autocleanup or manual cleanup issues regarding Change Tracking in CSS. If you have very busy tables that results in a lot of data being written to the side tables, it isn't uncommon to see error 22122 in the SQL Server error log indicating there was a failure in running cleanup and we will retry later. 

 

2022-04-07 17:30:29.13 spid42s     Error: 22122, Severity: 16, State: 1.

2022-04-07 17:30:29.13 spid42s     Change Tracking autocleanup failed on side table of "SourceTableName".  If the failure persists, use sp_flush_CT_internal_table_on_demand to clean up expired records from its side table.

 

 

If the issue persists and we stack a lot of data that needs to be removed which can be too much for autocleanup to manage. At that point, we recommend running (as is indicated in the error message) sp_flush_CT_internal_table_on_demand which is discusseed in detailed in Amit's blog post Change Tracking Cleanup–Part 1.

 

I will not discuss the internals of the process as Amit Banerjee already touched on this in the referenced blog. However if you are routinely encountering issues where autocleanup can't keep up and/or manual cleanup is taking significant time, you can add the index and see if it helps and I have the process enumerated below. Keep in mind this is not guaranteed to fix all cleanup issues and it may still be necessary to run manual cleanup regularly. If you continue to have issues with cleanup feel free to create a ticket with us here at CSS to assist you and get specific data to your environment.

 

How to Create si_commit_time Index

 

 

Some things to know before we start:

 

  • If you have SQL 2019 or a more recent version of SQL, this index already exists. You do not need to do this process.
  • We recommend you run the manual cleanup and letting it finish if at all possible BEFORE creating the index so the process is very fast. If you cannot, the question of how long the index will take to create is something we cannot answer due to the number of variables that impact the ability for SQL to create it. For reference, on one customer's server with 56 cores I saw it took approximately 11 minutes to create the index on a 26 GB syscommittab.
  • You will have to stop and start SQL to go through this process, and we will be starting SQL in single user mode. This is an outage and needs to be approached as such.
  • This is not a blanket recommendation to implement this for environments. However if you are having cleanup issues there is little to no risk to trying it. If you don't see performance improve, you can drop the index in the future.
  • If your database is part of an Availability Group, you will not be able to add the index. You'll need to remove the database from the AG and follow the steps, then add back.
  • The reason this index helps is an expensive part of cleanup is where we compare the invalid versions to syscommittab and pass a time parameter, which now we can use the NCI to do.

 

 

How to do it:

 

  1. First we want to do the manual cleanup using sp_flush_CT_internal_table_on_demand if we can to let it manually cleanup before proceeding. To help this run, if possible stop all updates against the CT enabled database. If you cannot, keep in mind the size of syscomittab is going to impact index creation as indicated above. The script in Amit's blog above is a great reference.
  2. Launch SQL Server Configuration Manager
  3. Then right-click the proper SQL Server service and go into Properties --> Startup Parameters tab and add under "specify a startup parameter" add  -mSQLCMD and -f (capitalization is important!) and restart the service. More information on the startup options are here; Database Engine Service Startup Options - SQL Server | Microsoft Docs
    1. Keep in mind if your instance is clustered, you'll need to edit these steps such as our documentation for clustered instances describes 
    2. sqlconfig1.png
  4. Then launch sqlcmd on the SQL box through an administrative CMD and connect using the -A parameter to use the DAC Connection.
    1. HINT: If you press enter you can run SELECT @@SERVERNAME, press enter, then type go, and enter again to verify you're connected. 
    2. /*
         Example below is using Windows Authentication (-E parameter), for the default instance 
         on server WIN-L3UUVD9S3HV (-S parameter) and using DAC (-A parameter)
      */
      sqlcmd -E -S WIN-L3UUVD9S3HV -A
  5. Then create the index using the code below. You'll want to do this for model (so newly created databases get this index) and any user database that has Change Tracking enabled. You can copy and paste this code after changing the database name into CMD and press enter and it should work. If it seems like it's taking a long time, verify that the database you are running against is recovered (check SQL Server error log for recovery messages). It should be very fast if you ran manual cleanup and syscomittab is small.
    1. USE [ChangeThisToYourDBName]
      GO
      CREATE NONCLUSTERED INDEX [si_commit_time]
      ON [sys].[syscommittab] (
          [commit_time] DESC,
          [commit_ts] DESC
      )
      WITH (SORT_IN_TEMPDB = ON)
      GO
  6. You can then verify the index is created on each database by using the code below. A row returned means it was created. 0 means it did not.
    1. USE [ChangeThisToYourDBName]
      GO
      SELECT object_name(object_id) tablename,index_id,name as index_name,type_desc 
      FROM sys.indexes 
      where object_id=object_id('sys.syscommittab') and name = 'si_commit_time'
      GO
  7. Then go into SQL Server Configuration manager, remove the -mSQLCMD and -f parameters and restart SQL. Now user connections should be able to access SQL and cleanup will use this new index.

 

Hope this helps!

James 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-3372764%22%20slang%3D%22en-US%22%3EChange%20Tracking%20Automatic%2FManual%20Cleanup%20Index%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3372764%22%20slang%3D%22en-US%22%3E%3CP%3EHowdy!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20isn't%20uncommon%20for%20us%20to%20receive%20autocleanup%20or%20manual%20cleanup%20issues%20regarding%20Change%20Tracking%20in%20CSS.%20If%20you%20have%20very%20busy%20tables%20that%20results%20in%20a%20lot%20of%20data%20being%20written%20to%20the%20side%20tables%2C%20it%20isn't%20uncommon%20to%20see%20error%2022122%20in%20the%20SQL%20Server%20error%20log%20indicating%20there%20was%20a%20failure%20in%20running%20cleanup%20and%20we%20will%20retry%20later.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CTABLE%20border%3D%221%22%20width%3D%22100%25%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22100%25%22%3E%3CP%3E2022-04-07%2017%3A30%3A29.13%20spid42s%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Error%3A%2022122%2C%20Severity%3A%2016%2C%20State%3A%201.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%22100%25%22%3E%3CP%3E2022-04-07%2017%3A30%3A29.13%20spid42s%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Change%20Tracking%20autocleanup%20failed%20on%20side%20table%20of%20%22SourceTableName%22.%26nbsp%3B%20If%20the%20failure%20persists%2C%20use%20sp_flush_CT_internal_table_on_demand%20to%20clean%20up%20expired%20records%20from%20its%20side%20table.%3C%2FP%3E%0A%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20the%20issue%20persists%20and%20we%20stack%20a%20lot%20of%20data%20that%20needs%20to%20be%20removed%20which%20can%20be%20too%20much%20for%20autocleanup%20to%20manage.%20At%20that%20point%2C%20we%20recommend%20running%20(as%20is%20indicated%20in%20the%20error%20message)%20sp_flush_CT_internal_table_on_demand%20which%20is%20discusseed%20in%20detailed%20in%20Amit's%20blog%20post%26nbsp%3B%3C%2FSPAN%3E%3CA%20style%3D%22font-family%3A%20inherit%3B%20background-color%3A%20%23ffffff%3B%22%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fsql-server-blog%2Fchange-tracking-cleanup-8211-part-1%2Fba-p%2F384861%22%20target%3D%22_self%22%3EChange%20Tracking%20Cleanup%E2%80%93Part%201%3C%2FA%3E%3CSPAN%3E.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20will%20not%20discuss%20the%20internals%20of%20the%20process%20as%26nbsp%3BAmit%20Banerjee%20already%20touched%20on%20this%20in%20the%20referenced%20blog.%20However%20if%20you%20are%20routinely%20encountering%20issues%20where%20autocleanup%20can't%20keep%20up%20and%2For%20manual%20cleanup%20is%20taking%20significant%20time%2C%20you%20can%20add%20the%20index%20and%20see%20if%20it%20helps%20and%20I%20have%20the%20process%20enumerated%20below.%20Keep%20in%20mind%20this%20is%20not%20guaranteed%20to%20fix%20all%20cleanup%20issues%20and%20it%20may%20still%20be%20necessary%20to%20run%20manual%20cleanup%20regularly.%20If%20you%20continue%20to%20have%20issues%20with%20cleanup%20feel%20free%20to%20create%20a%20ticket%20with%20us%20here%20at%20CSS%20to%20assist%20you%20and%20get%20specific%20data%20to%20your%20environment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20text-align%3A%20center%3B%20%22%3E%3CFONT%20size%3D%226%22%3E%3CU%3E%3CSTRONG%3EHow%20to%20Create%20si_commit_time%20Index%3C%2FSTRONG%3E%3C%2FU%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20text-align%3A%20center%3B%20%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20text-align%3A%20center%3B%20%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20text-align%3A%20center%3B%20%22%3E%3CU%3E%3CFONT%20size%3D%225%22%3ESome%20things%20to%20know%20before%20we%20start%3A%3C%2FFONT%3E%3C%2FU%3E%3C%2FP%3E%0A%3CP%20style%3D%22%20text-align%3A%20center%3B%20%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EIf%20you%20have%20SQL%202019%20or%20a%20more%20recent%20version%20of%20SQL%2C%20this%20index%20already%20exists.%20You%20do%20not%20need%20to%20do%20this%20process.%3C%2FLI%3E%0A%3CLI%3EWe%20recommend%20you%20run%20the%20manual%20cleanup%20and%20letting%20it%20finish%20if%20at%20all%20possible%20BEFORE%20creating%20the%20index%20so%20the%20process%20is%20very%20fast.%20If%20you%20cannot%2C%20the%20question%20of%20how%20long%20the%20index%20will%20take%20to%20create%20is%20something%20we%20cannot%20answer%20due%20to%20the%20number%20of%20variables%20that%20impact%20the%20ability%20for%20SQL%20to%20create%20it.%20For%20reference%2C%20on%20one%20customer's%20server%20with%2056%20cores%20I%20saw%20it%20took%20approximately%2011%20minutes%20to%20create%20the%20index%20on%20a%2026%20GB%20syscommittab.%3C%2FLI%3E%0A%3CLI%3EYou%20will%20have%20to%20stop%20and%20start%20SQL%20to%20go%20through%20this%20process%2C%20and%20we%20will%20be%20starting%20SQL%20in%20single%20user%20mode.%20This%20is%20an%20outage%20and%20needs%20to%20be%20approached%20as%20such.%3C%2FLI%3E%0A%3CLI%3EThis%20is%20not%20a%20blanket%20recommendation%20to%20implement%20this%20for%20environments.%20However%20if%20you%20are%20having%20cleanup%20issues%20there%20is%20little%20to%20no%20risk%20to%20trying%20it.%20If%20you%20don't%20see%20performance%20improve%2C%20you%20can%20drop%20the%20index%20in%20the%20future.%3C%2FLI%3E%0A%3CLI%3EIf%20your%20database%20is%20part%20of%20an%20Availability%20Group%2C%20you%20will%20not%20be%20able%20to%20add%20the%20index.%20You'll%20need%20to%20remove%20the%20database%20from%20the%20AG%20and%20follow%20the%20steps%2C%20then%20add%20back.%3C%2FLI%3E%0A%3CLI%3EThe%20reason%20this%20index%20helps%20is%20an%20expensive%20part%20of%20cleanup%20is%20where%20we%20compare%20the%20invalid%20versions%20to%20syscommittab%20and%20pass%20a%20time%20parameter%2C%20which%20now%20we%20can%20use%20the%20NCI%20to%20do.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22%20text-align%3A%20center%3B%20%22%3E%3CU%3E%3CFONT%20size%3D%225%22%3EHow%20to%20do%20it%3A%3C%2FFONT%3E%3C%2FU%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EFirst%20we%20want%20to%20do%20the%20manual%20cleanup%20using%20sp_flush_CT_internal_table_on_demand%20if%20we%20can%20to%20let%20it%20manually%20cleanup%20before%20proceeding.%20To%20help%20this%20run%2C%20if%20possible%20stop%20all%20updates%20against%20the%20CT%20enabled%20database.%20If%20you%20cannot%2C%20keep%20in%20mind%20the%20size%20of%20syscomittab%20is%20going%20to%20impact%20index%20creation%20as%20indicated%20above.%20The%20script%20in%20Amit's%20blog%20above%20is%20a%20great%20reference.%3C%2FLI%3E%0A%3CLI%3ELaunch%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fsql-server-configuration-manager%3Fview%3Dsql-server-ver15%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3ESQL%20Server%20Configuration%20Manager%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EThen%20right-click%20the%20proper%20SQL%20Server%20service%20and%20go%20into%20Properties%20--%26gt%3B%20Startup%20Parameters%20tab%20and%20add%20under%20%22specify%20a%20startup%20parameter%22%20add%26nbsp%3B%20-mSQLCMD%20and%20-f%20(%3CSTRONG%3Ecapitalization%20is%20important!%3C%2FSTRONG%3E)%20and%20restart%20the%20service.%20More%20information%20on%20the%20startup%20options%20are%20here%3B%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fdatabase-engine-service-startup-options%3Fview%3Dsql-server-ver15%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EDatabase%20Engine%20Service%20Startup%20Options%20-%20SQL%20Server%20%7C%20Microsoft%20Docs%3C%2FA%3E%3COL%3E%0A%3CLI%3EKeep%20in%20mind%20if%20your%20instance%20is%20clustered%2C%20you'll%20need%20to%20edit%20these%20steps%20such%20as%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fstart-sql-server-in-single-user-mode%3Fview%3Dsql-server-ver15%23note-for-clustered-installations%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Eour%20documentation%20for%20clustered%20instances%20describes%3C%2FA%3E%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sqlconfig1.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F371526i0E038248A0BD4DF4%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22sqlconfig1.png%22%20alt%3D%22sqlconfig1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3EThen%20launch%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Ftools%2Fsqlcmd-utility%3Fview%3Dsql-server-ver15%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Esqlcmd%3C%2FA%3E%26nbsp%3Bon%20the%20SQL%20box%20through%20an%20administrative%20CMD%20and%20connect%20using%20the%20-A%20parameter%20to%20use%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Fdatabase-engine%2Fconfigure-windows%2Fdiagnostic-connection-for-database-administrators%3Fview%3Dsql-server-ver15%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EDAC%20Connection.%3C%2FA%3E%3COL%3E%0A%3CLI%3EHINT%3A%20If%20you%20press%20enter%20you%20can%20run%20SELECT%26nbsp%3B%40%40SERVERNAME%2C%20press%20enter%2C%20then%20type%20go%2C%20and%20enter%20again%20to%20verify%20you're%20connected.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%2F*%0A%20%20%20Example%20below%20is%20using%20Windows%20Authentication%20(-E%20parameter)%2C%20for%20the%20default%20instance%20%0A%20%20%20on%20server%20WIN-L3UUVD9S3HV%20(-S%20parameter)%20and%20using%20DAC%20(-A%20parameter)%0A*%2F%0Asqlcmd%20-E%20-S%20WIN-L3UUVD9S3HV%20-A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3EThen%20create%20the%20index%20using%20the%20code%20below.%20You'll%20want%20to%20do%20this%20for%20model%20(so%20newly%20created%20databases%20get%20this%20index)%20and%20any%20user%20database%20that%20has%20Change%20Tracking%20enabled.%20You%20can%20copy%20and%20paste%20this%20code%20after%20changing%20the%20database%20name%20into%20CMD%20and%20press%20enter%20and%20it%20should%20work.%20If%20it%20seems%20like%20it's%20taking%20a%20long%20time%2C%20verify%20that%20the%20database%20you%20are%20running%20against%20is%20recovered%20(check%20SQL%20Server%20error%20log%20for%20recovery%20messages).%20It%20should%20be%20very%20fast%20if%20you%20ran%20manual%20cleanup%20and%20syscomittab%20is%20small.%3COL%3E%0A%3CLI%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EUSE%20%5BChangeThisToYourDBName%5D%0AGO%0ACREATE%20NONCLUSTERED%20INDEX%20%5Bsi_commit_time%5D%0AON%20%5Bsys%5D.%5Bsyscommittab%5D%20(%0A%20%20%20%20%5Bcommit_time%5D%20DESC%2C%0A%20%20%20%20%5Bcommit_ts%5D%20DESC%0A)%0AWITH%20(SORT_IN_TEMPDB%20%3D%20ON)%0AGO%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3EYou%20can%20then%20verify%20the%20index%20is%20created%20on%20each%20database%20by%20using%20the%20code%20below.%20A%20row%20returned%20means%20it%20was%20created.%200%20means%20it%20did%20not.%3COL%3E%0A%3CLI%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EUSE%20%5BChangeThisToYourDBName%5D%0AGO%0ASELECT%20object_name(object_id)%20tablename%2Cindex_id%2Cname%20as%20index_name%2Ctype_desc%20%0AFROM%20sys.indexes%20%0Awhere%20object_id%3Dobject_id('sys.syscommittab')%20and%20name%20%3D%20'si_commit_time'%0AGO%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3EThen%20go%20into%20SQL%20Server%20Configuration%20manager%2C%20remove%20the%20-mSQLCMD%20and%20-f%20parameters%20and%20restart%20SQL.%20Now%20user%20connections%20should%20be%20able%20to%20access%20SQL%20and%20cleanup%20will%20use%20this%20new%20index.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHope%20this%20helps!%3C%2FP%3E%0A%3CP%3EJames%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-3372764%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20made%20a%20product%20improvement%20in%20SQL%20Server%202019%20that%20wasn't%20backported%20to%20the%20previous%20versions%20as%20it%20requires%20a%20database%20version%20change.%20We%20generally%20avoid%20doing%20this%20in%20a%20service%20pack%20or%20cumulative%20update.%20However%20if%20you%20observe%20issues%20with%20Change%20Tracking%20cleanup%20(automatic%20or%20manual)%2C%20you%20can%20create%20this%20index%20and%20hopefully%20it%20will%20improve%20your%20performance.%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Co-Authors
Version history
Last update:
‎May 16 2022 07:49 AM
Updated by: