保留原則

保留原則可控制自動從資料表或具體化檢視中移除資料的機制。 這適用於移除持續流入資料表的資料,以及其相關性以存留期為基礎的資料。 例如,此原則可以用於保存診斷事件的資料表,而這些事件可能會在兩週後變成不重要。

可針對特定資料表或具體化檢視設定保留原則,也可以為整個資料庫設定保留原則。 原則接著會套用至資料庫中的所有資料表,這些資料表不會覆寫該原則。 當原則同時在資料庫和數據表層級設定時,數據表中的保留原則會優先於資料庫原則。

針對持續擷取資料的叢集而言,設定保留原則十分重要,因為這會限制成本。

您可以移除保留原則「外部」的資料。 拿掉時沒有特定保證。 即使觸發保留原則,資料還是會「延遲」。

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

注意

  • 刪除時間不精確。 系統保證不會在超過限制之前刪除資料,但不會在該時間點之後立即刪除。
  • 虛刪除期間 0 可以設定為資料表層級保留原則的一部分,但不能作為資料庫層級保留原則的一部分。
  • 完成這項作業時,不會將所擷取的資料認可至來源資料表,這樣就不需要持續保存資料。 因此,Recoverability 只能設定為 Disabled
  • 這類設定主要適用於將資料擷取至資料表時。 交易式更新原則用來進行轉換,並將輸出重新導向至另一個資料表。

原則物件

保留原則包括下列屬性:

  • SoftDeletePeriod
    • 保證資料持續可供查詢的時間範圍。 這段期間是從擷取資料時開始測量。
    • 預設值為 100 years
    • 改變資料表或資料庫的虛刪除期間時,會將新值套用至現有和新的資料。
  • 可復原性
    • 刪除資料之後的資料可復原性 (啟用/停用)。
    • 預設值為 Enabled
    • 如果設定為 Enabled,則資料將可在虛刪除後的 14 天內復原。
    • 無法設定復原期間。

管理命令

Defaults

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

您可以使用下列命令套用預設保留原則 (具有上面所提及的預設值)。

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

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

{
  "SoftDeletePeriod": "36500.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:針對每個資料表,設定資料表層級保留原則,並停用七天的虛刪除期間和可復原性。

    .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:針對 MySpecialTable,設定資料庫層級保留原則,並設定虛刪除期間為 100 年的資料表層級保留原則 (預設保留原則)。

    .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,設定虛刪除期間為 100 年的資料表層級保留原則 (預設保留原則)。

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