不用指定shard key 建表方式

最近更新时间: 2025-02-18 16:02:00

不指定shard key建表方法,系统默认使用第一个字段做为表的shard key。

  • 复制
    复制成功
postgres=# create table t_first_col_share(id serial not null,nickname text);
CREATE TABLE
postgres=# \d+ t_first_col_share
                                              Table "public.t_first_col_share"
  Column  |  Type   |                           Modifiers                            | Storage  | Stats target | Description 
----------+---------+----------------------------------------------------------------+----------+--------------+-------------
 id       | integer | not null default nextval('t_first_col_share_id_seq'::regclass) | plain    |              | 
 nickname | text    |                                                                | extended |              | 
Has OIDs: no
Distribute By SHARD(id)
        Location Nodes: ALL DATANODES

分布键选择原则:

  • 分布键只能选择一个字段。
  • 如果有主键,则选择主键做分布键。
  • 如果主键是复合字段组合,则选择字段值选择性多的字段做分布键。
  • 也可以把复合字段拼接成一个新的字段来做分布键。
  • 没有主键的可以使用UUID来做分布键。
  • 总之一定要让数据尽可能的分布得足够散。