創建shard表
更新時間 2025-02-05 09:36:26
最近更新時間: 2025-02-05 09:36:26
分享文章
本文為您介紹如何創建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 用于指定存儲組(每個存儲組可以有多個節點)。
- 分布鍵字段值不能修改,字段長度不能修改,字段類型不能修改。