2011-12-23 93 views
1

我正在处理用户订阅。更新记录或保留日志?

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

你建议:

  1. 拥有为每个用户,这对于如开始日期的所有可能的值的字段一个subscriptions记录,到期日期,是积极的,失败的结算日期,注销日期,暂停日期,重新启动日期?
  2. 或者有一个subscriptions的记录,用辅助表subscription_events?该次表中的一行可以记录“订阅X已被更新”。然后,我会查询订阅的最新事件以了解其当前状态。
  3. 还是更好的方法?

回答

1

不要去1.它不够灵活为新事件。每当新事件出现时,您都不需要一个需要更改表格的设计。你还需要每个事件日期的列,并且当你想知道事物的顺序时它就开始变得很难看。 当用户可以有多个订阅时会发生什么?

2是正确的。为了规范化它,我想象你有一个USER表和一个SUBSCRIPTION映射这两个表的SERVICE表。然后会有一个EVENT表,其中包含已知可能的事件和一个SUBSCRIPTION_EVENT_MAP映射SUBSCRIPTION,带有时间戳。

+0

谢谢,很明显。要检查订阅的状态(例如,当他们尝试登录时),您会查询SUBSCRIPTION_EVENT_MAP中的最新相关记录吗? – eoinoc 2011-12-23 16:51:25

+0

@eoinoc是,最新的记录按时间戳记。 – Glenn 2011-12-23 17:53:04

0

它归结于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(我想我可以作为一种方法更好的使用比文字和语言来解释)