联 系 我 们
售前咨询
售后咨询
微信关注:星环科技服务号
更多联系方式 >
4.4.5 分区表
更新时间:6/28/2023, 8:00:03 AM
分区

在数据仓库“T+1”场景中,通常只需要批量处理当天的数据。因此设置时间作为分区字段,能够大大减少磁盘读写开销。我们一般从数据的管理角度来选择分区键,选用时间或者区域作为分区键。

分区裁剪

  • 如果查询条件使用分区字段作为过滤条件,那么通过分区裁剪直接过滤不必要的分区,大大减少数据扫描范围。

  • 在扫描分区数据时,相同分区数据存储在相同的物理位置,因此可以做到分区数据的顺序读,从而大大减少磁盘读写开销。

多级分区

ArgoDB支持多级分区。当数据查询范围通常为多个字段时,也可以使用多级分区使得分区裁剪更为有效。例如设置月份-日期二级分区。

创建分区表
例 9. 创建一个按 trans_time 分区的单值分区表
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]:分区列是一个虚拟列,因此分区键不能存在于表的字段中。分区以文件目录的形式存在。

例 10. 创建一个按 trans_time 分区的范围分区表
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" ;
复制
增加分区
例 11. 添加单值分区 trans_time = "2022-02-23"
alter table orders_partition add partition (trans_time = "2022-02-23");
复制
例 12. 添加范围分区 before2014_04
alter table orders_range_partition add partition before2014_04 values less than ("2014-04-30");
复制

ArgoDB 支持动态分区创建及插入功能,具体请参考 《Transwarp ArgoDB 开发者指南》的 SQL 参考章节。

查看分区
例 13. 查看表 orders_partition 中的分区信息:
show partitions orders_partition;
复制
删除分区
例 14. 删除单值分区 trans_time = "2022-02-23"
alter table  orders_partition drop partition (trans_time = "2022-02-23");
复制
例 15. 删除范围分区 before2014_04
alter table  orders_range_partition drop partition before2014_04;
复制

删除分区数据可以直接通过物理方式删除数据,速度很快。