在数据库的精细化管理中,分片技术以其卓越的数据分布和查询优化能力,成为提升数据库性能的关键。Gbase8s国产数据库系统,提供了强大的分片管理功能。本文将接上篇说明GBase8s分片表的管理策略,包括分片的初始化、修改、添加、删除以及合并与分离操作,旨在帮助数据库管理员和开发者高效地管理分片表,确保数据的高性能存取。
GBase8s分片表管理
GBase 8s提供对分片进行管理的一系列操作:
ALTER FRAGMENT ... INIT
用于初始化一个分片表
举例1:将一个分片表初始化成不分片表
drop table if exists tab1;
CREATE TABLE tab1(col1 int, col2 date) FRAGMENT BY ROUND ROBIN IN datadbs1, datadbs2, datadbs3;
alter fragment on table tab1 init in datadbs1;
举例2:将不分片表初始化成分片表
在上述用例的基础上继续如下操作
alter fragment on table tab1 init FRAGMENT BY ROUND ROBIN IN datadbs1, datadbs2, datadbs3;
举例3:修改分片策略
在上述用例的基础上继续如下操作
alter fragment on table tab1 init FRAGMENT BY EXPRESSION
col1 >= 0 and col1 < 100 in datadbs1,
col1 >= 100 and col1 < 200 in datadbs2;
ALTER FRAGMENT ... ADD (or DROP)
添加新的分片到现有的分片表或者索引 (或者抛弃)
举例1:在表达式分片表上增加一个分片
Drop table if exists tab1;
CREATE TABLE tab1 (col1 integer, col2 date)
FRAGMENT BY EXPRESSION
col1 >= 0 and col1 < 100 in datadbs1,
col1 >= 100 and col1 < 200 in datadbs2;
alter fragment on table tab1 add
col1 >= 200 and col1 < 300 in datadbs3;
举例2:在轮转法分片表上增加一个分片
Drop table if exists tab1;
CREATE TABLE tab1 (col1 integer, col2 date)
FRAGMENT BY ROUND ROBIN IN datadbs1, datadbs2,datadbs3;
alter fragment on table tab1 add datadbs4;
举例3:删除指定的分片
在上述用例的基础上继续如下操作
alter fragment on table tab1 drop datadbs3;
举例4:在索引上删除一个分片
Drop table if exists tab1;
CREATE TABLE tab1 (col1 integer, col2 date)
FRAGMENT BY EXPRESSION
col1 >= 0 and col1 < 100 in datadbs1,
col1 >= 100 and col1 < 200 in datadbs2,
col1 >= 200 and col1 < 300 in datadbs3;
CREATE INDEX idx_tab1 on tab1 (col1)
FRAGMENT BY EXPRESSION
col1 >= 0 and col1 < 100 in datadbs1,
col1 >= 100 and col1 < 200 in datadbs2,
col1 >= 200 and col1 < 300 in datadbs3;
alter fragment on INDEX idx_tab1 drop datadbs2;
!oncheck -pt testdb:tab1 |grep DB
Table fragment partition datadbs1 in DBspace datadbs1
Table fragment partition datadbs2 in DBspace datadbs2
Table fragment partition datadbs3 in DBspace datadbs3
Index idx_tab1 fragment partition datadbs1 in DBspace datadbs1
Index idx_tab1 fragment partition datadbs3 in DBspace datadbs3
ALTER FRAGMENT ... MODIFY
修改分片表或者分片索引的表达式,或者分片对应的dbspace。
举例1:修改分片表的表达式
Drop table if exists tab1;
CREATE TABLE tab1 (col1 integer, col2 date)
FRAGMENT BY EXPRESSION
col1 >= 0 and col1 < 100 in datadbs1,
col1 >= 100 and col1 < 200 in datadbs2,
col1 >= 200 and col1 < 300 in datadbs3;
alter fragment on table tab1 modify datadbs3 to col1>=200 in datadbs3;
举例2:修改分片表的dbspace
Drop table if exists tab1;
CREATE TABLE tab1 (col1 integer, col2 date)
FRAGMENT BY EXPRESSION
col1 >= 0 and col1 < 100 in datadbs1,
col1 >= 100 and col1 < 200 in datadbs2,
col1 >= 200 and col1 < 300 in datadbs3;
alter fragment on table tab1 modify datadbs3 to col1 >= 200 and col1 < 300 in datadbs4;
!oncheck -pt testdb:tab1 |grep DB
Table fragment partition datadbs1 in DBspace datadbs1
Table fragment partition datadbs2 in DBspace datadbs2
Table fragment partition datadbs4 in DBspace datadbs4
ALTER FRAGMENT ... ATTACH (or DETACH)
把具有相同结构的表合并成一个分片表,或者把分片分离成一个单独的表,下面将详细介绍分片表的attach和detach操作。
- ALTER FRAGMENT ... ATTACH:用来将一个表作为一个分片合并到分片表中
- ALTER FRAGMENT ...DETACH:操作将分片表的一个分片卸载到一个表中
分片表attach举例
用法如下:
Alter fragment on table tab1 ATTACH tab1 as partition p1, tab2 as partition p2;
或者
ALTER FRAGMENT ON TABLE tb1 ATTACH tb1 AS (a <= 100), tb2 AS (a > 100);
举例1:把两个结构完全相同的非分片表合并成一个分片表,两个表可以在不同的 dbspaces中或者在相同的 dbspace都可以。
Database testdb;
drop table if exists tab6_1;
drop table if exists tab6_2;
CREATE TABLE tab6_1 (col1 integer, col2 date) in datadbs1;
Create index tab6_1_indx on tab6_1(col1);
CREATE TABLE tab6_2 (col1 integer, col2 date) in datadbs2;
Create index tab6_2_indx on tab6_2(col1);
alter fragment on table tab6_1 ATTACH tab6_1 as partition p1, tab6_2 as partition p2;
!oncheck -pt testdb:tab6_1|grep DB
Your evaluation license will expire on 2025-03-30 00:00:00
Table fragment partition p1 in DBspace datadbs1
Table fragment partition p2 in DBspace datadbs2
Index tab6_1_indx fragment partition p1 in DBspace datadbs1
Index tab6_1_indx fragment partition p2 in DBspace datadbs2
--或者带表达式
drop table if exists tab6_1;
drop table if exists tab6_2;
CREATE TABLE tab6_1 (col1 integer, col2 date) in datadbs1;
Create index tab6_1_indx on tab6_1(col1);
CREATE TABLE tab6_2 (col1 integer, col2 date) in datadbs2;
Create index tab6_2_indx on tab6_2(col1);
Alter fragment on table tab6_1 ATTACH tab6_1 as partition p11(col1 >= 0 and col1 < 200) , tab6_2 as partition p22(col1 >= 100 and col1 < 200);
!oncheck -pt testdb:tab6_1|grep DB
Table fragment partition p11 in DBspace datadbs1
Table fragment partition p22 in DBspace datadbs2
Index tab6_1_indx fragment partition p11 in DBspace datadbs1
Index tab6_1_indx fragment partition p22 in DBspace datadbs2
举例2:把一个非分片表合并成为一个分片表的一个分区
Database testdb;
Drop table if exists tab7;
Drop table if exists tab8;
CREATE TABLE tab7 (col1 int, col2 date)
FRAGMENT BY EXPRESSION
Partition p1( col1 >= 0 and col1 < 100) in datadbs1,
Partition p2 (col1 >= 100 and col1 < 200) in datadbs2;
CREATE TABLE tab8 (col1 int, col2 date,check(col1>=200 and col1<300)) in datadbs3;
Alter fragment on table tab7 attach tab8 as Partition p3 (col1>=200 and col1<300);
!oncheck -pt testdb:tab7 |grep DB
Table fragment partition p1 in DBspace datadbs1
Table fragment partition p2 in DBspace datadbs2
Table fragment partition p3 in DBspace datadbs3
举例3:我们可以使用 BEFORE 和 AFTER 子句,用来调整表达式的顺序。
Database testdb;
Drop table if exists tab7;
Drop table if exists tab8;
CREATE TABLE tab7 (col1 int, col2 date)
FRAGMENT BY EXPRESSION
col1 >= 0 and col1 < 100 in datadbs1,
col1 >= 100 and col1 < 200 in datadbs2;
CREATE TABLE tab8 (col1 int, col2 date) in datadbs3;
Alter fragment on table tab7 attach tab8 as (col1<0) before datadbs1;
!oncheck -pt testdb:tab7 |grep DB
Table fragment partition datadbs3 in DBspace datadbs3
Table fragment partition datadbs1 in DBspace datadbs1
Table fragment partition datadbs2 in DBspace datadbs2
--或者
Drop table if exists tab7;
Drop table if exists tab8;
CREATE TABLE tab7 (col1 int, col2 date)
FRAGMENT BY EXPRESSION
Partition p1( col1 >= 0 and col1 < 100) in datadbs1,
Partition p2 (col1 >= 100 and col1 < 200) in datadbs2;
CREATE TABLE tab8 (col1 int, col2 date) in datadbs3;
Alter fragment on table tab7 attach tab8 as Partition p3 (col1>=200 and col1<300) after p2;
!oncheck -pt testdb:tab7 |grep DB
Table fragment partition p1 in DBspace datadbs1
Table fragment partition p2 in DBspace datadbs2
Table fragment partition p3 in DBspace datadbs3
使用注意点:
- 在目标表中使用Check constraint 来避免在ATTACHE期间不必要的完整性检查。
- 对于目标表,外键,主键,唯一性约束,和serial类型的字段都是不允许的。
- 索引重建是可以避免的,如果:数据没有重叠
- 新分片的索引与目标表的索引是建立在相同的一个列或者多个列上的。
- 索引与目标表的索引具有相同的属性,例如: unique, duplicate。
- 新分片的索引不能在任何一个目标表的索引所使用的dbspaces中。
分片表detach举例
用法如下:
Alter fragment on table f1 DETACH dbspace2 f2
或者
Alter fragment on table mytab1 DETACH partition p2 mytab2
如果索引的分片策略和表的分片策略不同,则需要索引重建 (如果相同, 对应与被detached的分片的索引将被抛弃),数据库会自动处理索引,无需手动干预。
--索引的分片策略和表的分片策略相同
CREATE TABLE tab9 (a int) FRAGMENT BY EXPRESSION
(a >=0 AND a < 5) IN datadbs1,
(a >=5 AND a <10) IN datadbs2,
(a >=10 AND a <15) IN datadbs3;
CREATE INDEX idx_tab9 ON tab9 (a);
!oncheck -pt testdb:tab9 |grep DB
Table fragment partition datadbs1 in DBspace datadbs1
Table fragment partition datadbs2 in DBspace datadbs2
Table fragment partition datadbs3 in DBspace datadbs3
Index idx_tab9 fragment partition datadbs1 in DBspace datadbs1
Index idx_tab9 fragment partition datadbs2 in DBspace datadbs2
Index idx_tab9 fragment partition datadbs3 in DBspace datadbs3
ALTER FRAGMENT ON TABLE tab9 DETACH datadbs3 tab10;
!oncheck -pt testdb:tab9 |grep DB
Table fragment partition datadbs1 in DBspace datadbs1
Table fragment partition datadbs2 in DBspace datadbs2
Index idx_tab9 fragment partition datadbs1 in DBspace datadbs1
Index idx_tab9 fragment partition datadbs2 in DBspace datadbs2
--索引的分片策略和表的分片策略不同
CREATE TABLE tab11 (a int, b int) FRAGMENT BY EXPRESSION
(a >=0 AND a < 5) IN datadbs1,
(a >=5 AND a <10) IN datadbs2,
(a >=10 AND a <15) IN datadbs3;
CREATE INDEX idx_tab11 on tab11(a) FRAGMENT BY EXPRESSION
(a >=0 AND a< 10) IN datadbs3,
(a >=5 AND a< 15) IN datadbs4;
!oncheck -pt testdb:tab11 |grep DB
Table fragment partition datadbs1 in DBspace datadbs1
Table fragment partition datadbs2 in DBspace datadbs2
Table fragment partition datadbs3 in DBspace datadbs3
Index idx_tab11 fragment partition datadbs3 in DBspace datadbs3
Index idx_tab11 fragment partition datadbs4 in DBspace datadbs4
ALTER FRAGMENT ON TABLE tab11 DETACH datadbs3 tab12 ;
!oncheck -pt testdb:tab11 |grep DB
Your evaluation license will expire on 2025-03-30 00:00:00
Table fragment partition datadbs1 in DBspace datadbs1
Table fragment partition datadbs2 in DBspace datadbs2
Index idx_tab11 fragment partition datadbs3 in DBspace datadbs3
Index idx_tab11 fragment partition datadbs4 in DBspace datadbs4
GBase 8s的分片表管理功能为数据库的高性能运行提供了坚实的保障。通过本系列文章的详细介绍,我们希望能够帮助用户深入理解分片管理的各个方面,掌握分片策略的制定和调整,从而在实际应用中发挥GBase 8s的强大性能。随着技术的不断进步和业务需求的不断变化,GBase 8s将继续为用户提供更加全面和高效的数据库解决方案。