繁体   English   中英

更新记录还是保留日志?

[英]Update record or keep a log?

我正在处理用户订阅。

用户创建定期订阅。 该订阅可能会发生很多事情,例如,续订,取消订阅,最新的结算失败,用户已暂停或重新开始订阅。

您是否建议:

  1. 每个用户都有一个subscriptions记录,其中包含所有可能值的字段,例如开始日期,到期日期,活动,计费失败日期,取消日期,暂停日期,重新开始日期?
  2. 还是有一个subscriptions记录,以及一个辅助表subscription_events 该辅助表中的一行可能记录“订阅X已被更新”。 然后,我将查询最近的事件以获取订阅,以了解其当前状态。
  3. 还是更好的方法?

不要追求1。它对于新事件来说不够灵活。 您不希望每次出现新事件时都需要更改表的设计。 您还需要每个事件日期的列,并且当您想了解事物的顺序时,它只会变得很难看。 当用户可以有多个订阅时,还会发生什么?

2是正确的。 为了使它标准化,我可以想象您有一个USER表和一个SERVICE表,其中SUBSCRIPTION有两个表。 然后将有一个带有已知可能事件的EVENT表,以及一个SUBSCRIPTION_EVENT_MAP映射SUBSCRIPTION到带时间戳的EVENT。

它归结于designintention 以下是许多方法中的一些方法:

您可以使用历史记录表:

-- stores info re:reason for the last update of a subscription
CREATE TABLE subscription_history (
      subscription_id INT
    , change_date DATETIME
    , change_reason VARCHAR(255)
)

或结合查询的历史记录表:

-- stores info re:reason for the last update of a subscription
--     but links to a change_reason table for reason id lookups
CREATE TABLE subscription_history_L (
      subscription_id INT
    , change_date DATETIME
    , change_reason_id INT
)

-- lookup table containing change reasons
CREATE TABLE change_reason (
      change_reason_id INT
    , change_reason VARCHAR(255)
)

或审核表v1:

-- contains all columns in your subscription table plus audit fields
CREATE TABLE subscription_audit (
      subscription_audit_id INT
    -- All the fields from your `subscriptions` table
    , audit_date DATETIME
    , audit_reason VARCHAR(255)
    , audit_by VARCHAR(255) -- example
    -- etc etc whatever other information that is pertinent to the change
)

或审核表v2:

-- this could also act like a history table, so you can change the table name/purpose
CREATE TABLE subscription_audit (
      subscription_id INT
    , modified_column VARCHAR(255)
    , value_old VARCHAR(255)
    , value_new VARCHAR(255)
    , modified_date DATETIME
) -- Drawback here is that you'll have one audit record per column
  -- , and you may have to add extra columns for other data types
  -- , or convert your values to varchar or something else.. which isn't 
  --   a really good idea! I just want to present this in case you can
  --   develop the idea you find interesting further

我不知道您的RDBMS,但这几乎是一般的SQl(我认为我可以比单词和单词更好地用作一种解释方法)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM