創建shard表
更新時間 2025-02-14 10:21:39
最近更新時間: 2025-02-14 10:21:39
分享文章
本文為您介紹如何創建shard表。
不指定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分布鍵選擇原則:
分布鍵只能選擇一個字段。
如果有主鍵,則選擇主鍵做分布鍵。
如果主鍵是復合字段組合,則選擇字段值選擇性多的字段做分布鍵。
也可以把復合字段拼接成一個新的字段來做分布鍵。
沒有主鍵的可以使用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 DATANODESdistribute by shard(x) 用于指定分布鍵,數據分布于那個節點就是根據這個字段值來計算分片。
to group xxx 用于指定存儲組(每個存儲組可以有多個節點)。
分布鍵字段值不能修改,字段長度不能修改,字段類型不能修改。