2011-12-23 98 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(我想我可以作爲一種方法更好的使用比文字和語言來解釋)