0

We use a deleteAt column to specify whether something is active or (soft) deleted. Which technique is best going forward for querying active contents?

Would a filtered index on the actual table better, or an indexed view with deleteAt is NULL?

Is this a good use case for indexed view?

Or should we go for regular view with filtered index?

4

1 回答 1

2

Instead of having one Datetime column to handle soft deletion, I would use two columns .

ColumnName    Data Type
 Deleted        BIT      DEFAULT(0) 
 DeletedAt    DateTime     

And create a Filtered Index on the Deleted column something WHERE Deleted = 0.

The reason is, data with soft deleted rows we mostly only return Active/Non-Deleted rows. Hence each time you return rows sql server can look at the filtered index on the smallest data type (bit being 1 byte) and do less data processing.

And if you ever want to know when a record was deleted , DaletedAt column is there to store that piece of information.

On the other side If you only have ONE Datetime Column to to handle soft deletion, Your Filtered index would be something like WHERE DeletedAT IS NULL, now in this case SQL Server will be querying 8 Bytes of data just to check if a row is Active or not. In this case you are doing 8 times more data processing as compare to a BIT column just to get the same results. Sounds bad doesnt it :).

Hence my recomendation would be Add another Bit column , and a filtered index on that bit column to handle soft deletion. Leave DeletedAt column to handle only delets.

于 2015-03-14T01:25:13.423 回答