Oracle table space and partition table

Source: Internet
Author: User

(1) The concept of table space and partitioned table
Table Space:
is a collection of one or more data files, all of which are stored in the specified table space, but are primarily tables, so called table spaces.
Partition table:
When the amount of data in the table is increasing, the query data slows down and the performance of the application degrades, so you should consider partitioning the table. Table is partitioned, the logical table is still a complete table, just the data in the table is physically stored in multiple "table space" (physical file), so that the query data, not every time the whole table is scanned, but only from the current partition to find the data you want to greatly improve the speed of data query.

(2). The specific role of table partitioning
Oracle's table partitioning capabilities bring great benefits to a wide variety of applications by improving manageability, performance, and availability. In general, partitioning can greatly improve the performance of certain queries and maintenance operations. In addition, partitioning can greatly simplify common administrative tasks, and partitioning is a key tool for building gigabytes of data systems or ultra-high availability systems. The partitioning feature can further subdivide a table, index, or index organization table into segments, where the segments of these database objects are called partitions. Each partition has its own name, and you can choose your own storage features. From the database administrator's point of view, a partitioned object has multiple segments that can be collectively managed or managed separately, which gives the database manager considerable flexibility in managing the objects after the partition. However, from the application's point of view, the partitioned table is exactly the same as the non-partitioned table, and no modifications are required to access the partitioned table using the SQL DML command.
When to use partitioned tables:
1, the size of the table is more than 2GB.
2. The table contains historical data, and new data is added to the new partition.

(3). Table Partitioning Advantages and disadvantages
Table partitioning has the following advantages:
1. Improve query performance: Queries on partitioned objects can search only the partitions they care about and improve the retrieval speed.
2. Enhanced usability: If one partition of the table fails, the data of the table in other partitions is still available;
3, Maintenance Convenience: If a partition of the table fails, you need to repair the data, only repair the partition;
4. Balanced I/O: Different partitions can be mapped to different disks to balance I/O and improve overall system performance.
Disadvantages:
Partition Table Related: Existing tables have no methods that can be converted directly into partitioned tables. However, Oracle provides the ability to redefine tables online.

(4). Several types of table partitioning and how to do it

I. Scope partitioning:
A range partition maps data to each partition based on its scope, which is determined by the partition key you specify when you create the partition. This partitioning method is most commonly used, and the partitioning key often takes the date. For example, you might partition sales data by month.
When using range partitioning, consider the following rules:
1. Each partition must have a values less than clause that specifies an upper value that is not included in the partition. Any record of the partition key that is equal to or greater than this upper value is added to the next higher partition.
2. All partitions, except the first one, will have an implicit lower value, which is the upper limit of the previous partition of this partition.
3. In the highest partition, MaxValue is defined. The MaxValue represents an indeterminate value. This value is higher than the value of any partition key in the other partition, or it can be understood to be higher than the value less than specified in any partition, including null values.
Example one:
Suppose there is a customer table with data 200000 rows, we partition this table through CUSTOMER_ID, each partition stores 100000 rows, and we save each partition in a separate tablespace so that the data file can span multiple physical disks. Here is the code to create the table and partition, as follows:

CREATE TABLE CUSTOMER
(
customer_id number not NULL PRIMARY KEY,
First_Name VARCHAR2 (+) not NULL,
Last_Name VARCHAR2 (+) not NULL,
PHONE VARCHAR2 () not NULL,
EMAIL VARCHAR2 (80),
STATUS CHAR (1)
)
PARTITION by RANGE (customer_id)
(
PARTITION cus_part1 VALUES less THAN (100000) tablespace cus_ts01,
PARTITION cus_part2 VALUES less THAN (200000) tablespace CUS_TS02
)

Example two: divided by time

CREATE TABLE order_activities
(
ORDER_ID Number (7) is not NULL,
Order_date DATE,
Total_amount number,
CUSTOTMER_ID Number (7),
PAID CHAR (1)
)
PARTITION by RANGE (order_date)
(
PARTITION ord_act_part01 VALUES Less THAN (to_date (' 01-may-2003 ', ' dd-mon-yyyy ')) tablespaceord_ts01,
PARTITION ord_act_part02 VALUES Less THAN (to_date (' 01-jun-2003 ', ' dd-mon-yyyy ') tablespace ord_ts02,
PARTITION ord_act_part02 VALUES Less THAN (to_date (' 01-jul-2003 ', ' dd-mon-yyyy ') tablespace ord_ts03
)

Example three: MAXVALUE

CREATE TABLE rangetable
(
IDD INT PRIMARY KEY,
Iname VARCHAR (10),
Grade INT
)
PARTITION by RANGE (grade)
(
PARTITION part1 VALUES less THAN (+) tablespace PART1_TB,
PARTITION part2 VALUES less THAN (MAXVALUE) tablespace PART2_TB
);

Two. List partition:

This partition is characterized by a column with only a few values, based on which we can take the list partition.
Example One

CREATE TABLE Problem_tickets
(
PROBLEM_ID Number (7) Not NULL PRIMARY KEY,
DESCRIPTION VARCHAR2 (2000),
CUSTOMER_ID Number (7) is not NULL,
Date_entered DATE not NULL,
STATUS VARCHAR2 (20)
)
PARTITION by LIST (STATUS)
(
PARTITION prob_active VALUES (' ACTIVE ') tablespace prob_ts01,
PARTITION prob_inactive VALUES (' INACTIVE ') tablespace prob_ts02)

Example Two

CREATE TABLE listtable
(
ID INT PRIMARY KEY,
Name VARCHAR (20),
Area VARCHAR (10)
)
PARTITION by LIST (area)
(
PARTITION part1 VALUES (' Guangdong ', ' Beijing ') tablespace PART1_TB,
PARTITION part2 VALUES (' Shanghai ', ' Nanjing ') tablespace PART2_TB
);

Three. Hash partition (also called hash partition):
This type of partitioning uses a hashing algorithm on column values to determine which partition the rows are placed in. Hash partitioning is recommended when the value of the column does not have an appropriate condition.
A hash partition is a type of partition that distributes data evenly by specifying the partition number, because the partitions are identical in size by hashing on the I/O device.
Example one:

CREATE TABLE hash_table
(
COL Number (8),
INF VARCHAR2 (100)
)
PARTITION by HASH (COL)
(
PARTITION PART01 tablespace hash_ts01,
PARTITION PART02 tablespace HASH_TS02,
PARTITION PART03 tablespace Hash_ts03
)

Shorthand:

CREATE TABLE EMP
(
Empno Number (4),
Ename VARCHAR2 (30),
Sal number
)
PARTITION by HASH (empno) Partitions 8
STORE in (TBS01,TBS02,TBS03,TBS04,TBS05,TBS06,TBS07,TBS08);

Hash partition The main mechanism is based on the hash algorithm to calculate the specific record should be inserted into which partition, hash algorithm is the most important hash function, Oracle if you want to use the hash partition, only specify the number of partitions. It is recommended that the number of partitions be 2 N, which makes the data distribution more evenly between partitions.

Four. Combining Range list Partitioning
This partition is based on a range partition and a list partition, where the table is first partitioned by a column, then a list is partitioned by a column, and the partitions in the partition are called sub-partitions.

CREATE TABLE SALES
(
product_id VARCHAR2 (5),
Sales_date DATE,
Sales_cost Number (10),
STATUS VARCHAR2 (20)
)
PARTITION by RANGE (sales_date) subpartition by LIST (STATUS)
(
PARTITION P1 VALUES Less THAN (to_date (' 2003-01-01 ', ' yyyy-mm-dd ') tablespace rptfact2009
(
Subpartition p1sub1 VALUES (' ACTIVE ') tablespace rptfact2009,
Subpartition p1sub2 VALUES (' INACTIVE ') tablespace rptfact2009
),
PARTITION P2 VALUES Less THAN (to_date (' 2003-03-01 ', ' yyyy-mm-dd ') tablespace rptfact2009
(
Subpartition p2sub1 VALUES (' ACTIVE ') tablespace rptfact2009,
Subpartition p2sub2 VALUES (' INACTIVE ') tablespace rptfact2009
)
)

Five. Composite range Hash Partition:
This partition is based on range partitioning and hash partitioning, where the table is first scoped by a column and then hashed by a column.

CREATE TABLE Dinya_test
(
TRANSACTION_ID Number primary Key,
ITEM_ID Number (8) is not NULL,
Item_description VARCHAR2 (300),
Transaction_date Date
)
Partition by Range (transaction_date) subpartition by hash (transaction_id) Subpartitions 3 store in (Dinya_space01,dinya_ SPACE02,DINYA_SPACE03)
(
Partition part_01 values less than (to_date (' 2006-01-01 ', ' yyyy-mm-dd ')),
Partition part_02 values less than (to_date (' 2010-01-01 ', ' yyyy-mm-dd ')),
Partition part_03 values less than (MaxValue)
);

(5). Some maintenance actions related to table partitioning:
First, add the partition
The following code adds a P3 partition to the sales table

ALTER TABLE SALES ADD PARTITION P3 VALUES less THAN (to_date (' 2003-06-01 ', ' yyyy-mm-dd ') tablespace space_name;

Note: When you add a partition, the condition of the added partition must be greater than the maximum value of the existing partition, otherwise the system will prompt ORA-14074 partition bound must collate higher than that's the last partition error.
The following code adds a P3SUB1 sub-partition to the P3 partition of the sales table

ALTER TABLE SALES MODIFY PARTITION P3 ADD subpartition p3sub1 VALUES (' complete ') tablespace space_name;

Second, delete the partition
The following code removes a partition named P3 in the sales table:

ALTER TABLE SALES DROP PARTITION P3;

The P4SUB1 sub-partition was removed in the following code:

ALTER TABLE SALES DROP subpartition p4sub1;

Note: If the deleted partition is the only partition in the table, then this partition will not be deleted and you must delete the table if you want to delete this partition.
Third, truncate the partition
Truncating a partition means deleting data from a partition, and does not delete the partition, nor does it delete data from other partitions. You can truncate a table even if it has only one partition. Truncate the partition with the following code:

ALTER TABLE SALES TRUNCATE PARTITION P2;

Truncate the sub-partition with the following code:

ALTER TABLE SALES TRUNCATE subpartition p2sub2;

Iv. Merging of partitions
Merging partitions is the merging of adjacent partitions into a single partition, with the result that partitions are bounded by higher partitions, and it is worth noting that partitions cannot be merged into lower-bound partitions. The following code implements the merge of the P1 P2 partition:

ALTER TABLE SALES MERGE partitions P1, P2 into PARTITION P2;

V. Splitting partitions
Splitting a partition splits a partition into two new partitions, and the original partition does not exist after the split, but the original partition can exist if the table has a PMAX partition. Note You cannot split a hash-type partition.

ALTER TABLE SALES sblit PARTITION P2 at (to_date (' 2003-02-01 ', ' yyyy-mm-dd ')] into (PARTITION p21,partition P22);
This split statement splits the P2 partition into partition P21 and partition P22 two partitions, with data less than 2003-02-01 stored in the P21 partition or stored in the P22 partition.

--table split partition with Pmax partition
ALTER TABLE Hs_his. Hisholdsinfo SPLIT PARTITION PMAX at (20100900) into (PARTITION P201008, PARTITION PMAX);
This split statement splits the PMAX partition into P201008 and PMAX two partitions, with data less than 20100900 stored in the P201008 partition or stored in the PMAX partition.

Vi. junction Zoning (Coalesca)
Combined partitioning is the data in the hash partition into other partitions, when the data in the hash partition is relatively large, you can increase the hash partition and then engage, it is worth noting that the bonded partition can only be used in the hash partition. Use the following code to make a bonded partition:

ALTER TABLE SALES Coalesca PARTITION;

Vii. Renaming table Partitions
The following code changes P21 to P2

ALTER TABLE SALES RENAME PARTITION P21 to P2;

Oracle table space and partition table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.