Create and manage storage lifecycle policies

참고

:doc:`저장소 수명 주기 정책</user-guide/storage-management/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                                             |
;                                                                    |
---------------------------------------------------------------------+

테이블에 대한 저장소 수명 주기 정책 관리하기

다음 옵션을 사용하여 저장소 수명 주기 정책 첨부 파일을 관리합니다.

테이블에 정책 연결하기

하나의 저장소 수명 주기 정책으로 여러 테이블을 관리할 수 있습니다. 테이블을 생성하거나 변경할 때 정책을 연결합니다.

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 섹션을 참조하십시오.

  • 테이블에는 저장소 수명 주기 정책을 하나만 연결할 수 있습니다.

  • 열 개수는 정책 함수 서명의 인자 개수와 일치해야 하며 열 데이터는 인자 유형과 호환되어야 합니다.

  • 테이블 열의 이름을 바꿔도 관련 정책은 영향을 받지 않습니다. 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

정책을 일회성 작업으로 적용하기

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 권한이 있어야 합니다.