Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

18.07.2019 Document 165924.

1
Copyright (c) 2019, Oracle. All rights reserved. Oracle Confidential.

Example of Script to Create a Composite RANGE-HASH Partition Table (Doc ID 165924.1)


Modified: 04-Aug-2018 Type: SCRIPT

APPLIES TO:

Oracle Server - Enterprise Edition - Version 8.1.5.0 to 11.2.0.3 [Release 8.1.5 to 11.2]
Information in this document applies to any platform.
***Checked for relevance on 24-May-2012***

PURPOSE

The aim of this script will be to help in creating Composite

RANGE-HASH partitioned table.

The script is interactive. It will present the twice possibilities of

creating a RANGE-HASH partitioned table. It will ask you to give the

name of each partitioned table created with both methods. The content

of related dictionary views are then displayed. A pause is set between

each of the different screens, so its necessary to validate it to continue

the execution.

REQUIREMENTS

Execution Environment:

The script must be run under SQLPLUS environment.

CONFIGURING

The user which will execute this script must be granted at least with

CONNECT roles, and SELECT ANY TABLE, UNLIMITED TABLESPACE system

privileges.

INSTRUCTIONS

PROOFREAD THIS SCRIPT BEFORE USING IT! Due to differences in the way text

editors, e-mail packages, and operating systems handle text formatting (spaces,

tabs, and carriage returns), this script may not be in an executable state

https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 1/12


18.07.2019 Document 165924.1

when you first receive it. Check over the script to ensure that errors of

this type are corrected.The script will produce an output file named [outputfile].

The script will ask you to give 8 tablespace names. These tablespaces

will support the subpartition creations, so they should contain sufficient

free space.

You must choose the output file name which will trace the information

displayed. This file can be viewed in a browser or uploaded for support analysis.

CAUTION

This sample code is provided for educational purposes only and not supported by Oracle Support Services. It has been
tested internally, however, and works as documented. We do not guarantee that it will work for you, so be sure to test it
in your environment before relying on it.
Proofread this sample code before using it! Due to the differences in the way text editors, e-mail packages and
operating systems handle text formatting (spaces, tabs and carriage returns), this sample code may not be in
an executable state when you first receive it. Check over the sample code to ensure that errors of this type are
corrected.

SCRIPT

set feedback on

set verify off

set heading off

set pause off

set lin 180

prompt *****************************************************************************

prompt * It's necessary to dispose of eight tablespaces with sufficient free space *

prompt *****************************************************************************

accept TBS1 PROMPT 'tablespace 1: '

accept TBS2 PROMPT 'tablespace 2: '

accept TBS3 PROMPT 'tablespace 3: '

accept TBS4 PROMPT 'tablespace 4: '

accept TBS5 PROMPT 'tablespace 5: '


https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 2/12
18.07.2019 Document 165924.1

accept TBS6 PROMPT 'tablespace 6: '

accept TBS7 PROMPT 'tablespace 7: '

accept TBS8 PROMPT 'tablespace 8: '

accept OUTPUTFILE prompt 'Choose an output file: '

spool &OUTPUTFILE

prompt **************************************************************************

prompt * Two methods may be used for create a COMPOSITE partitioned table. *

prompt * *

prompt * The first one, below, is range partitioned on the columns ord_year, *

prompt * ord_month, and ord_day. Each range partition is further *

prompt * subpartitioned on the PRODUCTID key into eight subpartitions, for a *

prompt * total of 32 subpartitions. Each tablespace contains one subpartition *

prompt * from each partition. *

prompt **************************************************************************

prompt

accept TAB1 prompt 'Choose a table name for the first creation method: '

DROP TABLE &&TAB1

prompt CREATE TABLE &&TAB1(

prompt ord_id ord_day NUMBER(2),

prompt ord_month NUMBER(2),

prompt ord_year NUMBER(4)

prompt )

prompt PARTITION BY RANGE (ord_year,ord_month,ord_day)


https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 3/12
18.07.2019 Document 165924.1

prompt SUBPARTITION BY HASH(ord_id) SUBPARTITIONS 8

prompt STORE IN (&&TBS1,&&TBS2,&&TBS3,&&TBS4,&&TBS5,&&TBS6,&&TBS7,&&TBS8)

prompt ( PARTITION P1 VALUES LESS THAN (2001,3,31),

prompt PARTITION P2 VALUES LESS THAN (2001,6,30),

prompt PARTITION P3 VALUES LESS THAN (2001,9,30),

prompt PARTITION P4 VALUES LESS THAN (2001,12,31)

prompt )

prompt /

CREATE TABLE &&TAB1(

ord_id NUMBER(10),

ord_day NUMBER(2),

ord_month NUMBER(2),

ord_year NUMBER(4)

PARTITION BY RANGE (ord_year,ord_month,ord_day)

SUBPARTITION BY HASH(ord_id) SUBPARTITIONS 8

STORE IN (&&TBS1,&&TBS2,&&TBS3,&&TBS4,&&TBS5,&&TBS6,&&TBS7,&&TBS8)

( PARTITION P1 VALUES LESS THAN (2001,3,31),

PARTITION P2 VALUES LESS THAN (2001,6,30),

PARTITION P3 VALUES LESS THAN (2001,9,30),

PARTITION P4 VALUES LESS THAN (2001,12,31)

pause Press return to continue ...

prompt
***********************************************************************************

https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 4/12


18.07.2019 Document 165924.1

prompt * The second method will explicitly named and stored each subpartition *

prompt * in a specified tablespace. In this example we will create only 16 subpartitions * * *

prompt
***********************************************************************************

prompt

accept TAB2 prompt 'Choose a table name for the second creation method: '

DROP TABLE &&TAB2

prompt CREATE TABLE &&TAB2(

prompt ord_id NUMBER(10),

prompt ord_day NUMBER(2),

prompt ord_month NUMBER(2),

prompt ord_year NUMBER(4)

prompt )

prompt PARTITION BY RANGE(ord_year,ord_month,ord_day)

prompt SUBPARTITION BY HASH(ord_id)

prompt SUBPARTITIONS 8

prompt ( PARTITION q1 VALUES LESS THAN(2001,3,31)

prompt ( SUBPARTITION q1_h1 TABLESPACE &&TBS1,

prompt SUBPARTITION q1_h2 TABLESPACE &&TBS2,

prompt SUBPARTITION q1_h3 TABLESPACE &&TBS3,

prompt SUBPARTITION q1_h4 TABLESPACE &&TBS4

prompt ),

prompt PARTITION q2 VALUES LESS THAN(2001,6,30)

prompt ( SUBPARTITION q2_h5 TABLESPACE &&TBS5,

prompt SUBPARTITION q2_h6 TABLESPACE &&TBS6,

prompt SUBPARTITION q2_h7 TABLESPACE &&TBS7,


https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 5/12
18.07.2019 Document 165924.1

prompt SUBPARTITION q2_h8 TABLESPACE &&TBS8

prompt ),

prompt PARTITION q3 VALUES LESS THAN(2001,9,30)

prompt ( SUBPARTITION q3_h1 TABLESPACE &&TBS1,

prompt SUBPARTITION q3_h2 TABLESPACE &&TBS2,

prompt SUBPARTITION q3_h3 TABLESPACE &&TBS3,

prompt SUBPARTITION q3_h4 TABLESPACE &&TBS4

prompt ),

prompt PARTITION q4 VALUES LESS THAN(2001,12,31)

prompt ( SUBPARTITION q4_h5 TABLESPACE &&TBS5,

prompt SUBPARTITION q4_h6 TABLESPACE &&TBS6,

prompt SUBPARTITION q4_h7 TABLESPACE &&TBS7,

prompt SUBPARTITION q4_h8 TABLESPACE &&TBS8

prompt )

prompt )

prompt /

CREATE TABLE &&TAB2(

ord_id NUMBER(10),

ord_day NUMBER(2),

ord_month NUMBER(2),

ord_year NUMBER(4)

PARTITION BY RANGE(ord_year,ord_month,ord_day)

SUBPARTITION BY HASH(ord_id)

SUBPARTITIONS 8

( PARTITION q1 VALUES LESS THAN(2001,3,31)

( SUBPARTITION q1_h1 TABLESPACE &&TBS1,

SUBPARTITION q1_h2 TABLESPACE &&TBS2,


https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 6/12
18.07.2019 Document 165924.1

SUBPARTITION q1_h3 TABLESPACE &&TBS3,

SUBPARTITION q1_h4 TABLESPACE &&TBS4

),

PARTITION q2 VALUES LESS THAN(2001,6,30)

( SUBPARTITION q2_h5 TABLESPACE &&TBS5,

SUBPARTITION q2_h6 TABLESPACE &&TBS6,

SUBPARTITION q2_h7 TABLESPACE &&TBS7,

SUBPARTITION q2_h8 TABLESPACE &&TBS8

),

PARTITION q3 VALUES LESS THAN(2001,9,30)

( SUBPARTITION q3_h1 TABLESPACE &&TBS1,

SUBPARTITION q3_h2 TABLESPACE &&TBS2,

SUBPARTITION q3_h3 TABLESPACE &&TBS3,

SUBPARTITION q3_h4 TABLESPACE &&TBS4

),

PARTITION q4 VALUES LESS THAN(2001,12,31)

( SUBPARTITION q4_h5 TABLESPACE &&TBS5,

SUBPARTITION q4_h6 TABLESPACE &&TBS6,

SUBPARTITION q4_h7 TABLESPACE &&TBS7,

SUBPARTITION q4_h8 TABLESPACE &&TBS8

--

-- Content of dictionary views

--

set heading on
https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 7/12
18.07.2019 Document 165924.1

pause Press return to continue ...

prompt Content of DBA_PART_TABLES

prompt ==========================

col partitioning_type format A20

col subpartitioning_type format A20

SELECT table_name, partitioning_type, subpartitioning_type,

partition_count, def_subpartition_count,

partitioning_key_count, subpartitioning_key_count

FROM dba_part_tables

prompt Each partition of &&TAB1 and &&TAB2 in DBA_TAB_PARTITIONS

prompt ===========================================================

column partition_name format A15

column tablespace_name format A20

column composite format A9

SELECT table_name, composite, partition_name,

subpartition_count, tablespace_name

FROM dba_tab_partitions

WHERE table_name in (UPPER('&&TAB1'), UPPER('&&TAB2'))

order by table_name, partition_position

pause Press return to continue ...

https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 8/12


18.07.2019 Document 165924.1

prompt Content of DBA_SEGMENTS

prompt =======================

column segment_name format A20

select segment_name, partition_name, segment_type, tablespace_name from dba_segments

where segment_name in (UPPER('&&TAB1'), UPPER('&&TAB2'))

order by segment_name, partition_name

prompt **************************************************************************

prompt * Note the subpartition names in the first table (system generated) *

prompt * versus the second table (explicitly named) and the tablespaces defined *

prompt **************************************************************************

prompt

pause Press return to continue ...

prompt Partition key in DBA_PART_KEY_COLUMNS

prompt =====================================

column name format A15

column column_name format A15

SELECT name, object_type, column_name, column_position FROM dba_part_key_columns

WHERE name in (UPPER('&&TAB1'), UPPER('&&TAB2'))

order by name, column_position

pause Press return to continue ...

https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 9/12


18.07.2019 Document 165924.1

prompt Partition key in DBA_SUBPART_KEY_COLUMNS

prompt ========================================

select name, object_type, column_name, column_position from user_subpart_key_columns

WHERE name in (UPPER('&&TAB1'), UPPER('&&TAB2'))

order by name, COLUMN_POSITION

prompt Content of DBA_OBJECTS

prompt ======================

column object_name format A15

column subobject_name format A15

SELECT object_name, subobject_name, object_type FROM dba_objects

WHERE object_name in (UPPER('&&TAB1'), UPPER('&&TAB2'))

order by object_name, subobject_name

pause Press return to continue ...

--

-- Create a large number of rows for the partitioning

-- Worked examples. It is up to the user to ensure there is appropriate

-- Temporary/Rollback space.

--

-- Sequence used for control

--

DROP SEQUENCE partition_seq

/
https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 10/12
18.07.2019 Document 165924.1

CREATE SEQUENCE partition_seq

BEGIN

FOR k in 1..50 LOOP

FOR j in 1..12 LOOP

FOR i in 1..30 LOOP

INSERT INTO &&TAB1 VALUES(partition_seq.nextval, i , j, 2001);

INSERT INTO &&TAB2 VALUES(partition_seq.nextval, i , j, 2001);

END LOOP;

END LOOP;

END LOOP;

END;

commit;

pause Press return to continue ...

set feedback off

analyze table &&TAB1 compute statistics;

analyze table &&TAB2 compute statistics;

set feedback on

prompt ******************************************************************

prompt * The twice tables have been now analyzed so the statistics will *

prompt * be accurate. *

prompt ******************************************************************

https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 11/12


18.07.2019 Document 165924.1

pause Press return to continue ...

prompt Number of rows loaded in each partitions

prompt ========================================

select table_name, partition_name, num_rows from dba_tab_partitions

where table_name in (UPPER('&&TAB1'), UPPER('&&TAB2'))

order by table_name, partition_position

prompt **************************************************************

prompt * you can verify now that the rows are evenly distributed on *

prompt * the different partitions. *

prompt **************************************************************

SAMPLE OUTPUT

Didn't find what you are looking for?

https://support.oracle.com/epmos/faces/DocContentD splay?_adf.ctrl-state=6v ry7tjf_297& d=165924.1 12/12

You might also like