创建shard表
 
                  更新时间 2025-02-14 10:21:43
                 
 
                    最近更新时间: 2025-02-14 10:21:43
                  
 本文为您介绍如何创建shard表。
 不指定shard key 建表方式
不指定shard key 建表方法,系统默认使用第一个字段作为表的 shard key。
teledb=# create table test(id int, name varchar, birth date);
CREATE TABLE
teledb=# \d+ test 
Table "public.test"
Column| Type | Collation| Nullable |Default | Storage  | Stats target | Description 
--------+-------+-----+----------+---------+---+---------+-------------
id     | integer   |        |          |         | plain    |              | 
name  | character varying   |     |       |      | extended |              | 
birth  | timestamp(0) without time zone |   |    |   | plain    |          | 
Distribute By: SHARD(id)
Location Nodes: ALL DATANODES分布键选择原则:
- 分布键只能选择一个字段。 
- 如果有主键,则选择主键做分布键。 
- 如果主键是复合字段组合,则选择字段值选择性多的字段做分布键。 
- 也可以把复合字段拼接成一个新的字段来做分布键。 
- 没有主键的可以使用UUID 来做分布键。 
总之一定要让数据尽可能的分布得足够散。
指定shard key 建表方式
teledb=# create table test_shard(id serial not null, name text) distribute by shard(name) to group default_group;
CREATE TABLE
teledb=# \d+ test_shard
Table "public.test_shard"
Column |  Type   | Collation | Nullable |     Default           | Storage  | Stats target | Description 
--------+---------+-----------+----------+------------------------------+----------+--------------+-------------
id   | integer |  |   not null |  nextval('test_shard_id_seq'::regclass) | plain |         | 
name | text  |   |         |                      | extended |              | 
Distribute By: SHARD(name)
Location Nodes: ALL DATANODES- distribute by shard(x) 用于指定分布键,数据分布于那个节点就是根据这个字段值来计算分片。 
- to group xxx 用于指定存储组(每个存储组可以有多个节点)。 
- 分布键字段值不能修改,字段长度不能修改,字段类型不能修改。 
