创建分区表
 
                  更新时间 2025-02-05 09:36:26
                 
 
                    最近更新时间: 2025-02-05 09:36:26
                  
 本文为您介绍如何创建分区表。
 创建范围分区表
teledb=# create table t_range (f1 bigint,f2 timestamp default now(), f3 integer) partition by range (f3) begin (1) step (50) partitions (3) distribute by shard(f1);
CREATE TABLE
teledb=# insert into t_range(f1,f3) values(1,1),(2,50),(3,100),(2,110);
COPY 4
teledb=# insert into t_range(f1,f3) values(1,1),(2,50),(3,100),(2,110);
COPY 4
teledb=# \d+ t_range;
                                            Table "public.t_range"
 Column |            Type             | Collation | Nullable | Default | Storage | Stats target | Description 
--------+-----------------------------+-----------+----------+---------+---------+--------------+-------------
 f1     | bigint                      |           |          |         | plain   |              | 
 f2     | timestamp without time zone |           |          | now()   | plain   |              | 
 f3     | integer                     |           |          |         | plain   |              | 
Distribute By: SHARD(f1)
Location Nodes: ALL DATANODES
Partition By: RANGE(f3)
	 # Of Partitions: 3
	 Start With: 1
	 Interval Of Partition: 50
创建时间范围分区表
teledb=# create table t_time_range
(f1 bigint, f2 timestamp ,f3 bigint) 
partition by range (f2) begin (timestamp without time zone '2017-09-01 0:0:0') 
step (interval '1 month') 
partitions (12) distribute by shard(f1);
CREATE TABLE
teledb=# \d+ t_time_range
                                         Table "public.t_time_range"
 Column |            Type             | Collation | Nullable | Default | Storage | Stats target | Description 
--------+-----------------------------+-----------+----------+---------+---------+--------------+-------------
 f1     | bigint                      |           |          |         | plain   |              | 
 f2     | timestamp without time zone |           |          |         | plain   |              | 
 f3     | bigint                      |           |          |         | plain   |              | 
Distribute By: SHARD(f1)
Location Nodes: ALL DATANODES
Partition By: RANGE(f2)
	 # Of Partitions: 12
	 Start With: 2017-09-01
	 Interval Of Partition: 1 MONTH
说明
partion by range(xx) 代表范围分区, 支持timesamp,int类型,数据分布于那个子表就是根据这个字段值来计算分区。
begin(xx) 表示开始分区的初始值。
partition(xx) 代表初始分区数量。
step (xx) 代表分区步长。
