Create and manage storage lifecycle policies

注釈

ストレージライフサイクルポリシー は現在、政府リージョンではご利用いただけません。

次のセクションでは、テーブルのストレージライフサイクルポリシーを作成、再作成、および管理する方法について説明します。

ストレージライフサイクルポリシーの作成

ストレージのライフサイクルポリシーを作成するには、CREATE STORAGE LIFECYCLE POLICY コマンドを使用します。

When you create a storage lifecycle policy, you can choose an archive tier and optionally set an archival period in days. If you set an archival period, Snowflake moves table rows that match the policy expression into a lower-cost storage tier for the specified number of days before expiring the rows. Snowflake also enables change tracking on any tables that you attach the policy to.

例:

CREATE STORAGE LIFECYCLE POLICY my_slp
  AS (event_ts TIMESTAMP, account_id NUMBER)
  RETURNS BOOLEAN ->
    event_ts < DATEADD(DAY, -60, CURRENT_TIMESTAMP())
    AND EXISTS (
      SELECT 1 FROM closed_accounts
      WHERE id = account_id
    )
  ARCHIVE_TIER = COOL
  ARCHIVE_FOR_DAYS = 90;
Copy

注釈

For considerations when you work with tables that have archival storage policies, see アーカイブストレージポリシー.

ベストプラクティス:時間ベースの式に日付変換を使用する

パフォーマンスを向上させ、一貫したポリシーの実行を確保するには、時間値を比較するときに、ポリシー式のタイムスタンプを日付に変換します。

たとえば、このポリシー式を考えてみましょう。

event_time < DATEADD(DAY, -400, CURRENT_TIMESTAMP())
Copy

この比較には、タイムスタンプの時間コンポーネントが含まれます。これが、一貫性のない動作を引き起こす可能性があります。event_time によってデータが時系列で挿入されるとき、ポリシーの実行時間は、各ファイルから削除される行数に影響します。

To avoid this inconsistent behavior, convert timestamps to dates in your expression:

TO_DATE(event_time) < TO_DATE(DATEADD(DAY, -400, CURRENT_TIMESTAMP()))
Copy

This method provides consistent policy execution regardless of the time of day.

ストレージライフサイクルポリシーの再作成

この機能は GET_DDL コマンドを拡張して、指定されたストレージライフサイクルポリシーを再作成します。ポリシーのアーカイブティアを変更する場合は、これを実行する場合があります。

To recreate a storage lifecycle policy named my_slp, return the DDL, as shown in the following example:

SELECT GET_DDL('policy','my_slp');
Copy

出力:

---------------------------------------------------------------------+
                      GET_DDL('POLICY','SLP')                        |
---------------------------------------------------------------------+
create or replace storage lifecycle policy SLP as                    |
  (event_ts timestamp, account_id number)
    returns boolean ->
    event_ts < dateadd(day, -60, current_timestamp())
    and exists (
      select 1 from closed_accounts
      where id = account_id
  )
  ARCHIVE_FOR_DAYS = 365                                             |
;                                                                    |
---------------------------------------------------------------------+

テーブルのストレージライフサイクルポリシーの管理

次のオプションを使用して、ストレージライフサイクルポリシーの添付を管理します。

テーブルへのポリシーの添付

1つのストレージライフサイクルポリシーで複数のテーブルを管理できます。テーブルを作成または変更するときにポリシーを添付します。

To create a table and attach the policy to a new table by using the specified columns, use CREATE TABLE, as shown in the following example.

注釈

  • ポリシーを適用するために必要な権限を持っている必要があります。必要な権限については、 Storage lifecycle policy privileges をご参照ください。

  • テーブルには、ストレージライフサイクルポリシーを1つだけ添付できます。

  • 列の数はポリシー関数署名の引数の数と一致する必要があり、列データは引数の型に対応する必要があります。

  • テーブル列の名前を変更しても、関連付けられたポリシーは影響を受けません。Snowflakeは列 IDs を使用してポリシーをテーブルに関連付けます。

  • ストレージライフサイクルポリシー式を評価して適用するために、Snowflakeは内部的および一時的にテーブルに対するガバナンスポリシーをバイパスします。

CREATE TABLE my_table
  ...
  WITH STORAGE LIFECYCLE POLICY my_slp ON (col1);
Copy

To attach the policy to an existing table by using the specified columns, use ALTER TABLE, as shown in the following example:

ALTER TABLE my_table ADD STORAGE LIFECYCLE POLICY my_slp
  ON (col1);
Copy

1回限りの操作としてポリシーを適用する

If you only need to expire or archive historical data once, as a one-time operation, we recommend the following procedure:

  1. Create, and then attach a storage lifecycle policy to your table.

  2. Wait for the policy to execute, and then archive or expire the data.

    Monitor the INFORMATION_SCHEMA.STORAGE_LIFECYCLE_POLICY_HISTORY table function to confirm the process is complete.

  3. To prevent recurring charges, remove the storage lifecycle policy from the table.

    Storage lifecycle policies incur cost per execution.

This method ensures that you only pay for a single execution instead of ongoing daily charges for a policy that has already processed all eligible data. For more information about cost, see ストレージライフサイクルポリシーの請求.

テーブルからポリシーを削除する

To remove a storage lifecycle policy from a table, use ALTER TABLE, as shown in the following example:

ALTER TABLE my_table DROP STORAGE LIFECYCLE POLICY;
Copy
  • This command removes all future policy executions for this table.

  • ポリシーの実行は、テーブルからドロップされる前に完了する場合があります。

  • ストレージライフサイクルポリシーをドロップするには、ポリシーがアタッチされているテーブルに対する OWNERSHIP 権限が必要です。