将标准表转换为无限表 - Amazon Aurora
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

将标准表转换为无限表

您可以将标准表转换为分片表或引用表。在转换过程中,数据从标准表移动到分布式表,然后删除源标准表。使用 INSERT INTO SELECT FROM 命令移动数据。

创建分片表

您可以通过在标准表上运行 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 约束条件不支持表达式。

有关更多信息,请参阅 约束

创建分片表

以下示例说明如何使用分片键 customer_id 创建 customer 分片表。

  1. 创建标准表。

    CREATE TABLE customer (customer_id INT PRIMARY KEY NOT NULL, zipcode INT, email VARCHAR);
  2. 将标准表转换为分片表。

    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)

创建并置表

如果使用相同的分片键对两个或多个表进行分片,则可以显式对齐(并置)这些表。当两个或多个表并置时,这些表中具有相同分片键值的行将放在同一个分片上。并置有助于将某些操作限制在单个分片上,从而提高性能。

您可以使用具有以下语法的 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. 按照 创建分片表 中的流程创建第一个分片表。

  2. 为并置表创建标准表。

    CREATE TABLE mytable2 (customer_id INT PRIMARY KEY NOT NULL, column1 INT, column2 VARCHAR);
  3. 将标准表转换为并置表。

    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)

创建引用表

可以通过在标准表上运行 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);
  2. 将标准表转换为引用表。

    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 引用表。