oracle 組合分區

  1. 範圍-哈希分區(手工添加)
drop table test_range_hast_tab;

CREATE TABLE test_range_hast_tab
(
    tab_seq  number not null,
    tab_type varchar2(1),   
    tab_date date           
) tablespace MPBUS_1901
partition by range(tab_date)
subpartition by hash(tab_type)
SUBPARTITION TEMPLATE
(
   SUBPARTITION PA TABLESPACE MPBUS_1901_A,
   SUBPARTITION PB TABLESPACE MPBUS_1901_B
)
(
   PARTITION P1901 VALUES LESS THAN(TO_DATE('2019-02-01', 'YYYY-MM-DD')) --tablespace MPBUS_1901
);

alter table test_range_hast_tab
add constraint pk_test_range_hast_tab primary key (tab_date,tab_type,tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_MPBUS_1901
 (
    SUBPARTITION P1901_PA TABLESPACE I_MPBUS_1901_A, 
    SUBPARTITION P1901_PB TABLESPACE I_MPBUS_1901_B     
 )
);
-----------------------------------------------------------------------------------------------------------------
ALTER TABLE test_range_hast_tab SET SUBPARTITION TEMPLATE
( 
  SUBPARTITION PA TABLESPACE MPBUS_1902_A,
  SUBPARTITION PB TABLESPACE MPBUS_1902_B
);

#修改分区表的默认表空间:
alter table test_range_hast_tab  modify default attributes tablespace MPBUS_1902;

#修改该表上某个索引的默认表空间:
alter index  pk_test_range_hast_tab modify default attributes tablespace I_MPBUS_1902;

alter table test_range_hast_tab add partition P1902 values less than (TO_DATE('2019-03-01', 'YYYY-MM-DD'));

select b.table_name,b.partition_name,b.tablespace_name,b.partition_position from USER_TAB_PARTITIONS b
where b.table_name=upper('test_range_hast_tab')
ORDER BY 4;
1   TEST_RANGE_HAST_TAB P1901   MPBUS_1901  1
2   TEST_RANGE_HAST_TAB P1902   MPBUS_1902  2

select c.table_name,c.partition_name,c.subpartition_name,c.partition_position,c.subpartition_position,c.tablespace_name 
from USER_TAB_SUBPARTITIONS c 
where c.table_name=upper('test_range_hast_tab')
ORDER BY 4,5;
1   TEST_RANGE_HAST_TAB P1901   P1901_PA    1   1   MPBUS_1901_A
2   TEST_RANGE_HAST_TAB P1901   P1901_PB    1   2   MPBUS_1901_B
3   TEST_RANGE_HAST_TAB P1902   P1902_PA    2   1   MPBUS_1902_A
4   TEST_RANGE_HAST_TAB P1902   P1902_PB    2   2   MPBUS_1902_B


SELECT TABLE_NAME, A.INDEX_NAME, A.TABLESPACE_NAME,a.partition_name ,a.partition_position
FROM USER_IND_PARTITIONS A, USER_INDEXES B
WHERE A.INDEX_NAME = B.INDEX_NAME
and A.INDEX_NAME=upper('pk_test_range_hast_tab')
ORDER BY 4;
1   TEST_RANGE_HAST_TAB PK_TEST_RANGE_HAST_TAB  I_MPBUS_1901    P1901   1
2   TEST_RANGE_HAST_TAB PK_TEST_RANGE_HAST_TAB  I_MPBUS_1902    P1902   2


select d.index_name,d.partition_name,d.subpartition_name,d.tablespace_name
,d.partition_position,d.subpartition_position
from user_ind_subpartitions d where d.index_name=upper('pk_test_range_hast_tab')
order by 5,6
1   PK_TEST_RANGE_HAST_TAB  P1901   P1901_PA    I_MPBUS_1901_A  1   1
2   PK_TEST_RANGE_HAST_TAB  P1901   P1901_PB    I_MPBUS_1901_B  1   2
3   PK_TEST_RANGE_HAST_TAB  P1902   P1902_PA    I_MPBUS_1902    2   1
4   PK_TEST_RANGE_HAST_TAB  P1902   P1902_PB    I_MPBUS_1902    2   2

alter index pk_test_range_hast_tab rebuild subPARTITION P1902_PA  TABLESPACE I_MPBUS_1902_A;
alter index pk_test_range_hast_tab rebuild subPARTITION P1902_PB  TABLESPACE I_MPBUS_1902_B;

select d.index_name,d.partition_name,d.subpartition_name,d.tablespace_name
,d.partition_position,d.subpartition_position,d.status
from user_ind_subpartitions d where d.index_name=upper('pk_test_range_hast_tab')
order by 5,6
1   PK_TEST_RANGE_HAST_TAB  P1901   P1901_PA    I_MPBUS_1901_A  1   1   USABLE
2   PK_TEST_RANGE_HAST_TAB  P1901   P1901_PB    I_MPBUS_1901_B  1   2   USABLE
3   PK_TEST_RANGE_HAST_TAB  P1902   P1902_PA    I_MPBUS_1902_A  2   1   USABLE
4   PK_TEST_RANGE_HAST_TAB  P1902   P1902_PB    I_MPBUS_1902_B  2   2   USABLE

  1. 範圍-哈希分區(自動添加)
--------------------------------------------------------------------------------------------------
drop table test_range_hast_tab;

CREATE TABLE test_range_hast_tab
(
    tab_seq  number not null,
    tab_type varchar2(1),   
    tab_date date           
) tablespace MPBUS_1901
partition by range(tab_date) interval(numtoyminterval(1, 'month'))
subpartition by hash(tab_type)
SUBPARTITION TEMPLATE
(
   SUBPARTITION PA TABLESPACE MPBUS_1901_A,
   SUBPARTITION PB TABLESPACE MPBUS_1901_B
)
(
   PARTITION P1901 VALUES LESS THAN(TO_DATE('2019-02-01', 'YYYY-MM-DD')) 
);

alter table test_range_hast_tab
add constraint pk_test_range_hast_tab primary key (tab_date,tab_type,tab_seq)
using index local
(
 PARTITION P1901 TABLESPACE I_MPBUS_1901
 (
    SUBPARTITION P1901_PA TABLESPACE I_MPBUS_1901_A, 
    SUBPARTITION P1901_PB TABLESPACE I_MPBUS_1901_B     
 )
);

ALTER TABLE test_range_hast_tab SET SUBPARTITION TEMPLATE
( 
  SUBPARTITION PA TABLESPACE MPBUS_1902_A,
  SUBPARTITION PB TABLESPACE MPBUS_1902_B
);

#修改分区表的默认表空间:
alter table test_range_hast_tab  modify default attributes tablespace MPBUS_1902;

#修改该表上某个索引的默认表空间:
alter index  pk_test_range_hast_tab modify default attributes tablespace I_MPBUS_1902;

insert into test_range_hast_tab(tab_seq,tab_type,tab_date)
values(1,'a',date'2019-01-01');
insert into test_range_hast_tab(tab_seq,tab_type,tab_date)
values(2,'b',date'2019-02-01');
commit;


select b.table_name,b.partition_name,b.tablespace_name,b.partition_position from USER_TAB_PARTITIONS b
where b.table_name=upper('test_range_hast_tab')
ORDER BY 4;
1   TEST_RANGE_HAST_TAB P1901   MPBUS_1901  1
2   TEST_RANGE_HAST_TAB SYS_P2475   MPBUS_1902  2


select c.table_name,c.partition_name,c.subpartition_name,c.partition_position,c.subpartition_position,c.tablespace_name 
from USER_TAB_SUBPARTITIONS c 
where c.table_name=upper('test_range_hast_tab')
ORDER BY 4,5;
1 TEST_RANGE_HAST_TAB P1901 P1901_PA  1 1 MPBUS_1901_A
2 TEST_RANGE_HAST_TAB P1901 P1901_PB  1 2 MPBUS_1901_B
3 TEST_RANGE_HAST_TAB SYS_P2475 SYS_SUBP2473  2 1 MPBUS_1902_A
4 TEST_RANGE_HAST_TAB SYS_P2475 SYS_SUBP2474  2 2 MPBUS_1902_B


SELECT TABLE_NAME, A.INDEX_NAME, A.TABLESPACE_NAME,a.partition_name ,a.partition_position
FROM USER_IND_PARTITIONS A, USER_INDEXES B
WHERE A.INDEX_NAME = B.INDEX_NAME
and A.INDEX_NAME=upper('pk_test_range_hast_tab')
ORDER BY 4;
1   TEST_RANGE_HAST_TAB PK_TEST_RANGE_HAST_TAB  I_MPBUS_1901    P1901   1
2   TEST_RANGE_HAST_TAB PK_TEST_RANGE_HAST_TAB  I_MPBUS_1902    SYS_P2475   2


select d.index_name,d.partition_name,d.subpartition_name,d.tablespace_name
,d.partition_position,d.subpartition_position
from user_ind_subpartitions d where d.index_name=upper('pk_test_range_hast_tab')
order by 5,6
1 PK_TEST_RANGE_HAST_TAB  P1901 P1901_PA  I_MPBUS_1901_A  1 1
2 PK_TEST_RANGE_HAST_TAB  P1901 P1901_PB  I_MPBUS_1901_B  1 2
3 PK_TEST_RANGE_HAST_TAB  SYS_P2475 SYS_SUBP2473  I_MPBUS_1902  2 1
4 PK_TEST_RANGE_HAST_TAB  SYS_P2475 SYS_SUBP2474  I_MPBUS_1902  2 2


alter index pk_test_range_hast_tab rebuild subPARTITION SYS_SUBP2473  TABLESPACE I_MPBUS_1902_A;
alter index pk_test_range_hast_tab rebuild subPARTITION SYS_SUBP2474  TABLESPACE I_MPBUS_1902_B;

select d.index_name,d.partition_name,d.subpartition_name,d.tablespace_name
,d.partition_position,d.subpartition_position,d.status
from user_ind_subpartitions d where d.index_name=upper('pk_test_range_hast_tab')
order by 5,6;

1 PK_TEST_RANGE_HAST_TAB  P1901 P1901_PA  I_MPBUS_1901_A  1 1 USABLE
2 PK_TEST_RANGE_HAST_TAB  P1901 P1901_PB  I_MPBUS_1901_B  1 2 USABLE
3 PK_TEST_RANGE_HAST_TAB  SYS_P2475 SYS_SUBP2473  I_MPBUS_1902_A  2 1 USABLE
4 PK_TEST_RANGE_HAST_TAB  SYS_P2475 SYS_SUBP2474  I_MPBUS_1902_B  2 2 USABLE
--------------------------------------------------------------------------------------------------
--select dbms_metadata.get_ddl('TABLE',upper('test_range_hast_tab'),'BUS') from dual;

你可能感兴趣的:(oracle 組合分區)