表分区-保留基础和分区子级?

时间:2018-08-23 10:00:52

标签: sql postgresql partitioning

我有一个表结构,我想按时间戳在一天的范围内进行分区。

它由两个具有共同字段的表组成,为简单起见,我将使用表A,B的示例,如下所示:

create table A (
    id serial not null, 
    ts timestamp without time zone not null,
    name text,
    some_custom_A_field_1 numeric,
    some_custom_A_field_2 numeric
);

create table B (
    id serial not null, 
    ts timestamp without time zone not null,
    name text,
    some_custom_B_field_1 numeric,
    some_custom_B_field_2 numeric,
    some_custom_B_field_3 numeric,
    some_custom_B_field_4 numeric
);

所以我有两个选择,我不确定哪个是首选。

选项1-创建基表,继承子分区:

create table Base (
    id serial not null, 
    ts timestamp without time zone not null,
    name text
)

create table ABase (
    some_custom_A_field_1 numeric,
    some_custom_A_field_2 numeric
)INHERITS (Base );

create table BBase (
    some_custom_B_field_1 numeric,
    some_custom_B_field_2 numeric,
    some_custom_B_field_3 numeric,
    some_custom_B_field_4 numeric
)INHERITS (Base );

CREATE TABLE a_2018_06_24 (CHECK (ts >= '2018-06-24 00:00:00' AND ts < '2018-06-25 00:00:00')) INHERITS (ABase);
CREATE TABLE b_2018_06_24 (CHECK (ts >= '2018-06-24 00:00:00' AND ts < '2018-06-25 00:00:00')) INHERITS (BBase);

选项2-两个基本表已分区:

create table ABase (
    id serial not null, 
    ts timestamp without time zone not null,
    name text,
    some_custom_A_field_1 numeric,
    some_custom_A_field_2 numeric
);

create table BBase (
    id serial not null, 
    ts timestamp without time zone not null,
    name text,
    some_custom_B_field_1 numeric,
    some_custom_B_field_2 numeric,
    some_custom_B_field_3 numeric,
    some_custom_B_field_4 numeric
);

CREATE TABLE a_2018_06_24 (CHECK (ts >= '2018-06-24 00:00:00' AND ts < '2018-06-25 00:00:00')) INHERITS (ABase);
CREATE TABLE b_2018_06_24 (CHECK (ts >= '2018-06-24 00:00:00' AND ts < '2018-06-25 00:00:00')) INHERITS (BBase);

其他信息:

  1. 我正在使用9.6版
  2. 表A每天约200万条记录,表B每天约1.2亿条记录。
  3. 用例-每天一次将数据提取到表中,运行选择查询(基于时间序列)。

什么是更好的做法,优点/缺点?

谢谢。

0 个答案:

没有答案