oracle optimization tutorial - partitioning
DESCRIPTION
Learn how to use partitions and subpartitions in Oracle. Watch video lesson - visit: http://www.learn-with-video-tutorials.com/plsql-oracle-tuning-free-tutorialTRANSCRIPT
http://www.learn-with-video-tutorials.com/
PL/SQL OPTIMIZATION – PARTITIONING
Creating a Partitioned Table
As the number of rows in your tables grows, the management and performance impacts will
increase. Backups will take longer, recoveries will take longer, and queries that span an entire table
will take longer. You can mitigate the administrative and performance issues for large tables by
separating the rows of a single table into multiple parts. Dividing a table’s data in this manner is
called partitioning the table; the table that is partitioned is called a partitioned table, and the parts
are called partitions.
Partitioning is useful for very large tables. By splitting a large table’s rows across multiple smaller
partitions, you accomplish several important goals:
The performance of queries against the tables may improve because Oracle may have to
search only one partition (one part of the table) instead of the entire table to resolve a
query.
Backup and recovery operations may perform better. Because the partitions are smaller than
the partitioned table, you may have more options for backing up and recovering the
partitions than you would have for a single large table.
The table may be easier to manage. Because the partitioned table’s data is stored in multiple
parts, it may be easier to load and delete data in the partitions than in the large table.
To create a partitioned table, you specify how to set up the partitions of the table’s 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 VARCHAR2(40 BYTE),
COUNTRY VARCHAR2(10 BYTE),
YEAR INT,
SHOP_ID VARCHAR2(20 BYTE),
PRODUCT_ID VARCHAR2(10 BYTE),
SALES_DATA VARCHAR2(26 BYTE),
PRICE NUMBER(28, 2),
PRODUCT_VERSION VARCHAR2(15 BYTE),
CONSTRAINT trans_id_part_pk PRIMARY KEY (trans_id)
)
PARTITION BY RANGE (YEAR)
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.
Creating a hash partition
In addition to range partitions, Oracle supports hash partitions. A hash partition determines the
physical placement of data by performing a hash function on the values of the partition key. In range
partitioning, consecutive values of the partition key are usually stored in the same partition. In hash
partitioning, consecutive values of the partition key are not generally stored in the same partition.
Hash partitioning distributes a set of records over a greater set of partitions than range partitioning
does, potentially decreasing the likelihood for I/O contention.
To create a hash partition, use the partition by hash clause in place of the partition by range clause.
CREATE TABLE tb_transactions_part
(
trans_id VARCHAR2(40 BYTE),
country VARCHAR2(10 BYTE),
YEAR INT,
shop_id VARCHAR2(20 BYTE),
product_id VARCHAR2(10 BYTE),
sales_data VARCHAR2(26 BYTE),
price NUMBER(28, 2),
product_version VARCHAR2(15 BYTE),
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 VARCHAR2(40 BYTE),
country VARCHAR2(10 BYTE),
YEAR INT,
shop_id VARCHAR2(20 BYTE),
product_id VARCHAR2(10 BYTE),
sales_data VARCHAR2(26 BYTE),
price NUMBER(28, 2),
product_version 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 subpartitions—that 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 VARCHAR2(40 BYTE),
country VARCHAR2(10 BYTE),
YEAR INT,
shop_id VARCHAR2(20 BYTE),
product_id VARCHAR2(10 BYTE),
sales_data VARCHAR2(26 BYTE),
price NUMBER(28, 2),
product_version 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/
TABLESPACE system,
PARTITION part2
TABLESPACE example);
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 partitions—one 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.
More lessons: http://www.learn-with-video-tutorials.com/