如何对任务进行双重检查(如何将其存储在数据库中)?

发布于 2024-08-07 16:12:01 字数 288 浏览 9 评论 0原文

我有一个数据库,在不同的表中存储不同类型的任务和更多项目。 在许多这样的表中(它们的结构不同),我需要一种方法来做到这一点,即必须仔细检查该项目,这意味着该项目无法“保存”(我的意思是它当然会被保存)在其他人进入程序并确认之前。

正确的说法应该是什么:

  1. 每个表都应该有一列“IsConfirmed”,然后当那个人想要确认所有内容时,程序会遍历所有表并创建一个项目列表未检查的。
  2. 应该有第三个表来保存必须确认的该行的表名和 ID。
  3. 我希望你有比上面两个丑陋的更好的想法。

I have a DB that stores different types of tasks and more items in different tables.
In many of these tables (that their structure is different) I need a way to do it that the item has to be double-checked, meaning that the item can't be 'saved' (I mean of course it will be saved) before someone else goes in the program and confirms it.

What should be the right way to say which item is confirmed:

  1. Each of these tables should have a column "IsConfirmed", then when that guy wants to confirm all the stuff, the program walks thru all the tables and creates a list of the items that are not checked.
  2. There should be a third table that holds the table name and Id of that row that has to be confirmed.
  3. I hope you have a better idea than the two uglies above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天邊彩虹 2024-08-14 16:12:01

对于一个实体来说,双重确认状态是否只发生一次?还是可以被拒绝,需要再次确认?在后一种情况下,您需要保留所有这些历史记录吗?您是否需要跟踪每次确认的人员(例如,这样就不会由同一个人执行两次确认)?

简单的情况:

ALTER TABLE dbo.Table ADD ConfirmCount TINYINT NOT NULL DEFAULT 0;
ALTER TABLE dbo.Table ADD Processed BIT NOT NULL DEFAULT 0;

第一次确认时:

UPDATE dbo.Table SET ConfirmCount = 1 WHERE PK = <PK> AND ConfirmCount = 0;

第二次确认时:

UPDATE dbo.Table SET ConfirmCount = 2 WHERE PK = <PK> AND ConfirmCount = 1;

被拒绝时:

UPDATE dbo.Table SET ConfirmCount = 0 WHERE PK = <PK>;

现在显然您的后台作业只能处理 Processed = 0 ConfirmCount = 2 的行。然后当它处理该行时:

UPDATE dbo.Table SET Processed = 1 WHERE PK = <PK>;

如果您有比这更复杂的场景,请提供更多详细信息,包括双重确认过程的目标。

Is the double-confirmed status something that happens exactly once for an entity? Or can it be rejected and need to go through confirmation again? In the latter case, do you need to keep all of this history? Do you need to keep track of who confirmed each time (e.g. so you don't have the same person performing both confirmations)?

The simple case:

ALTER TABLE dbo.Table ADD ConfirmCount TINYINT NOT NULL DEFAULT 0;
ALTER TABLE dbo.Table ADD Processed BIT NOT NULL DEFAULT 0;

When the first confirmation:

UPDATE dbo.Table SET ConfirmCount = 1 WHERE PK = <PK> AND ConfirmCount = 0;

On second confirmation:

UPDATE dbo.Table SET ConfirmCount = 2 WHERE PK = <PK> AND ConfirmCount = 1;

When rejected:

UPDATE dbo.Table SET ConfirmCount = 0 WHERE PK = <PK>;

Now obviously your background job can only treat rows where Processed = 0 and ConfirmCount = 2. Then when it has processed that row:

UPDATE dbo.Table SET Processed = 1 WHERE PK = <PK>;

If you have a more complex scenario than this, please provide more details, including the goals of the double-confirm process.

浅浅 2024-08-14 16:12:01

考虑添加一个新表来保存要确认的记录(例如TasksToBeConfirmed)。确认记录后,将这些记录移至永久表(任务)。

添加“IsConfirmed”列的缺点是,几乎每个使用该表的 SQL 语句都必须对“IsConfirmed”进行过滤,以防止获取未经确认的记录。每次错过这一点,就会引入缺陷。

如果您需要确认和未确认的记录,请使用 UNION。

这种模式需要更多的编码和实现工作,但根据我的经验,它可以显着提高性能并减少缺陷。

Consider adding a new table to hold the records to be confirmed (e.g. TasksToBeConfirmed). Once the records are confirmed, move those records to the permanent table (Tasks).

The disadvantage of adding an "IsConfirmed" column is that virtually every SQL statement that uses the table will have to filter on "IsConfirmed" to prevent getting unconfirmed records. Every time this is missed, a defect is introduced.

In cases where you need confirmed and unconfirmed records, use UNION.

This pattern is a little more work to code and implement, but in my experience, significantly improves performance and reduces defects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文