软删除是个好主意吗? [英] Are soft deletes a good idea?

查看:262
本文介绍了软删除是个好主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

软删除是一个好主意还是坏主意?

Are soft deletes a good idea or a bad idea?

除了实际删除数据库中的记录,您只需将其标记为 IsDeleted = true ,并且在恢复记录后,您可以将其标记为 False

Instead of actually deleting a record in your database, you would just flag it as IsDeleted = true, and upon recovery of the record you could just flag it as False.

这是个好主意吗?

这是一个更好的想法,物理删除记录,然后将其移动到存档数据库,如果用户想要

Is it a better idea to physically delete the record, then move it to an archive database, and if the user wants the record back, then software will look for the record in the archive and recreate it?

推荐答案

我说这是一个坏主意,一般来说(也许有一些例外)。

I say it's a bad idea, generally (with some exceptions, perhaps).

首先,您的数据库应该定期备份,因此您永远不应该因为DELETE而永久丢失数据,除非是删除添加的数据,当然)。

First, your database should be backed up regularly, so you should never be in a situation where you would lose data permanently because of a DELETE (unless it's a deletion of just-added data, of course).

其次,像这样的软删除意味着你现在必须包括一个 WHERE IsDeleted = false 这个表上的每个查询(如果你加入这些表,那么糟糕得多)。一旦用户或测试人员注意到已删除的记录再次显示,可能需要一些时间,这里的错误就会被捕获。此外,开发人员很容易从COUNT(*)查询中省略WHERE子句,这可能需要更长时间才能发现(我曾在一个项目中工作多年,没有多少记录被删除) ,所以总计接近于预期值,没有人注意到)。

Second, a soft delete like this means you now have to include a WHERE IsDeleted = false clause in every query on this table (and so much worse if you're JOINing these tables). A mistake here would be caught as soon as a user or tester noticed a deleted record showing up again, which might take some time. Also, it would be easy for a developer to omit the WHERE clause from COUNT(*) queries, which might take even longer to discover (I worked on one project where this had been happening for years; not many records were ever "deleted", so the totals were close to what was expected and no one noticed).

最后,软删除将在使用人工键的表上工作,但可能不会使用自然主键在一个表上工作(例如,你从一个关键的社会安全号码表中删除某人 - 当你需要添加他的时候你做什么?请不要说include IsDeleted in a compound primary key 。)。

Finally, a soft delete will work on a table with artificial keys, but potentially won't work on a table with a natural primary key (e.g. you "delete" someone from a table keyed by Social Security Number - what do you do when you need to add him back? Please don't say "include IsDeleted in a compound primary key".).

在设计评审中,我希望开发人员能够表现出对成本和收益的意识,并提供一个优秀的这样做软删除的原因。 为什么不是这样做?不是一个很好的理由。

In a design review, I would expect the developer to demonstrate an awareness of the costs and benefits and to present an excellent reason for doing soft deletes in this manner. "Why not do it?" is not an excellent reason.

这篇关于软删除是个好主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆