執行深層複製 - Amazon Redshift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

執行深層複製

深層複製會使用會自動排序資料表的大量插入來重新建立和重新填入資料表。如果資料表有大型未排序的區域,深層複製較清空的速度快得多。建議您僅在可以追蹤更新時,再於深層複製操作期間進行並行更新。該程序完成後,將差異更新移到新資料表中。VACUUM 操作自動支援並行更新。

您可以選擇以下其中一個方法來建立原始資料表的複本:

  • 使用原始資料表 DDL。

    如果 CREATE TABLE DDL 可供使用,則這是最快速和偏好的方法。如果建立新的資料表,您可以指定所有資料表和資料欄屬性,包括主索引鍵和外部索引鍵。您可以透過使用 SHOW TABLE 功能找到原始的 DDL。

  • 使用 CREATE TABLE LIKE。

    如果原始 DDL 無法使用,您可以使用 CREATE TABLE LIKE 來重新建立原始資料表。新的資料表會繼承父資料表的編碼、分佈索引鍵、排序索引鍵和非 null 屬性。新資料表不會繼承父資料表的主索引鍵和外部索引鍵屬性,但您可以使用 ALTER TABLE 新增它們。

  • 建立暫時資料表並截斷原始資料表。

    如果您必須保留父資料表的主索引鍵和外部索引鍵屬性。如果父資料表具有相依性,您可以使用 CREATE TABLE... AS (CTAS) 來建立暫時資料表。然後截斷原始資料表並從暫時資料表填入它。

    相較於使用永久資料表,使用暫時資料表可大幅改善效能,但有遺失資料的風險。暫時資料表在結束建立它所在的工作階段中時自動捨棄。TRUNCATE 會立即認可,即使它在交易區塊內。如果 TRUNCATE 成功但工作階段在後續 INSERT 完成之前關閉,則資料會遺失。如果無法接受資料遺失,請使用永久資料表。

建立資料表副本之後,您可能必須授與新資料表的存取權。您可以使用 GRANT 來定義存取權限。若要檢視並授與資料表的所有存取權限,您必須是下列其中一個角色:

  • 超級使用者。

  • 您想要複製之資料表的擁有者。

  • 具有 ACCESS SYSTEM TABLE 權限可查看資料表權限,且具有所有相關許可授權的使用者。

此外,您可能必須針對深層複製所在的結構描述授與使用許可。如果深層複製的結構描述與原始資料表的結構描述不同,且也不是 public 結構描述,則必須授與使用許可。若要檢視並授與使用權限,您必須是下列其中一個角色:

  • 超級使用者。

  • 可針對深層複製結構描述授與 USES 許可的使用者。

使用原始資料表 DDL 來執行深層複製
  1. (選用) 執行名為 v_generate_tbl_ddl 的指令碼來重新建立資料表 DDL。

  2. 使用原始 CREATE TABLE DDL 建立資料表的複本。

  3. 使用 INSERT INTO … SELECT 陳述式以來自原始資料表的資料填入該複本。

  4. 檢查舊資料表上授與的權限。您可以在 SVV_RELATION_PRIVILEGES 系統檢視中看到這些許可。

  5. 如有必要,請對新資料表授與舊資料表的許可。

  6. 針對在原始資料表中具有權限的每個群組和使用者授與使用許可。如果您的深層複製資料表位於 public 結構描述中,或與原始資料表位於相同的結構描述中,則不需要執行此步驟。

  7. 捨棄原始資料表。

  8. 使用 ALTER TABLE 陳述式將複本重新命名為原始資料表名稱。

下列範例會使用名為 sample_copy 的 SAMPLE 複本,在 SAMPLE 資料表上執行深層複製。

--Create a copy of the original table in the sample_namespace namespace using the original CREATE TABLE DDL. create table sample_namespace.sample_copy ( … ); --Populate the copy with data from the original table in the public namespace. insert into sample_namespace.sample_copy (select * from public.sample); --Check SVV_RELATION_PRIVILEGES for the original table's privileges. select * from svv_relation_privileges where namespace_name = 'public' and relation_name = 'sample' order by identity_type, identity_id, privilege_type; --Grant the original table's privileges to the copy table. grant DELETE on table sample_namespace.sample_copy to group group1; grant INSERT, UPDATE on table sample_namespace.sample_copy to group group2; grant SELECT on table sample_namespace.sample_copy to user1; grant INSERT, SELECT, UPDATE on table sample_namespace.sample_copy to user2; --Grant usage permission to every group and user that has privileges in the original table. grant USAGE on schema sample_namespace to group group1, group group2, user1, user2; --Drop the original table. drop table public.sample; --Rename the copy table to match the original table's name. alter table sample_namespace.sample_copy rename to sample;
使用 CREATE TABLE LIKE 來執行深層複製
  1. 使用 CREATE TABLE LIKE 來建立新的資料表。

  2. 使用 INSERT INTO … SELECT 陳述式將資料列從目前資料表複製到新資料表。

  3. 檢查舊資料表上授與的權限。您可以在 SVV_RELATION_PRIVILEGES 系統檢視中看到這些許可。

  4. 如有必要,請對新資料表授與舊資料表的許可。

  5. 針對在原始資料表中具有權限的每個群組和使用者授與使用許可。如果您的深層複製資料表位於 public 結構描述中,或與原始資料表位於相同的結構描述中,則不需要執行此步驟。

  6. 捨棄目前的資料表。

  7. 使用 ALTER TABLE 陳述式將新資料表重新命名為原始資料表名稱。

下列範例會使用 CREATE TABLE LIKE 在 SAMPLE 資料表上執行深層複製。

--Create a copy of the original table in the sample_namespace namespace using CREATE TABLE LIKE. create table sameple_namespace.sample_copy (like public.sample); --Populate the copy with data from the original table. insert into sample_namespace.sample_copy (select * from public.sample); --Check SVV_RELATION_PRIVILEGES for the original table's privileges. select * from svv_relation_privileges where namespace_name = 'public' and relation_name = 'sample' order by identity_type, identity_id, privilege_type; --Grant the original table's privileges to the copy table. grant DELETE on table sample_namespace.sample_copy to group group1; grant INSERT, UPDATE on table sample_namespace.sample_copy to group group2; grant SELECT on table sample_namespace.sample_copy to user1; grant INSERT, SELECT, UPDATE on table sample_namespace.sample_copy to user2; --Grant usage permission to every group and user that has privileges in the original table. grant USAGE on schema sample_namespace to group group1, group group2, user1, user2; --Drop the original table. drop table public.sample; --Rename the copy table to match the original table's name. alter table sample_namespace.sample_copy rename to sample;
建立暫時資料表並截斷原始資料表來執行深層複製
  1. 使用 CREATE TABLE AS 以來自原始資料表的資料列建立暫時資料表。

  2. 截斷目前的資料表。

  3. 使用 INSERT INTO … SELECT 陳述式將資料列從暫時資料表複製到原始資料表。

  4. 捨棄暫時資料表。

下列範例會透過建立暫存資料表和截斷原始資料表,在 SALES 資料表上執行深層複製。由於原始資料表仍會保留,因此您不需要對複製資料表授與許可。

--Create a temp table copy using CREATE TABLE AS. create temp table salestemp as select * from sales; --Truncate the original table. truncate sales; --Copy the rows from the temporary table to the original table. insert into sales (select * from salestemp); --Drop the temporary table. drop table salestemp;