Oracle Optimization Tutorial - Partitioning
Oracle Optimization Tutorial - Partitioning
To create a partitioned table, you specify how to set up the partitions of the tables data as part of the create table command. Typically, tables are partitioned by ranges of values (known as range partitioning).
CREATE TABLE tb_transactions_part ( TRANS_ID COUNTRY YEAR SHOP_ID PRODUCT_ID SALES_DATA PRICE PRODUCT_VERSION VARCHAR2(40 BYTE), VARCHAR2(10 BYTE), INT, VARCHAR2(20 BYTE), VARCHAR2(10 BYTE), VARCHAR2(26 BYTE), NUMBER(28, 2), VARCHAR2(15 BYTE),
http://www.learn-with-video-tutorials.com/
(PARTITION part1 VALUES LESS THAN (2012) TABLESPACE SYSTEM, PARTITION part2 VALUES LESS THAN (MAXVALUE) TABLESPACE example);
The tb_transactions_part table will be partitioned based on the values in the Year column. For any Years less than '2012', the records will be stored in the partition named PART1. The PART1 partition will be stored in the System tablespace. Any other years will be stored in the PART2 partition. You do not need to specify a maximum value for the last partition; the maxvalue keyword tells Oracle to use the partition to store any data that could not be stored in the earlier partitions.
CONSTRAINT trans_id_part_pk PRIMARY KEY (trans_id) ) PARTITION BY HASH (YEAR) PARTITIONS 2 STORE IN (system, example);
http://www.learn-with-video-tutorials.com/
The number of tablespaces specified in the store in clause does not have to equal the number of partitions. If more partitions than tablespaces are specified, the partitions are assigned to the tablespaces in a round-robin fashion.
You can specify named partitions. In this method, each partition is given a name and a tablespace, with the option of using an additional lob or varray storage clause. This method gives you more control over the location of the partitions, with the added benefit of letting you specify meaningful names for the partitions.
List Partitioning
You can use list partitions in place of range and hash partitioning. In list partitioning, you tell Oracle all the possible values and designate the partitions into which the corresponding rows should be inserted.
CREATE TABLE tb_transactions_part ( trans_id country YEAR shop_id product_id sales_data price product_version VARCHAR2(40 BYTE), VARCHAR2(10 BYTE), INT, VARCHAR2(20 BYTE), VARCHAR2(10 BYTE), VARCHAR2(26 BYTE), NUMBER(28, 2), VARCHAR2(15 BYTE),
CONSTRAINT trans_id_part_pk PRIMARY KEY (trans_id) ) PARTITION BY LIST (product_version) (PARTITION part1 VALUES ('standard', 'simple') TABLESPACE system, PARTITION part2 VALUES ('full', 'vip') TABLESPACE example);
Creating Subpartitions
You can create subpartitionsthat is, partitions of partitions. You can use subpartitions to combine all types of partitions: range partitions, list partitions, and hash partitions. For example, you can use hash partitions in combination with range partitions, creating hash partitions of the range partitions.
http://www.learn-with-video-tutorials.com/
For very large tables, this composite partitioning may be an effective way of separating the data into manageable and tunable divisions. The following example range-partitions the tb_transactions_part table by the year column, and it hash-partitions the year partitions by product version values.
CREATE TABLE tb_transactions_part ( trans_id country YEAR shop_id product_id sales_data price product_version VARCHAR2(40 BYTE), VARCHAR2(10 BYTE), INT, VARCHAR2(20 BYTE), VARCHAR2(10 BYTE), VARCHAR2(26 BYTE), NUMBER(28, 2), VARCHAR2(15 BYTE),
CONSTRAINT trans_id_part_pk PRIMARY KEY (trans_id) ) PARTITION BY RANGE (YEAR) SUBPARTITION BY HASH (product_version) SUBPARTITIONS 4 (PARTITION part1 VALUES LESS THAN (2011) TABLESPACE system, PARTITION part2 VALUES LESS THAN (MAXVALUE) TABLESPACE example);
The tb_transactions_part table will be range-partitioned into two partitions, using the year value ranges specified for the named partitions. Each of those partitions will be hash-partitioned on the product version column.
Indexing Partitions
When you create a partitioned table, you should create an index on the table. The index may be partitioned according to the same range values used to partition the table.
CREATE INDEX ix_trans_year ON tb_transactions_part(year) LOCAL (PARTITION part1
http://www.learn-with-video-tutorials.com/
In this create index command, no ranges are specified. Instead, the local keyword tells Oracle to create a separate index for each partition of the tb_transactions_part table. Two partitions were created on tb_transactions_part. This index will create two separate index partitionsone for each table partition. Because there is one index per partition, the index partitions are local to the partitions.
CREATE INDEX ix_trans_year_2 ON tb_transactions_part(year) GLOBAL;
The global clause in this create index command allows you to create a nonpartitioned index or to specify ranges for the index values that are different from the ranges for the table partitions. Local indexes may be easier to manage than global indexes; however, global indexes may perform uniqueness checks faster than local (partitioned) indexes perform them. You cannot create global indexes for hash partitions or subpartitions.
http://www.learn-with-video-tutorials.com/