代码改变世界

Vertica 分区表设计

2015-04-23 23:05  AlfredZhao  阅读(7144)  评论(3编辑  收藏  举报

Vertica数据库中的表只是一个逻辑概念。

实际存储在磁盘上的是projection。 当创建一张表,没有创建projection时,那么插入数据的时候会自动创建一个默认的projection。如果运行中发现projection不合适,可以运行dbd进行优化,得出一些建议,参考来重建projection。

如果清楚projection如何建立最优,那么建表的时候就可以在插入数据之前直接手动建立对应的projection。

Projection

Optimized collections of table columns that provide physical storage for data. A projection can contain some or all of the columns of one or more tables. A projection that contains all of the columns of a table is called a super-projection. A projection that joins one or more tables is called a pre-join projection.

导出Vertica库中原有的建表语句:

例如导出test用户下的t_jingyu表的建表语句到/tmp/t_jingyu.sql文件(需要dbadmin用户登录vsql操作):
select export_objects('/tmp/t_jingyu.sql','test.t_jingyu');

vertica建分区表:

按doy分区:

create table t_jingyu(
col1 int, 
col2 varchar, 
col3 timestamp not null)
PARTITION BY (date_part('doy', t_jingyu.col3));

这样的分区表卸载时:

SELECT DROP_PARTITION('test.t_jingyu', EXTRACT('doy' FROM '2015-04-01'::date)); 

按月分区:

create table t_jingyu(
col1 int, 
col2 varchar, 
col3 timestamp not null)
partition by EXTRACT(year FROM col3)*100 + EXTRACT(month FROM col3);

插入测试数据:

insert into t_jingyu values(1,11,sysdate-1);
insert into t_jingyu values(1,11,sysdate);
insert into t_jingyu values(2,11,sysdate-33);
commit;

这样的分区表卸载时:

SELECT DROP_PARTITION('test.t_jingyu', EXTRACT('year' FROM '20150401'::date)*100 +  EXTRACT('month' FROM '20150401'::date)); 

上面就是删除201504的分区。

创建Projection:

CREATE PROJECTION t_jingyu
(
col1, 
col2, 
col3
)
AS
SELECT * FROM t_jingyu
ORDER BY col3, col1
SEGMENTED BY hash(col3) ALL NODES KSAFE 1;