Greenplum - the most complete partition table operation

A, Greenplum partition principle

Meaning that the partition table is divided into a few large table in the physical, GPDB the partition table, PostgreSQL implementation principle, are used table inheritance, constraints to implementation. However, the PostgreSQL also different, in PostgreSQL, a parent table, a plurality of sub-tables to implement the partition table, need to manually insert the data into the child table, if data is inserted into the parent table, then directly be inserted into the parent table, GPDB may be directly inserted in the data table like the parent, can be automatically inserted into the data directly to the corresponding sub-table according to the constraint, when the child partition table does not exist, the insert fails
 

Second, create a partition table

2.1, range partitioning (range)

The range interval partition field to the partition, each partition is a sub-table


    
    
  1. eg:
  2. create table test_partition_range
  3. (
  4. to you ,
  5. name varchar( 64),
  6. fdate varchar( 64)
  7. ) distributed by ( id)
  8. partition by ranks (FDATE)
  9. (
  10. partition p1 start ( '2017-01-01') inclusive end ( '2017-01-31') exclusive,
  11. partition p2 start ( '2017-02-01') inclusive end ( '2017-02-29') exclusive,
  12. default partition default_p
  13. );
  14. inclusive: Specifies comprise, for example, the above Start ( '2017-01-01' ) comprising Inclusive is '2017-01-01'
  15. exclusive: Specifies does not contain, for example, the above End ( '2017-01-31' ) is not included exclusive '2017-01-31'

 

2.2, fast partition (every)

Depending on the selected range, across the base, each sub-partition table fast


    
    
  1. eg:
  2. create table test_partition_every_1
  3. (
  4. to you ,
  5. name varchar( 64),
  6. fdate date
  7. )
  8. distributed by ( id)
  9. partition by ranks (FDATE)
  10. (
  11. partition pn_ start ( '2017-01-01':: date) end ( '2017-12-31':: date) every ( '1 day':: interval),
  12. default partition default_p
  13. );
  14. every: Specifies the spanning base

 

2.3, list partition (list)

The grouping values, data classified into the same group, also a partition


    
    
  1. eg:
  2. create table test_partition_list
  3. (
  4. to you ,
  5. name varchar( 64),
  6. fdate varchar( 10)
  7. )
  8. distributed by ( id)
  9. partition by list (fdate)
  10. (
  11. partition p1 values ( '2017-01-01', '2017-01-02'),
  12. partition p2 values ( '2017-01-03'),
  13. default partition pd
  14. );

 

Third, partition-related operations

3.1, the partition split

Cutting ordinary partitions:


    
    
  1. In the partition p2 '2017-02-20' cut into two right and left
  2. alter table test_partition_range split partition p2 at ( '2017-02-20') into ( partition p2, partition p3);
  3. Cutting default partition:
  4. alter table test_partition_range split default partition start ( '2017-03-01') end ( '2017-03-31') into ( partition p4, default partition);

 

3.2, add partition

If there is default partition, you can not add a partition, only split default partition

alter table test_partition_range_1 add partition p2 start ('2017-02-01') end ('2017-02-31');
    
    

 

3.3 Partition drop

Completely delete the corresponding partition table

alter table test_partition_range_1 DROP partition p2;
    
    

 

3.4, partition truncate

Empty partition table data, equivalent to delete the partition, and then create a

alter table test_partition_range_1 truncate partition p1;
    
    

 

Fourth, the child partition creation and operation

4.1, create a child partition

In GPDB, the partition can be nested increased, a child partition can have the following partition


    
    
  1. create table test_partition_range_2
  2. (
  3. to you ,
  4. name varchar( 64),
  5. fdate varchar( 10)
  6. )
  7. distributed by ( id)
  8. partition by ranks (FDATE)
  9. subpartition by list( name)
  10. subpartition template
  11. (
  12. subpartition c1 values ( 'xiaoxiao'),
  13. subpartition c2 values ( 'xiaohua')
  14. )
  15. (
  16. partition p1 start ( '2017-01-01') end ( '2017-01-31')
  17. )
  18. Above the partition, p1 would be subdivided two c1 / c2 subpartition

 

4.2, truncate child partition

alter table test_partition_range_2 alter partition p1 truncate partition c2;
    
    

 

4.3, drop child partition

alter table test_partition_range_2 alter partition p1 drop partition c2; 
    
    

Creation timestamp partitioned instance

CREATE TABLE fi_middle.order_detail
(
  date_id integer,
  order_id character varying(22),
  product_id character varying(50),
  order_quantity numeric,
  allot_quantity numeric,
  original_price numeric,
  sale_price numeric,
  vip_price numeric,
  bargin_price numeric,
  medium numeric,
  promotion_id numeric,
  is_vip_discount numeric,
  product_type numeric,
  reduce_price numeric,
  etl_change_date timestamp without time zone,
  order_items_id numeric,
  gift_card_charge numeric(12,2),
  gift_unit_price numeric,
  item_id numeric,
  parent_item_id numeric,
  allot_activity_fee numeric(12,2),
  allot_point_deduction_amount numeric,
  send_date timestamp without time zone,
  privilege_code_discount_amount numeric,
  relation_type numeric,
  parent_id character varying(16),
  shop_id numeric,
  shop_type numeric
)
WITH (
  OIDS=FALSE
)
DISTRIBUTED BY (order_id)
PARTITION BY RANGE(send_date)
(
PARTITION p_order_detail_20170701 START ('2017-06-01 00:00:00'::timestamp without time zone) END ('2017-07-01 00:00:00'::timestamp without time zone),
PARTITION p_order_detail_20170801 START ('2017-07-01 00:00:00'::timestamp without time zone) END ('2017-08-01 00:00:00'::timestamp without time zone)
)
 
添加分区
alter table fi.order_detail_adt_cp add partition p_order_detail_adt_20170601 START ('2017-05-01 00:00:00'::timestamp without time zone) END ('2017-06-01 00:00:00'::timestamp without time zone) EVERY ('1 mon'::interval)
修改表名称
alter table fi_middle.order_detail rename to order_detail_adt; (表名默认在旧表模式下,不必指定模式)
内容修改 update fi.fi_promotion_info set supp_no='00'||supp_no where date_id=20170915 and length(supp_no)=5 and supp_no<>'80000'; update fi.fi_promotion_info set supp_no='0'||supp_no where date_id=20170915 and length(supp_no)=4; update fi.fi_promotion_info set end_date=end_date+interval '1 day' where date_id=20170915 and to_char(end_date,'yyyymmdd hh24:mi:ss') like '%00:00:00'; update fi.fi_promotion_info set cat2_name='全品' where date_id=20170915 and cat2_name='全部品种';

Reproduced in: https: //blog.51cto.com/13126942/2053712

Original articles published 0 · won praise 27 · views 80000 +

Guess you like

Origin blog.csdn.net/yimenglin/article/details/102911335