【博客文章2021】Oracle分区技术和复合分区技术的例子和注意事项
Author: Bo Tang
SYSTEM分区:
- CREATE TABLE hr.systab (c1 integer, c2 integer)
- PARTITION BY SYSTEM
- (
- PARTITION p1 TABLESPACE users,
- PARTITION p2 TABLESPACE example,
- PARTITION p3 TABLESPACE users,
- PARTITION p4 TABLESPACE example
- );
- select * from dba_part_tables pt
- where pt.owner='HR' and pt.table_name='SYSTAB';
- select * from dba_tab_partitions tp
- where tp.table_owner='HR' and tp.table_name='SYSTAB';
-
- INSERT INTO hr.systab VALUES (4,5);会报错
复制代码
- INSERT INTO hr.systab PARTITION (p1) VALUES (4,5);
- commit;
-
- select * from hr.systab partition ( p1);
-
复制代码 ----------------虚拟列表:
- create table hr.t5006_a ( a number ,
- b as (a*2 ) );
- select * from dba_tab_columns tc
- where tc.OWNER='HR' and tc.TABLE_NAME='T5006_A';
- insert into hr.T5006_A (a) values (1 );
- commit;
- begin
- for i in 2..300
- loop
- insert into hr.T5006_A (a) values (i );
- end loop;
- commit;
- end;
- begin
- dbms_stats.gather_table_stats(ownname => 'HR',
- tabname => 'T5006_A',
- method_opt => 'for columns b size 254 for all columns size auto');
- end;
-
- select * from dba_tab_col_statistics tcs
- where tcs.owner='HR' and tcs.table_name='T5006_A';
-
- select * from hr.t5006_a;
-
- update hr.t5006_a set b=4 where a=1;会报错
复制代码
- create table hr.t5006_b ( a varchar2(10) ,
- b as (a||'X' ) )
- partition by list (b)
- (
- partition p1 values ('AX','BX'),
- partition p2 values ('CX')
- );
-
- insert into hr.t5006_b (a ) values ('A');
- commit;
- select * from hr.t5006_b partition (p1);
复制代码 语法上可以不写virtual:
- create table hr.t5006_c ( a number ,
- b as (a*2 ) virtual );
复制代码 --------------------------- 引用分区,要特别注意外键非空:
- create table hr.t5006_parent ( a number primary key , b varchar2(20) )
- partition by range ( a )
- (
- partition p1 values less than(100),
- partition p2 values less than (maxvalue)
- );
- create table hr.t5006_son ( a number constraint fk_t5006 references hr.t5006_parent ,
- c date )
- partition by reference (fk_t5006); 会报错:
复制代码
- create table hr.t5006_son ( a number not null constraint fk_t5006 references hr.t5006_parent ,
- c date )
- partition by reference (fk_t5006);
复制代码 ------------------------引用分区的自动维护:
- create table hr.t5006_parent ( a number primary key , b varchar2(20) )
- partition by range ( a )
- (
- partition p1 values less than(100),
- partition p2 values less than (maxvalue)
- );
- create table hr.t5006_son ( a number not null constraint fk_t5006 references hr.t5006_parent ,
- c date )
- partition by reference (fk_t5006);
- ----------------------------------------
- select table_name, partition_name, high_value
- from dba_tab_partitions
- where table_name in ('T5006_PARENT','T5006_SON')
- order by partition_position, table_name;
- ----------------------------------------
- insert into hr.t5006_parent values ( 99,'A99' ) ;
- commit;
- insert into hr.t5006_son values (99,sysdate );
- commit;
- select * from hr.t5006_son partition (p2) ;
- -----------------------------------------
- alter table hr.t5006_parent split partition p1 at ( 50) into
- ( partition p11 , partition p12);
- ------------------------------------------
- select table_name, partition_name, high_value
- from dba_tab_partitions
- where table_name in ('T5006_PARENT','T5006_SON')
- order by partition_position, table_name;
- -----------------------------------------------------------------------
- alter table hr.t5006_parent merge partitions p11, p12
- into partition p1;
- -------------------------------------------------
- select table_name, partition_name, high_value
- from dba_tab_partitions
- where table_name in ('T5006_PARENT','T5006_SON')
- order by partition_position, table_name;
复制代码 ---------------------分区键并不是主外键(书上的例子):
- create table hr.t5006_p ( a number primary key , b date )
- partition by range ( b )
- (
- partition p1 values less than( to_date ('2000-01-01','YYYY-MM-DD' )),
- partition p2 values less than ( to_date ('2010-01-01','YYYY-MM-DD') )
- );
- create table hr.t5006_s ( a number not null constraint fk_t5006_s references hr.t5006_p ,
- b date )
- partition by reference (fk_t5006_s);
- insert into hr.t5006_p values ( 1, to_date ('2009-01-01','YYYY-MM-DD') );
- commit;
- insert into hr.t5006_s values ( 1, to_date ('1999-01-01','YYYY-MM-DD') );
- commit;
- select table_name, partition_name, high_value
- from dba_tab_partitions
- where table_name in ('T5006_P','T5006_S')
- order by partition_position, table_name;
- select * from hr.t5006_p partition (p2) ;
- select * from hr.t5006_s partition (p2);
- select * from hr.t5006_s;
- select * from hr.t5006_s partition (p2);
复制代码
以上这种情况,son表的分区范围(date)与 parent表是不一样的,son表的分区分布: 主键(1)在p2,子键(1)就在子表自己的p2上。 --------------------------------------复合分区 有办法写temple的语法:
- CREATE TABLE hr.sales ( prod_id NUMBER(6) NOT NULL,
- cust_id NUMBER NOT NULL,
- time_id DATE NOT NULL,
- channel_id char(1) NOT NULL,
- promo_id NUMBER (6) NOT NULL,
- quantity_sold NUMBER(3) NOT NULL,
- amount_sold NUMBER(10,2) NOT NULL )
- PARTITION BY RANGE (time_id)
- SUBPARTITION BY RANGE (cust_id)
- SUBPARTITION TEMPLATE
- ( SUBPARTITION sp1 VALUES LESS THAN (50000),
- SUBPARTITION sp2 VALUES LESS THAN (100000),
- SUBPARTITION sp3 VALUES LESS THAN (150000),
- SUBPARTITION sp4 VALUES LESS THAN (MAXVALUE) )
- (
- PARTITION p1 VALUES LESS THAN (TO_DATE('1-4-2007','DD-MM-YYYY')),
- PARTITION p2 VALUES LESS THAN (TO_DATE('1-7-2007','DD-MM-YYYY')),
- PARTITION p3 VALUES LESS THAN (TO_DATE('1-8-2007','DD-MM-YYYY')),
- PARTITION p4 VALUES LESS THAN (TO_DATE('1-1-2008','DD-MM-YYYY'))
- );
- select * from dba_tab_partitions tp
- where tp.table_owner='HR' and tp.table_name='SALES';
- select * from dba_tab_subpartitions tp
- where tp.table_owner='HR' and tp.table_name='SALES';
复制代码 没办法写template的语法:
- create table shipments
- ( order_id number not null
- , order_date date not null
- , ship_date date not null
- , customer_id number not null
- , sales_amount number not null
- )
- partition by range (order_date)
- subpartition by range (ship_date)
- ( partition p_2006_jul values
- less than (to_date('01-AUG-2006','dd-MON-yyyy'))
- ( subpartition p_2006_jul_early values
- less than (to_date('15-AUG-2006','dd-MON-yyyy'))
- , subpartition p_2006_jul_agreed values
- less than (to_date('01-SEP-2006','dd-MON-yyyy'))
- , subpartition p_2006_jul_late values less than (maxvalue)
- )
- , partition p_2006_aug values
- less than (to_date('01-SEP-2006','dd-MON-yyyy'))
- ( subpartition p_2006_aug_early values
- less than (to_date('15-SEP-2006','dd-MON-yyyy'))
- , subpartition p_2006_aug_agreed values
- less than (to_date('01-OCT-2006','dd-MON-yyyy'))
- , subpartition p_2006_aug_late values less than (maxvalue)
- )
- , partition p_2006_sep values
- less than (to_date('01-OCT-2006','dd-MON-yyyy'))
- ( subpartition p_2006_sep_early values
- less than (to_date('15-OCT-2006','dd-MON-yyyy'))
- , subpartition p_2006_sep_agreed values
- less than (to_date('01-NOV-2006','dd-MON-yyyy'))
- , subpartition p_2006_sep_late values less than (maxvalue)
- )
- , partition p_2006_oct values
- less than (to_date('01-NOV-2006','dd-MON-yyyy'))
- ( subpartition p_2006_oct_early values
- less than (to_date('15-NOV-2006','dd-MON-yyyy'))
- , subpartition p_2006_oct_agreed values
- less than (to_date('01-DEC-2006','dd-MON-yyyy'))
- , subpartition p_2006_oct_late values less than (maxvalue)
- )
- , partition p_2006_nov values
- less than (to_date('01-DEC-2006','dd-MON-yyyy'))
- ( subpartition p_2006_nov_early values
- less than (to_date('15-DEC-2006','dd-MON-yyyy'))
- , subpartition p_2006_nov_agreed values
- less than (to_date('01-JAN-2007','dd-MON-yyyy'))
- , subpartition p_2006_nov_late values less than (maxvalue)
- )
- , partition p_2006_dec values
- less than (to_date('01-JAN-2007','dd-MON-yyyy'))
- ( subpartition p_2006_dec_early values
- less than (to_date('15-JAN-2007','dd-MON-yyyy'))
- , subpartition p_2006_dec_agreed values
- less than (to_date('01-FEB-2007','dd-MON-yyyy'))
- , subpartition p_2006_dec_late values less than (maxvalue)
- )
- )
复制代码 |
|