創建shard表
更新時間 2025-02-14 10:21:48
最近更新時(shi)間(jian): 2025-02-14 10:21:48
分享文(wen)章
本文為您介紹如何創建shard表。
不指定(ding)shard key 建表方式
不指(zhi)定shard key 建(jian)表方法(fa),系統默認使用第(di)一個(ge)字段作為表的 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分布鍵選(xuan)擇原則:
分(fen)布鍵只能選擇一個字段。
如果有主鍵,則選擇主鍵做分布鍵。
如果主鍵(jian)是復合字段(duan)(duan)組合,則選擇(ze)字段(duan)(duan)值選擇(ze)性多的字段(duan)(duan)做分布鍵(jian)。
也(ye)可以(yi)把復合字段(duan)拼接(jie)成一(yi)個新的字段(duan)來做分布(bu)鍵。
沒有主鍵的可以(yi)使用UUID 來做(zuo)分布鍵。
總之一(yi)定要讓數據盡可能的分布得足夠散。
指定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) 用于(yu)指定分(fen)布(bu)鍵,數據分(fen)布(bu)于(yu)那個節點就是(shi)根據這個字段值來計算(suan)分(fen)片。
to group xxx 用于(yu)指定存儲組(zu)(每個存儲組(zu)可以有多個節點(dian))。
分(fen)布鍵字(zi)段(duan)值不能修(xiu)改,字(zi)段(duan)長度不能修(xiu)改,字(zi)段(duan)類型不能修(xiu)改