Create table orders_partition( trans_id int, acc_num int, trans_type string, stock_id string, price decimal, amount int) partitioned by(trans_time date) --[1] stored as holodesk;
复制
-
[1]:分区列是一个虚拟列,因此分区键不能存在于表的字段中。分区以文件目录的形式存在。
在数据仓库“T+1”场景中,通常只需要批量处理当天的数据。因此设置时间作为分区字段,能够大大减少磁盘读写开销。我们一般从数据的管理角度来选择分区键,选用时间或者区域作为分区键。
分区裁剪
如果查询条件使用分区字段作为过滤条件,那么通过分区裁剪直接过滤不必要的分区,大大减少数据扫描范围。
在扫描分区数据时,相同分区数据存储在相同的物理位置,因此可以做到分区数据的顺序读,从而大大减少磁盘读写开销。
多级分区
ArgoDB支持多级分区。当数据查询范围通常为多个字段时,也可以使用多级分区使得分区裁剪更为有效。例如设置月份-日期二级分区。
Create table orders_partition( trans_id int, acc_num int, trans_type string, stock_id string, price decimal, amount int) partitioned by(trans_time date) --[1] stored as holodesk;
复制
[1]:分区列是一个虚拟列,因此分区键不能存在于表的字段中。分区以文件目录的形式存在。
CREATE TABLE orders_range_partition( trans_id int, acc_num int, trans_type string, stock_id string, price decimal, amount int) PARTITIONED BY RANGE (trans_time date) ( PARTITION before2014_02 VALUES LESS THAN ('2014-02-28'), PARTITION before2014_03 VALUES LESS THAN ('2014-03-31') --[1] ) stored as holodesk;
复制
[1]:范围分区只能添加新的分区,不能分裂原有分区。因此当最后一个分区是 maxvalue 结尾时,它将不能再增加分区。
select trans_id , acc_num ,trans_type ,stock_id , price ,amount ,trans_time from orders_partition where trans_time = "2022-02-22" ;
复制
alter table orders_partition add partition (trans_time = "2022-02-23");
复制
alter table orders_range_partition add partition before2014_04 values less than ("2014-04-30");
复制
ArgoDB 支持动态分区创建及插入功能,具体请参考 《Transwarp ArgoDB 开发者指南》的 SQL 参考章节。 |
show partitions orders_partition;
复制
alter table orders_partition drop partition (trans_time = "2022-02-23");
复制
alter table orders_range_partition drop partition before2014_04;
复制
删除分区数据可以直接通过物理方式删除数据,速度很快。 |