对现有表数据进行分区

使用 ALTER TABLE 对现有表进行分区或重新分区,由 PARTITION BY 子句指定:

ALTER TABLE table-name PARTITION BY partition‑expression [ GROUP BY group‑expression ] [ REORGANIZE ];

例如,可以对前面定义store_orders 表进行重新分区。下面的 ALTER TABLE 将每年的所有 store_orders 数据划分为每月分区,每个分区键标识订单日期的年份和月份:

=> ALTER TABLE store_orders
     PARTITION BY EXTRACT(YEAR FROM order_date)*100 + EXTRACT(MONTH FROM order_date)
         GROUP BY EXTRACT(YEAR from order_date)*100 + EXTRACT(MONTH FROM order_date);
NOTICE 8364:  The new partitioning scheme will produce partitions in 42 physical storage containers per projection
WARNING 6100:  Using PARTITION expression that returns a Numeric value
HINT:  This PARTITION expression may cause too many data partitions.  Use of an expression that returns a more accurate value, such as a regular VARCHAR or INT, is encouraged
WARNING 4493:  Queries using table "store_orders" may not perform optimally since the data may not be repartitioned in accordance with the new partition expression
HINT:  Use "ALTER TABLE public.store_orders REORGANIZE;" to repartition the data

执行此语句后,Vertica 会删除现有的分区键。但是,partition 子句会省略 REORGANIZE,以便现有数据仍然根据前一个 partition 子句进行存储。这可能会使表分区处于不一致状态,并对查询性能、DROP_PARTITIONS 和节点恢复产生负面影响。在这种情况下,必须通过以下方式之一显式请求 Vertica 将现有数据重新组织到新分区中:

  • 发出 ALTER TABLE...REORGANIZE:

    ALTER TABLE table-name REORGANIZE;
    
  • 调用 Vertica 元函数 PARTITION_TABLE

例如:

=> ALTER TABLE store_orders REORGANIZE;
NOTICE 4785:  Started background repartition table task
ALTER TABLE

ALTER TABLE...REORGANIZE 和 PARTITION_TABLE 的运行方式相同:两者都会拆分分区键与新 partition 子句不相符的任何 ROS 容器。在执行下一次合并时,Tuple Mover 会将分区合并到相应的 ROS 容器中。