溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Greenplum -- 最全分區(qū)表操作

發(fā)布時(shí)間:2020-08-07 08:24:16 來源:網(wǎng)絡(luò) 閱讀:84989 作者:朱飛東 欄目:MySQL數(shù)據(jù)庫

一、Greenplum 分區(qū)原理

分區(qū)表意思是將一個(gè)大表在物理上分割成幾塊,GPDB中的分區(qū)表和PostgreSQL中實(shí)現(xiàn)原理一樣,都是用過表繼承、約束來實(shí)現(xiàn)。但是與PostgreSQL也有所不同,在PostgreSQL中,一個(gè)父表,多個(gè)子表來實(shí)現(xiàn)分區(qū)表,需要手動(dòng)向子表插入數(shù)據(jù),如果向父表插入數(shù)據(jù),則直接會(huì)被插入到父表中,在GPDB中,可以直接想父表插入數(shù)據(jù),便可以根據(jù)約束直接自動(dòng)向?qū)?yīng)的子表插入數(shù)據(jù),當(dāng)分區(qū)子表不存在時(shí),插入失敗
 

二、分區(qū)表創(chuàng)建

2.1、范圍分區(qū)(range)

根據(jù)分區(qū)字段的值范圍區(qū)間來分區(qū),每一個(gè)分區(qū)就是一個(gè)子表

eg:
create table test_partition_range
(
    id int, 
    name varchar(64), 
    fdate varchar(64)
    ) distributed by (id) 
    partition by range(fdate) 
    (
        partition p1 start ('2017-01-01') inclusive end ('2017-01-31') exclusive, 
        partition p2 start ('2017-02-01') inclusive end ('2017-02-29') exclusive, 
        default partition default_p
    );

    inclusive :指定包含,例如上面的 start ('2017-01-01') inclusive 則是包含'2017-01-01'
    exclusive : 指定不包含, 例如上面的 end ('2017-01-31') exclusive 則是不包含'2017-01-31'

 

2.2、快速分區(qū)(every)

根據(jù)選定的范圍,跨越基數(shù),快速分區(qū)每一個(gè)子表

eg:
create table test_partition_every_1 
(
    id int, 
    name varchar(64), 
    fdate date
) 
distributed by (id) 
partition by range (fdate) 
(
    partition pn_ start ('2017-01-01'::date) end ('2017-12-31'::date) every ('1 day'::interval), 
    default partition default_p
);

every:指定跨越基數(shù)

 

2.3、list分區(qū)(list)

根據(jù)值的分組,相同的數(shù)據(jù)歸類到一組,也就一個(gè)分區(qū)中

eg:
create table test_partition_list 
(
    id int, 
    name varchar(64), 
    fdate varchar(10)
) 
distributed by (id) 
partition by list (fdate) 
(
    partition p1 values ('2017-01-01', '2017-01-02'), 
    partition p2 values ('2017-01-03'), 
    default partition pd
);

 

三、分區(qū)相關(guān)操作

3.1、分區(qū)split

切割普通分區(qū):

將分區(qū)p2 在 '2017-02-20' 左右切分成兩塊
 alter table test_partition_range split partition p2 at ('2017-02-20') into (partition p2, partition p3); 

切割默認(rèn)分區(qū):
alter table test_partition_range split default partition start ('2017-03-01')  end ('2017-03-31')  into (partition p4, default partition);

 

3.2、分區(qū)add

如果存在default partition,則不能add分區(qū),只能split default partition

alter table test_partition_range_1 add partition p2 start ('2017-02-01') end ('2017-02-31');

 

3.3、分區(qū)drop

徹底刪除對(duì)應(yīng)的分區(qū)表

alter table test_partition_range_1 DROP partition p2;

 

3.4、分區(qū)truncate

清空分區(qū)表數(shù)據(jù),相當(dāng)于刪除分區(qū),然后再新建一個(gè)

alter table test_partition_range_1 truncate partition p1;

 

四、子分區(qū)創(chuàng)建與操作

4.1、子分區(qū)創(chuàng)建

在GPDB中,分區(qū)是可以嵌套增加的,分區(qū)下面可以有子分區(qū)

create table test_partition_range_2 
(
    id int, 
    name varchar(64), 
    fdate varchar(10)
) 
distributed by (id) 
partition by range(fdate) 
subpartition by list(name) 
subpartition template
(
    subpartition c1 values ('xiaoxiao'), 
    subpartition c2 values ('xiaohua')
)
(
    partition p1 start ('2017-01-01') end ('2017-01-31')
)
上面的分區(qū)中,p1會(huì)再分兩個(gè)c1/c2子分區(qū)

 

4.2、truncate 子分區(qū)

alter table test_partition_range_2 alter partition p1 truncate partition c2;

 

4.3、drop 子分區(qū)

alter table test_partition_range_2 alter partition p1 drop partition c2; 
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI