

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

# 將標準資料表轉換為無限資料表
<a name="limitless-converting-standard"></a>

您可以將標準資料表轉換為碎片或參考資料表。在轉換期間，資料會從標準資料表移至分散式資料表，然後來源標準資料表會遭到刪除。使用 `INSERT INTO SELECT FROM` 命令移動資料。

**Contents**
+ [建立碎片資料表](#limitless-creating-sharded)
+ [建立共置資料表](#limitless-creating-sharded.colocated)
+ [建立參考資料表](#limitless-creating-reference)

## 建立碎片資料表
<a name="limitless-creating-sharded"></a>

您可以在標準資料表執行 `rds_aurora.limitless_alter_table_type_sharded` 程序來建立碎片資料表。此程序會採用標準資料表和欄清單，然後使用欄清單作為碎片索引鍵來分配指定的資料表。程序會同步執行，以及在資料表取得 `ACCESS EXCLUSIVE` 鎖定。

程序成功完成後，系統會刪除來源標準資料表，而同名的碎片資料表會變成可用。

`rds_aurora.limitless_alter_table_type_sharded` 程序會使用以下語法：

```
postgres=> CALL rds_aurora.limitless_alter_table_type_sharded('{{schema}}.{{table}}', ARRAY['{{shard_key1}}', '{{shard_key2}}', ... '{{shard_keyn}}']);
```

此程序需要下列參數：
+ `schema`：包含要碎片處理之資料表的資料庫結構描述。如果未指定結構描述，則程序會使用 `search_path`。
+ `table`：要碎片處理的資料表。
+ `shard_keyn`：要用作碎片索引鍵的資料表欄陣列。

  碎片索引鍵值是字串常值，因此區分大小寫。如果碎片索引鍵包含單引號 (')，請使用另一個單引號來加以逸出。例如，如果資料表欄名為 `customer's id`，請使用 `customer''s id` 作為碎片索引鍵。不需要逸出反斜線 (\\) 和雙引號 (")。

**注意**  
所有主要和唯一索引鍵都必須包含碎片索引鍵。這表示碎片索引鍵是主要或唯一索引鍵的子集。  
在碎片資料表中，`CHECK` 限制條件不支援表達式。  
如需詳細資訊，請參閱[Constraints](limitless-reference.DDL-limitations.md#limitless-reference.DDL-limitations.Constraints)。

**建立碎片資料表**

下列範例示範如何使用碎片索引鍵 `customer_id` 建立 `customer` 碎片資料表。

1. 建立標準資料表。

   ```
   CREATE TABLE customer (customer_id INT PRIMARY KEY NOT NULL, zipcode INT, email VARCHAR);
   ```

1. 將標準資料表轉換為碎片資料表。

   ```
   postgres=> CALL rds_aurora.limitless_alter_table_type_sharded('public.customer', ARRAY['customer_id']);
   
   postgres=> \d
   
                       List of relations
    Schema |     Name     |       Type        |       Owner
   --------+--------------+-------------------+--------------------
    public | customer     | partitioned table | postgres_limitless
    public | customer_fs1 | foreign table     | postgres_limitless
    public | customer_fs2 | foreign table     | postgres_limitless
    public | customer_fs3 | foreign table     | postgres_limitless
    public | customer_fs4 | foreign table     | postgres_limitless
    public | customer_fs5 | foreign table     | postgres_limitless
   (6 rows)
   ```

## 建立共置資料表
<a name="limitless-creating-sharded.colocated"></a>

如果使用相同的碎片索引鍵分割二或多個資料表，您可以將那些資料表明確對齊 (共置)。將二或多個資料表共置時，會將具有相同碎片索引鍵值的資料表中的列放置在相同的碎片。共置有助於將某些操作限制為單一碎片，進而獲得更好的效能。

您使用 `rds_aurora.limitless_alter_table_type_sharded` 程序搭配以下語法：

```
postgres=> CALL rds_aurora.limitless_alter_table_type_sharded('{{schema}}.{{collocated_table}}', ARRAY['{{shard_key1}}', '{{shard_key2}}', ... '{{shard_keyn}}'], '{{schema}}.{{sharded_table}}');
```

此程序需要下列參數：
+ `schema`：資料庫結構描述，其中包含要共置的資料表。如果未指定結構描述，則程序會使用 `search_path`。
+ `collocated_table`：要共置的資料表。
+ `shard_keyn`：要用作碎片索引鍵的資料表欄陣列。

  您必須使用與原始碎片資料表相同的碎片索引鍵，包括相同的欄名稱和欄類型。
+ `sharded_table`：您要共置 `collocated_table` 時所用的碎片資料表。

**建立共置資料表**

1. 遵循[建立碎片資料表](#limitless-creating-sharded)中的程序建立第一個碎片資料表。

1. 建立共置資料表的標準資料表。

   ```
   CREATE TABLE mytable2 (customer_id INT PRIMARY KEY NOT NULL, column1 INT, column2 VARCHAR);
   ```

1. 將標準資料表轉換為共置資料表。

   ```
   postgres=> CALL rds_aurora.limitless_alter_table_type_sharded('public.mytable2', 
   ARRAY['customer_id'], 'public.customer');
   
   postgres=> \d
   
                       List of relations
    Schema |     Name     |       Type        |       Owner
   --------+--------------+-------------------+--------------------
    public | customer     | partitioned table | postgres_limitless
    public | customer_fs1 | foreign table     | postgres_limitless
    public | customer_fs2 | foreign table     | postgres_limitless
    public | customer_fs3 | foreign table     | postgres_limitless
    public | customer_fs4 | foreign table     | postgres_limitless
    public | customer_fs5 | foreign table     | postgres_limitless
    public | mytable2     | partitioned table | postgres_limitless
    public | mytable2_fs1 | foreign table     | postgres_limitless
    public | mytable2_fs2 | foreign table     | postgres_limitless
    public | mytable2_fs3 | foreign table     | postgres_limitless
    public | mytable2_fs4 | foreign table     | postgres_limitless
    public | mytable2_fs5 | foreign table     | postgres_limitless
   (12 rows)
   ```

## 建立參考資料表
<a name="limitless-creating-reference"></a>

您可以在標準資料表執行 `rds_aurora.limitless_alter_table_type_reference` 程序來建立參考資料表。此程序會將指定的資料表複寫至資料庫碎片群組中的所有碎片，並將資料表類型變更為參考。程序會同步執行，以及在資料表取得 `ACCESS EXCLUSIVE` 鎖定。

程序成功完成後，系統會刪除來源標準資料表，而同名的參考資料表會變成可用。

`rds_aurora.limitless_alter_table_type_reference` 程序會使用以下語法：

```
postgres=> CALL rds_aurora.limitless_alter_table_type_reference('{{schema}}.{{table}}');
```

預存程序需要下列參數：
+ `schema`：包含要複寫之資料表的資料庫結構描述。如果未指定結構描述，則程序會使用 `search_path`。
+ `table`：要複寫的資料表。

**注意**  
您從中建立參考資料表的標準資料表必須具有主索引鍵。  
在參考資料表中，`CHECK` 限制條件不支援表達式。  
先前的函數 (`limitless_table_alter_type_reference`) 已遭棄用。

**建立參考資料表**

下列範例示範如何建立 `zipcodes` 參考資料表。

1. 建立標準資料表。

   ```
   CREATE TABLE zipcodes (zipcode INT PRIMARY KEY, details VARCHAR);
   ```

1. 將標準資料表轉換為參考資料表。

   ```
   CALL rds_aurora.limitless_alter_table_type_reference('public.zipcodes');
   
   postgres=> \d
   
                       List of relations
    Schema |     Name     |       Type        |       Owner
   --------+--------------+-------------------+--------------------
    public | customer     | partitioned table | postgres_limitless
    public | customer_fs1 | foreign table     | postgres_limitless
    public | customer_fs2 | foreign table     | postgres_limitless
    public | customer_fs3 | foreign table     | postgres_limitless
    public | customer_fs4 | foreign table     | postgres_limitless
    public | customer_fs5 | foreign table     | postgres_limitless
    public | zipcodes     | foreign table     | postgres_limitless
   (7 rows)
   ```

   輸出會顯示 `customer` 碎片資料表和 `zipcodes` 參考資料表。