Sql-server – Change Data Capture vs trigger for logging changes

change-data-capturesql serversql-server-2016t-sqltrigger

I am going to create history table which is populated by trigger (after insert, update, delete). As only 20% of the columns are going to be updated, I decided to log only the changed values – if the values are not changed, NULL value is going to be used in the history table. For example:

enter image description here

The history table columns will be sparse and I am going to save a lot of space versus ordinary implementation which is logging all data (this is due to my test and my bussness cases).

As I the SQL Server 2016 SP1 standard edition supports Change Data Capture I am wondering are their any pros/cons/differences between using it and trigger-based logging?

I have check few artciles (here and here) and cannot see what more Change Data Capture can give me.

Best Answer

Given the choice between those two things, here's why I'd choose triggers:

  • Triggers can make related table changes synchronously (like if you need to update reporting tables or check referential integrity)
  • Triggers let you store the data in the format that works for you, whereas CDC puts it in its own CDC schema, in tables that may not work for your reports
  • Triggers can have logic that checks the kind of update or the source of the update, and only log things you want to log
  • Triggers can easily be temporarily disabled for things like bulk loads, or if the business is facing huge load demands (like Black Friday sales)
  • Triggers let you put more business logic in, like if you want to put some kinds of history in one table and other kinds of history in another
  • Triggers don't directly add storage overhead (whereas CDC adds at least 34 bytes per row)