全部產品
Search
文件中心

MaxCompute:CLONE TABLE

更新時間:Jun 26, 2026

CLONE TABLE支援高效地將源表資料複製到目標表中,適用於表資料移轉情境。本文以具體樣本介紹如何使用CLONE TABLE功能。

適用範圍

  • Schema相容性:目標表需相容源表的Schema。

  • 分區數量限制

    • 目標表已存在時,單次最多可複製10,000個分區。

    • 目標表不存在時,無分區數量限制,滿足原子性。

  • 支援表類型

    • PK/Append Delta Table

      不支援覆蓋已有的 Delta Table,只能通過 CLONE TABLE 建立新的 Delta Table。

    • 普通表(分區表、非分區表、聚簇表)

    • Transactional 表

      不支援覆蓋已有的 Transactional 表,只能通過 CLONE TABLE 建立新的 Transactional 表。

  • 外部表格限制:不支援對外部表格使用CLONE TABLE命令複製表資料。

  • 行級許可權或資料脫敏策略

    不支援對設定了行級存取原則(Row Access Policy)或資料脫敏策略(Data Masking Policy)的表執行 CLONE TABLE。

  • 跨地區限制:不支援在跨地區或跨叢集(專案營運遷移期間)的MaxCompute專案之間使用CLONE TABLE命令複製表資料。

  • 跨儲存類型限制

    不支援在跨不同儲存類型的專案之間使用CLONE TABLE命令複製表資料,例如多 AZ 儲存專案與單 AZ 儲存專案之間互相剋隆。

    • 跨不同儲存類型的專案之間複製表推薦方案:

      • 非分區表採用CREATE TABLE AS方式遷移資料。

      • 分區表通過CREATE TABLE LIKE先建表,然後執行INSERT OVERWRITE遷移資料。

  • 不支援對已進行Schema Evolution(例如增刪列)的表執行CLONE操作。

費用說明

CLONE TABLE 是基於中繼資料的操作,不產生計算費用。複製產生的目標表會按實際儲存量計費。

命令格式

CLONE TABLE <[<src_project_name>.]<src_table_name>> [PARTITION(<pt_spec>), ...]
 TO <[<dest_project_name>.]<dest_table_name>> [IF EXISTS [OVERWRITE | IGNORE]] ;

參數說明

參數

是否必填

說明

src_project_name

源表所屬MaxCompute專案名稱。不指定時,預設為當前專案。當源表與目標表不屬於同一個MaxCompute專案時,需要攜帶此參數。

src_table_name

源表名稱。

pt_spec

源表的分區資訊。

格式為:(partition_col1 = partition_col_value1, partition_col2 = partition_col_value2, ...)。其中partition_col是分區欄位,partition_col_value是分區值。

dest_project_name

目標表所屬MaxCompute專案名稱。不指定時,預設為當前專案。當目標表與源表不屬於同一個MaxCompute專案時,需要攜帶此參數。

dest_table_name

目標表名稱。

  • 當目標表不存在時,CLONE TABLE命令會建立目標表,建立目標表使用的是CREATE TABLE LIKE語義。更多CREATE TABLE LIKE資訊,請參見建立和刪除表

  • 當目標表已存在並指定IF EXISTS OVERWRITE時,CLONE TABLE命令會覆蓋目標表或對應分區的資料。

  • 當目標表已存在並指定IF EXISTS IGNORE時,CLONE TABLE命令會跳過已存在分區,不會覆蓋目標表已有分區的資料。

樣本資料

為便於理解,本文提供來源資料,並基於來源資料提供相關樣本。分別建立分區表sale_detail和非分區表sale_detail_np,並添加資料,命令樣本如下:

分區表sale_detail

  1. 建立分區表

    -- 建立一張分區表sale_detail。
    CREATE TABLE IF NOT EXISTS sale_detail
    (
    shop_name     string,
    customer_id   string,
    total_price   double
    )
    PARTITIONED BY (sale_date string, region string);
    
    -- 向源表增加分區。
    ALTER TABLE sale_detail ADD PARTITION (sale_date='2013', region='china') PARTITION (sale_date='2014', region='shanghai');
    
    -- 向源表追加資料。
    INSERT INTO sale_detail PARTITION (sale_date='2013', region='china') VALUES ('s1','c1',100.1),('s2','c2',100.2),('s3','c3',100.3);
    INSERT INTO sale_detail PARTITION (sale_date='2014', region='shanghai') VALUES ('null','c5',null),('s6','c6',100.4),('s7','c7',100.5);
  2. 查詢分區表sale_detail中的資料,命令樣本如下:

    -- 開啟全表掃描,僅此Session有效。執行select語句查看錶sale_detail中的資料。
    SET odps.sql.allow.fullscan=true; 
    SELECT * FROM sale_detail;

    單擊查看返回結果

    -- 返回結果如下:
    +------------+-------------+-------------+------------+------------+
    | shop_name  | customer_id | total_price | sale_date  | region     |
    +------------+-------------+-------------+------------+------------+
    | s1         | c1          | 100.1       | 2013       | china      |
    | s2         | c2          | 100.2       | 2013       | china      |
    | s3         | c3          | 100.3       | 2013       | china      |
    | null       | c5          | NULL        | 2014       | shanghai   |
    | s6         | c6          | 100.4       | 2014       | shanghai   |
    | s7         | c7          | 100.5       | 2014       | shanghai   |
    +------------+-------------+-------------+------------+------------+

非分區表sale_detail_np

  1. 建立非分區表

    -- 建立一張非分區表sale_detail_np。
    CREATE TABLE IF NOT EXISTS sale_detail_np
    (
    shop_name     string,
    customer_id   string,
    total_price   double
    );
    
    -- 向源表追加資料。
    INSERT INTO sale_detail_np VALUES ('s4','c4',100.4);
  2. 查詢非分區表sale_detail_np中的資料,命令樣本如下:

    SELECT * FROM sale_detail_np;

    單擊查看返回結果

    -- 返回結果如下:
    +------------+-------------+-------------+
    | shop_name  | customer_id | total_price |
    +------------+-------------+-------------+
    | s4         | c4          | 100.4       |
    +------------+-------------+-------------+

使用樣本

基於樣本資料,CLONE TABLE命令的使用樣本如下:

樣本一:將非分區表資料全量複製到目標表

全量複製非分區表sale_detail_np的資料至目標表sale_detail_np_clone。

-- 複製表資料。
CLONE TABLE sale_detail_np TO sale_detail_np_clone;
-- 查看複製後目標表sale_detail_np_clone的資訊,驗證資料準確性。
SELECT * FROM sale_detail_np_clone;

單擊查看返回結果

-- 返回結果如下:
+------------+-------------+-------------+
| shop_name  | customer_id | total_price |
+------------+-------------+-------------+
| s4         | c4          | 100.4       |
+------------+-------------+-------------+

樣本二:將分區表指定分區資料複製到目標表

複製分區表sale_detail指定分區的資料至目標表sale_detail_clone。

-- 複製表資料
CLONE TABLE sale_detail PARTITION (sale_date='2013', region='china') TO sale_detail_clone IF EXISTS OVERWRITE;

-- 開啟全表掃描,執行select語句查看複製後目標表sale_detail_clone的資訊,驗證資料準確性。
SET odps.sql.allow.fullscan=true;
SELECT * FROM sale_detail_clone;

單擊查看返回結果

-- 返回結果如下:
+------------+-------------+-------------+------------+------------+
| shop_name  | customer_id | total_price | sale_date  | region     |
+------------+-------------+-------------+------------+------------+
| s1         | c1          | 100.1       | 2013       | china      |
| s2         | c2          | 100.2       | 2013       | china      |
| s3         | c3          | 100.3       | 2013       | china      |
+------------+-------------+-------------+------------+------------+

樣本三:將分區表資料全量複製到目標表並跳過目標表中已存在的分區

全量複製分區表sale_detail的資料至目標表sale_detail_clone(樣本2已產生的表)並跳過目標表中已存在的分區。

-- 複製表資料。
CLONE TABLE sale_detail TO sale_detail_clone IF EXISTS IGNORE;

-- 查看複製後目標表sale_detail_clone的資訊,驗證資料準確性。
-- 開啟全表掃描,僅此Session有效。執行select語句查看錶sale_detail_clone中的資料。
SET odps.sql.allow.fullscan=true; 
SELECT * FROM sale_detail_clone;

單擊查看返回結果

-- 返回結果如下:
+------------+-------------+-------------+------------+------------+
| shop_name  | customer_id | total_price | sale_date  | region     |
+------------+-------------+-------------+------------+------------+
| s1         | c1          | 100.1       | 2013       | china      |
| s2         | c2          | 100.2       | 2013       | china      |
| s3         | c3          | 100.3       | 2013       | china      |
| null       | c5          | NULL        | 2014       | shanghai   |
| s6         | c6          | 100.4       | 2014       | shanghai   |
| s7         | c7          | 100.5       | 2014       | shanghai   |
+------------+-------------+-------------+------------+------------+

樣本四:將分區表資料全量複製到目標表

全量複製分區表sale_detail的資料至目標表sale_detail_clone1。

-- 複製表資料。
CLONE TABLE sale_detail TO sale_detail_clone1;

-- 開啟全表掃描,執行select語句查看複製後目標表sale_detail_clone1的資訊,驗證資料準確性。
SET odps.sql.allow.fullscan=true; 
SELECT * FROM sale_detail_clone1;

單擊查看返回結果

-- 返回結果如下:
+------------+-------------+-------------+------------+------------+
| shop_name  | customer_id | total_price | sale_date  | region     |
+------------+-------------+-------------+------------+------------+
| s1         | c1          | 100.1       | 2013       | china      |
| s2         | c2          | 100.2       | 2013       | china      |
| s3         | c3          | 100.3       | 2013       | china      |
| null       | c5          | NULL        | 2014       | shanghai   |
| s6         | c6          | 100.4       | 2014       | shanghai   |
| s7         | c7          | 100.5       | 2014       | shanghai   |
+------------+-------------+-------------+------------+------------+

樣本五:複製Delta Table表

  • 複製Delta Table非分區表

    -- 建立Delta Table非分區表
    CREATE TABLE IF NOT EXISTS sale_detail_delta                                        
      (                                                                                   
        shop_name     STRING,                                                             
        customer_id   STRING,
        total_price   DOUBLE
      )
      TBLPROPERTIES ("table.format.version"="2");
    
    INSERT INTO sale_detail_delta VALUES ('s1','c1',100.1),('s2','c2',100.2);
    
    -- 複製樣本                             
    CLONE TABLE sale_detail_delta TO sale_detail_delta_clone;
  • 複製Delta Table分區表

    -- 建立Delta Table分區表
    CREATE TABLE IF NOT EXISTS sale_detail_delta_pt                                     
      (
        shop_name     STRING,                                                             
        customer_id   STRING,                 
        total_price   DOUBLE
      )
      PARTITIONED BY (dd STRING, hh STRING)
      TBLPROPERTIES ("table.format.version"="2");
    
    INSERT INTO sale_detail_delta_pt PARTITION (dd='01', hh='01') VALUES                
      ('s3','c3',100.3);
    
    -- 複製樣本
    CLONE TABLE sale_detail_delta_pt PARTITION (dd='01', hh='01') TO sale_detail_delta_pt_clone;  

最佳實務

實現同Region的MaxCompute專案資料移轉請參見使用CLONE TABLE實現同地區MaxCompute跨專案資料移轉