SAP HANA提供了三种分区方式:Hash分区、Range分区以及Roundrobin分区。前两者是针对某个或联合字段进行分区,区别在于hash分区是对字段的哈希值进行分区,而range分区则是定义了字段的取值范围。一般来说,如果涉及到年月日时间字段的数据,可以考虑通过Range分区,将数据划分到月或日的区间内;如果例如客户ID号这样的字段数据,可以考虑Hash分区,在真实系统中,ID号这样的自增数字字段,按照Hash值划分在可以达到较好的均匀分布。Roundrobin则是针对整张表记录进行随机划分,将数据按记录条数均匀分配到分区之中。
Range Partitioning 范围分区
按时间分区,字段是主键
ALTER TABLE schema.tablename
PARTITION BY RANGE ("DAY_ID")
(PARTITION '20170101' <= VALUES < '20180101',
PARTITION '20180101' <= VALUES < '20190101',
PARTITION '20190101' <= VALUES < '20200101',
PARTITION '20200101' <= VALUES < '20210101',
PARTITION '20210101' <= VALUES < '20220101',
PARTITION '20220101' <= VALUES < '20230101',
PARTITION OTHERS)
--修改-删除分区:
ALTER TABLE mytab ADD PARTITION OTHERS
ALTER TABLE mytab DROP PARTITION OTHERS
查看表分区情况:
select * from sys.M_CS_PARTITIONS where table_name = ''
哈希分区(HASH partitioning):
CREATE COLUMN TABLE TEST_HASH_PARTITION_2 (a INT, b INT, c INT, PRIMARY KEY (a,b)) PARTITION BY HASH (a, b) PARTITIONS 4;
select * from sys.m_cs_partitions where table_name='TEST_HASH_PARTITION_2';
分区数是由数据库引擎在运行时根据其配置来决定:
CREATE COLUMN TABLE TEST_HASH_PARTITION (A INT, B INT, C INT, PRIMARY KEY (A,B)) PARTITION BY HASH (A, B)
PARTITIONS GET_NUM_SERVERS() ;
select * from sys.m_cs_partitions where table_name='TEST_HASH_PARTITION';
alter table "MES"."ZBCT001" PARTITION BY HASH (ZHM)
PARTITIONS GET_NUM_SERVERS() ;
用这个 参数 让HASH自动决定分区的数量
循环分区(Round-robin partitioning):
CREATE COLUMN TABLE mytab (a INT, b INT, c INT) PARTITION BY ROUNDROBIN PARTITIONS 4;
select * from sys.m_cs_partitions where table_name='mytab';