HPE Vertica:DROP_PARTITION 动态谓词值

HPE Vertica : DROP_PARTITION dynamic predicate value

来自 Vertica 文档:
DROP_PARTITION ( table_name , partition_value [ , ignore_moveout_errors, reorganize_data ])

partition_value 谓词可以通过任何方法动态化吗?

我想从另一个分段 table.
中删除基于 MAX(partition_col_val) 条件的分区 Vertica 也不支持这样的变量创建,我可以在其中保留 MAX(partition_col_val)。

有什么解决办法吗?

您不能使用子查询为您生成动态谓词值drop_partition。

通常我会用一个脚本来处理缺少动态特性的问题,该脚本会将 drop_partition sql 表达式生成到 .sql 文件中,然后我在下一步中执行。

看这里的例子:

create table tblone (id int not null) partition by id;

insert into tblone values (1);
commit;

-- 将查询的输出刷新到文件中

\o /tmp/file.sql
select 
'SELECT DROP_PARTITION(''public.tblone'','||
max(id)||
');' from tblone;

--执行文件内容

\i /tmp/file.sql
--this is the content.
SELECT DROP_PARTITION('public.tblone',1);

这是当您有基于非日期和需要从其他数据集派生的数据的分区时。

如果您将日期作为分区键或数据列的派生值,您可以使用内部函数动态填充 drop_partition 键值:

    drop table tblone cascade;
create table tblone (id date not null) partition by 
 (((date_part('year', id) * 100) + date_part('month', id)));

insert into tblone values (getdate());
commit;

dbadmin=> select * from tblone;
     id
------------
 2017-01-04
(1 row)


dbadmin=> SELECT DROP_PARTITION('tblone',(date_part('year', getdate()) * 100) + date_part('month', getdate()));
  DROP_PARTITION
-------------------
 Partition dropped
(1 row)

dbadmin=> select * from tblone;
 id
----
(0 rows)

-您可以随时使用 getdate() 来获取当前或上个月或任何您想要的时间段。

另一种选择是使用 vsql cmd 行变量 例子

dbadmin=> drop table tblone cascade;
DROP TABLE
dbadmin=> create table tblone (id int not null) partition by id;
CREATE TABLE
dbadmin=> insert into tblone values (1);
      1

dbadmin=> commit;
COMMIT
dbadmin=> select * from tblone;
 id
----
  1
(1 row)

-- 只显示元组

dbadmin=> \t
Showing only tuples.

--将最大值吐到文件

dbadmin=> \o /tmp/file
dbadmin=> select max(id) from tblone;
dbadmin=> \o
dbadmin=> \t
Tuples only is off.

--设置变量的值为文件内容(你的最大值)

dbadmin=> \set maxvalue `cat /tmp/file`
dbadmin=> \echo :maxvalue
       1

--运行drop分区使用变量

dbadmin=> SELECT DROP_PARTITION('tblone',:maxvalue);
  DROP_PARTITION
-------------------
 Partition dropped
(1 row)

dbadmin=> select * from tblone;
 id
----
(0 rows)
  • 希望对您有所帮助 :)

使用单行代码从 table 删除许多分区的简单方法是使用 MOVE_PARTITIONS_TO_TABLE 到虚拟 table 然后放下假人 table - 这将不需要锁定主 table 并且删除虚拟 table 对数据库来说是一项廉价的任务(将是大量的 drop_partition)。

  1. 从基础 table(包括投影)创建虚拟 table。
  2. 生成动态 MOVE_PARTITIONS_TO_TABLE('source','from 分区','to partition','target/dummy table').
  3. 放置假人table。

看小例子(不是100个完整的-你可以适应它) 是相同的方法(生成&执行)

    \o /tmp/file.sql
select 'create dummy table as source table including projections;';
select 
    'SELECT MOVE_PARTITIONS_TO_TABLE(''source'','''||
    :minpartition()||
    ''','''||
    :maxpartition()||
    ''',''target/dummy table'')'
      from tblone;
select 'drop table dummy cascade';

-- execute the content of the file

    \i /tmp/file.sql
--make sure the content of the file is correct content

**

BTW - if you look for Vertica Database Articles and Scripts from time to time i post at http://wwww.aodba.com

**