MaxCompute表的小文件会影响存储和计算性能,所以咱们先介绍下什么样的操做会产生大量小文件,从 而在作表设计的时候考虑避开此类操做。数据库
项目空间(Project)是MaxCompute最高层的对象,按项目空间进行资源的分配、隔离和管理,实现了 多租户的管理能力。session
通常状况下描述属性的表设计为维度表。维度表能够和任意表组的任意表进行关联,而且建立时不须要配 置分区信息,可是对单表数据量大小有所限制。维度表的设计和使用注意如下几点:工具
极限存储功能待发布,在此介绍主要提供设计思想。 基于MaxCompute的拉链表设计背景 在数据仓库的数据模型设计过程当中,常常会遇到这样的需求:性能
create table src_tbl (key0 STRING, key1 STRING, col0 STRING, col1 STRING, col2 STRING) PARTITIO N (datestam p_x STRING, pt0 STRING);
set odps.exstore.primarykey=key0,key1; [set odps.exstore.ignorekey=col0;] EXSTO RE exstore_tbl PARTITIO N (datestam p_x='20140801'); EXSTO RE exstore_tbl PARTITIO N (datestam p_x='20140802');
拉链表设计更详细介绍能够参考云栖文章:https://yq.aliyun.com/articles/542146#? spm=a2c41.11181499.0.0开发工具
数据采集方式:流式数据写入, 批量数据写入,周期调度条式数据插入。
大数据量状况下,确保同一个业务单元的数据使用分区和表进行分;在数据量较小状况下,优化采集频率。大数据
流式数据写入。优化
日志实际上是个流水表,不涉及记录的更新,来一条采集一条,多条一块儿存放,日志表设计的主要注意几 点:阿里云
create table src_tbl (key0 STRING, key1 STRING, col0 STRING, col1 STRING, col2 STRING) PARTITIO N (datestam p_x STRING, pt0 STRING); set odps.exstore.primarykey=key0,key1; [set odps.exstore.ignorekey=col0;] EXSTO RE exstore_tbl PARTITIO N (datestam p_x='20140801'); EXSTO RE exstore_tbl PARTITIO N (datestam p_x='20140802');
考虑是否须要扩展维度属性。spa
考虑区分终端类型。命令行
注意:
周期快照表,天天对收藏的全部记录进行快照存放。
问题:历史累计的记录很是多,天天生成快照要拿当天增量表与前一天的全量表merge,很是耗资源。统 计最近1天的新增收藏数,须要扫描全量表,如何下降资源?
建议的方案:创建一个事务性事实表,在创建一个存放当前有效收藏的周期快照表,以知足各类不一样业务 的统计分析须要。
注意:
关系型数据库支持的 delete/update/merge SQL ,在MaxCompute上的实现方式示例以下:
表准备
-- 上日全量表 table1(key1 string,key2 string,col1 string,col2 string); -- 今日增量表 table2(key1 string,key2 string,col1 string,col2 string); -- 今日增量表(删除) table3(key1 string,key2 string,col1 string,col2 string);
update(table2 表中的记录的值,更新到table1表中)
insert overwrite table table1 select t1.key1 ,t1.key2 ,case when t2.key1 is not null then t2.col1 else t1.col1 end as col1 ,case when t2.key1 is not null then t2.col2 else t1.col2 end as col2 from table1 t1 left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2 ;
delete(table2 表中的记录,从table1表中删除)
insert overwrite table table1 select t1.key1 ,t1.key2 ,t1.col1 ,t1.col2 from table1 t1 left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2 where t2.key1 is null ;
merge(没有del)
insert overwrite table table1 select from ( -- 先把上日存在,今日也存在的记录从上日表中排除。剩下的就是今日没有更新的记录 select t1.key1 ,t1.key2 ,t1.col1 ,t1.col2 from table1 t1 left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2 where t2.key1 is null union all -- 再合并上今日增量,就是今天的全量 select t2.key1 select t2.key1 ,t2.key2 ,t2.col1 ,t2.col2 from table2 t2)tt ;
merge(有del)
insert overwrite table table1 select
from (
-- 先把上日存在,今日也存在的记录从上日表中排除,再把今日删除的记录排除。剩下的就是今日没有更 新的记录
insert overwrite table table1 select from ( -- 先把上日存在,今日也存在的记录从上日表中排除,再把今日删除的记录排除。剩下的就是今日没有更 新的记录 select t1.key1 ,t1.key2 ,t1.col1 ,t1.col2 from table1 t1 left outer join table2 t2 on t1.key1=t2.key1 and t1.key2 = t2.key2 left outer join table3 t3 on t1.key1=t3.key1 and t1.key2 = t3.key2 where t2.key1 is null or t2.key1 is null union all -- 再合并上今日增量,就是今天的全量 select t2.key1 ,t2.key2 ,t2.col1 ,t2.col2 from table2 t2)tt ;
场景:天气状况信息采集。
MaxCompute表/分区提供数据生命周期管理。表(分区)数据从最后一次更新时间算起,在通过指定的 时间后没有变更,则此表(分区)将被MaxCompute自动回收。这个指定的时间就是生命周期,生命周期 设置为表级别。
create table test_lifecycle(key string) lifecycle 100;/alter table test_l ifecycle set lifecycle 50;
MaxCompute会根据每张非分区表或者分区的的LastDataModifiedTime以及lifecycle的设置来判断是 否要回收此非分区表或者分区表中的分区。 MaxCompute SQL提供touch操做用来修改分区的 LastDataModifiedTime。会将分区的LastDataModifiedTime修改成当前时间。修改 LastDataModifiedTime的值,MaxCompute会认为表或分区的数据有变更,生命周期的计算会从新开始。
ALTER TABLE table_nam e TO UCH PARTITIO N(partition_col='partition_col_valu e', ...);
注意:
表设计:
Tunnel数据采集过程当中产生的小文件建议:
使用临时表建议建立时都加上生命周期,到期后垃圾回收自动回收。 - 申请过多的datahub shard将会产生小文件问题,申请datahub shard数目的策略 :
Hash Clustering表的优点:优化Bucket Pruning/优化Aggregation/优化存储。 在建立表时使用CLUSTERED BY指定Hash Key,MaxCompute将对指定列进行Hash运算,按照Hash 值分散到各个Bucket里面。
Hash Key指选择原则:
ALTER TABLE table_nam e [CLUSTERED BY (col_nam e [, col_nam e, ...]) [SO RTED B Y (col_nam e [ASC | DESC] [, col_nam e [ASC | DESC] ...])] INTO num ber_of_buck ets BUCKETS]
ALTER TABLE语句适用于存量表,在增长了新的汇集属性以后,新的分区将作hash cluster存储。 建立 完HashClustering的表以后使用insert overwrite从另一个源表进行转化。
注意,Hash Clustering表有如下限制: