共用方式為


保留原則

保留原則會控制自動從數據表或 具體化檢視中移除數據的機制。 以年齡為依據移除持續流入資料表的資料及其相關性,是非常有用的做法。 例如,原則可用於保存兩周后可能不感興趣之診斷事件的數據表。

保留原則可以針對特定數據表或具體化檢視或整個資料庫設定。 然後,原則會套用至資料庫中未覆寫它的所有數據表。 在資料庫和數據表層級設定原則時,數據表中的保留原則優先於資料庫原則。

設定保留原則對於持續擷取數據的叢集而言很重要,這會限制成本。

保留原則「外部」的數據符合移除資格。 拿掉時沒有特定保證。 即使觸發保留原則,數據仍可能會「揮之不去」。

保留原則最常設定為限制擷取之後的數據存留期。 如需詳細資訊,請參閱 SoftDeletePeriod

注意

  • 刪除時間不精確。 系統保證在未超出限制之前不會刪除資料,但並不會在一超出限制就馬上刪除。
  • 虛刪除期間0可以設定為數據表層級保留原則的一部分,但不能設定為資料庫層級保留原則的一部分。
  • 完成此作業時,內嵌的數據將不會認可至源數據表,避免需要保存數據。 因此, Recoverability 只能設定為 Disabled
  • 這類設定主要適用於將數據內嵌至數據表時。 交易 式更新原則 可用來轉換它,並將輸出重新導向至另一個數據表。

原則物件

保留原則包含下列屬性:

  • SoftDeletePeriod
    • 保證數據可供查詢的時間範圍。 期間是從擷取數據的時間開始測量。
    • 預設為 1,000 years
    • 更改數據表或資料庫的虛刪除期間時,新值會同時套用至現有和新數據。
  • 復原能力
    • 刪除數據之後的數據可復原性 (Enabled/Disabled)。
    • 預設為 Enabled
    • 如果設定為 Enabled,數據將會在虛刪除之後復原 14 天。
    • 無法設定復原期間。

管理命令

Defaults

根據預設,建立資料庫或數據表時,不會定義保留原則。 一般而言,資料庫會建立,然後立即根據已知需求,由建立者設定其保留原則。 當您針對尚未設定原則之資料庫或資料表的保留原則執行 .show 命令 時, Policy 會顯示為 null

使用上述預設值的預設保留原則,可以使用下列命令來套用。

.alter database DatabaseName policy retention "{}"
.alter table TableName policy retention "{}"
.alter materialized-view ViewName policy retention "{}"

此命令會產生套用至資料庫或數據表的下列原則物件。

{
  "SoftDeletePeriod": "365000.00:00:00", "Recoverability":"Enabled"
}

您可以使用下列命令清除資料庫或資料表的保留原則。

.delete database DatabaseName policy retention
.delete table TableName policy retention

範例

對於具有名為 MyDatabase之資料庫的叢集,具有資料表 MyTable1MyTable2MySpecialTable

已停用七天的虛刪除期間和可復原性

將資料庫中的所有數據表設定為具有七天的虛刪除期間,並停用復原能力。

  • 選項 1 (建議):設定資料庫層級保留原則,並確認沒有設定數據表層級的原則。

    .delete table MyTable1 policy retention        // optional, only if the table previously had its policy set
    .delete table MyTable2 policy retention        // optional, only if the table previously had its policy set
    .delete table MySpecialTable policy retention  // optional, only if the table previously had its policy set
    .alter-merge database MyDatabase policy retention softdelete = 7d recoverability = disabled
    .alter-merge materialized-view ViewName policy retention softdelete = 7d 
    
  • 選項 2:針對每個資料表,請設定資料表層級保留原則,並停用虛刪除期間為 7 天且可復原性。

    .alter-merge table MyTable1 policy retention softdelete = 7d recoverability = disabled
    .alter-merge table MyTable2 policy retention softdelete = 7d recoverability = disabled
    .alter-merge table MySpecialTable policy retention softdelete = 7d recoverability = disabled
    

啟用七天的虛刪除期間和可復原性

  • 將數據表 MyTable1MyTable2 設定為具有七天的虛刪除期間,並停用可復原性。

  • 設定 MySpecialTable 為具有 14 天的虛刪除期間,並啟用復原能力。

  • 選項 1 (建議):設定資料庫層級保留原則,並設定數據表層級保留原則。

    .delete table MyTable1 policy retention   // optional, only if the table previously had its policy set
    .delete table MyTable2 policy retention   // optional, only if the table previously had its policy set
    .alter-merge database MyDatabase policy retention softdelete = 7d recoverability = disabled
    .alter-merge table MySpecialTable policy retention softdelete = 14d recoverability = enabled
    
  • 選項 2:針對每個資料表,請設定具有相關虛刪除期間和可復原性的數據表層級保留原則。

    .alter-merge table MyTable1 policy retention softdelete = 7d recoverability = disabled
    .alter-merge table MyTable2 policy retention softdelete = 7d recoverability = disabled
    .alter-merge table MySpecialTable policy retention softdelete = 14d recoverability = enabled
    

虛刪除期間為七天,並 MySpecialTable 無限期地保留其數據

將數據表 MyTable1MyTable2 設定為具有七天的虛刪除期間,並 MySpecialTable 無限期地保留其數據。

  • 選項 1:設定資料庫層級保留原則,並設定數據表層級保留原則,其中虛刪除期間為 1,000 年,預設保留原則。MySpecialTable

    .delete table MyTable1 policy retention   // optional, only if the table previously had its policy set
    .delete table MyTable2 policy retention   // optional, only if the table previously had its policy set
    .alter-merge database MyDatabase policy retention softdelete = 7d
    .alter table MySpecialTable policy retention "{}" // this sets the default retention policy
    
  • 選項 2:針對數據表 MyTable1MyTable2,設定數據表層級保留原則,並確認未設定的資料庫層級和數據表層級原則 MySpecialTable

    .delete database MyDatabase policy retention   // optional, only if the database previously had its policy set
    .delete table MySpecialTable policy retention   // optional, only if the table previously had its policy set
    .alter-merge table MyTable1 policy retention softdelete = 7d
    .alter-merge table MyTable2 policy retention softdelete = 7d
    
  • 選項 3:針對資料表 MyTable1MyTable2,設定資料表層級保留原則。 針對數據表 MySpecialTable,請設定具有虛刪除期間為1,000年的數據表層級保留原則,預設保留原則。

    .alter-merge table MyTable1 policy retention softdelete = 7d
    .alter-merge table MyTable2 policy retention softdelete = 7d
    .alter table MySpecialTable policy retention "{}"