There are some similar questions on the topic, but they are not really helping me.
I want to implement a soft delete feature like on StackOverflow, where items are not really deleted, but just hidden. I am using a SQL database. Here are 3 options:
Add a
is_deletedboolean field.- Advantages: Simple.
- Disadvantages: No date record. Forces me to add a
is_deleted = 0in every query.
Add a
deleted_datedate field. This is set toNULLif it's not deleted.- Advantages: Has date.
- Disadvantages: Still cluttering my queries.
For both of the above
- It will also impact performance because there are all these useless rows. They still have to be maintained in indexes. Also an index on the
deletedcolumn won't help when fetching non-deleted (the majority) of the rows. Full table scan is needed.
Another option is to create a separate table to hold deleted items:
- Advantages: Improved performance when querying non-deleted rows. No need to add conditions to my queries on non-deleted rows. Easier on index maintenance.
- Disadvantages: Complexity: Requires data migration for both deletion and undeletion. Need for new tables. Referential integrity is harder to handle.
Is there a better option?