Computed column or trigger

Persisted computed column or regular column with trigger?

The scenario: you have a table column in which the value is dependent on the values in the other table columns. The two options that you have are computed columns or a trigger.

Computed columns have been supported since 2005. If you specify a persisted computed column, the calculated value will be stored in the database. An index can also be created on the column, although this will be deterministic but not precise.

The second approach is to specify that the column is a regular datatype. Then create a trigger to update the value. The benefit to this approach is that you can create a full index on the column.

Testing to calculate hash fields shows there is little performance between the two approaches.

The potential downsides to using the trigger approach is that triggers can be disabled. In addition the column holding our calculated value could be overwritten, however this would quickly be reverted by the trigger.

I think it’s down to the DBD to choose which approach is best based on the data held within and the size of the database.