簡體   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