Partitioning of oracle tables

This article organizes the concepts and operations of partition tables from the following aspects:
        1. The concept of table space and partition table
        2. The specific role of
        table partition 3. The advantages and disadvantages of
        table partition 4. Several types and operations of table partition Method
        5. Maintenance operations on table partitions.
(1.) Concept of table space and partition table
Table space: is a collection of one or more data files, all data objects are stored in the specified table space, but the main The table is stored, so it is called a table space.
Partitioned table:
When the amount of data in the table continues to increase, the speed of querying data will slow down, and the performance of the application will decrease. At this time, you should consider partitioning the table. After the table is partitioned, the table is still a complete table logically, but the data in the table is physically stored in multiple table spaces (physical files), so that when querying data, the entire table will not be scanned every time. surface.
 
(2). The specific role of table partitioning
Oracle 's table partitioning function brings great benefits to various applications by improving manageability, performance and availability. In general, partitioning can greatly improve the performance of certain queries as well as maintenance operations. In addition, partitioning can greatly simplify common administrative tasks, and partitioning is a key tool in building gigabyte data systems or ultra-high-availability systems.
 
Partitioning features the ability to further subdivide a table, index, or index-organized table into segments, these databaseSegments of objects are called partitions. Each partition has its own name and can also choose its own storage characteristics. From the database administrator's point of view, a partitioned object has multiple segments, which can be managed collectively or individually, which gives the database administrator considerable flexibility in managing the partitioned objects sex. However, from an application perspective, a partitioned table is exactly the same as a non-partitioned table, and no modification is required to access the partitioned table using SQL DML commands.
 
When to use a partition table:
1. The size of the table exceeds 2GB.
2. The table contains historical data, and new data is added to the new partition.
 
 (3) Advantages and disadvantages of
table partitioning Table partitioning has the following advantages:
1. Improve query performance: You can search only the partitions you care about when querying partitioned objects, which improves retrieval speed.
2. Enhanced availability: If a partition of the table fails, the data in other partitions of the table is still available;
3. Easy maintenance: If a partition of the table fails and data needs to be repaired, only the partition can be repaired;
4. Balanced I/O: Different partitions can be mapped to disks to balance I/O and improve overall system performance.
 
Disadvantages:
Partitioned table related: There is no way for an existing table to be directly converted to a partitioned table. However, Oracle provides the function of redefining tables online.
 
(4) Several types and operation methods of table partitioning
 1. Range partitioning:
Range partitioning maps data to each partition based on a range, which is determined by the partition key you specify when creating the partition. This partitioning method is the most common, and the partition key is often date. For example: you might partition your sales data by month.
When using range partitioning, consider the following rules:
1. Each partition must have a VALUES LESS THEN clause, which specifies an upper limit value that is not included in the partition. Any records with a partition key equal to or greater than this upper limit will be added to the next higher partition.
2. All partitions, except the first one, will have an implicit lower limit value, which is the upper limit value of the previous partition of this partition.
3. In the highest partition, MAXVALUE is defined. MAXVALUE represents an indeterminate value. This value is higher than the value of any partition key in other partitions, and can also be understood as higher than the value of VALUE LESS THEN specified in any partition, including null values.
Example 1:
Suppose there is a CUSTOMER table with 200,000 rows of data. We partition this table by CUSTOMER_ID, and each partition stores 100,000 rows. We save each partition to a separate table space, so that the data files can be spans 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(30) NOT NULL,
    LAST_NAME VARCHAR2(30) NOT NULL,
    PHONE VARCHAR2(15) 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
)
例二:按时间划分
CREATE TABLE ORDER_ACTIVITIES
(
    ORDER_ID      NUMBER(7) 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 3: MAXVALUE
CREATE TABLE RangeTable
( idd
  INT PRIMARY KEY ,
  iNAME VARCHAR(10),
  grade INT
)
PARTITION BY RANGE (grade)
(
      PARTITION part1 VALUES LESS THEN (1000) TABLESPACE Part1_tb,
      PARTITION part2 VALUES LESS THEN (MAXVALUE) TABLESPACE Part2_tb
);
 
2. List partition:
The feature of this partition is that there are only a few values ​​for a column, based on this The characteristics we can use list partitioning.
Example 1
CREATE TABLE PROBLEM_TICKETS
(
    PROBLEM_ID NUMBER(7) NOT NULL PRIMARY KEY,
    DESCRIPTION VARCHAR2(2000),
    CUSTOMER_ID NUMBER(7) NOT NULL,




















 


Hash partitioning is a type of partitioning that distributes data evenly by specifying partition numbers because the partitions are made uniform in size by hashing partitions on I/O devices.
Example 1:
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 (emp1,emp2,emp3,emp4,emp5,emp6,emp7,emp8);
 
The main mechanism of hash partitioning is to calculate which partition a specific record should be inserted into according to the hash algorithm. The most important part of the hash algorithm is the hash function. If you want to use hash partitioning in Oracle, you only need to specify the number of partitions. . It is recommended that the number of partitions be 2 to the nth power, which can make the data distribution among the partitions more uniform.
 
4. Combined range hash partitioning
This partitioning is based on range partitioning and list partitioning. The table first performs range partitioning by a certain column, and then performs list partitioning by a certain column. The partitions among the partitions 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
          )
)
 
5. Compound range hash partitioning:
This partitioning is based on range partitioning and hash partitioning. The table first performs range partitioning by a certain column, and then performs hash partitioning by a certain column.
create table dinya_test
 (
 transaction_id number primary key,
 item_id number(8) 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 operations related to table partitions:
1. Add 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'));
Note: The partition boundary added above should be higher than the last partition boundary.
The following code adds a P3SUB1 subpartition to the P3 partition of the SALES table
ALTER TABLE SALES MODIFY PARTITION P3 ADD SUBPARTITION P3SUB1 VALUES('COMPLETE');
Second, delete the partition
The following code deletes the P3 table partition: ALTER
TABLE SALES DROP PARTITION P3;
The following code deletes the P4SUB1 subpartition:
ALTER TABLE SALES DROP SUBPARTITION P4SUB1;
Note: If the deleted partition is the only partition in the table, then this partition cannot be deleted. To delete this partition, the table must be deleted.
3. Truncating a partition
Truncating a partition means deleting the data in a certain partition, and neither the partition nor the data in other partitions will be deleted. When there is even only one partition in the table, that partition can be truncated. Truncate the partition by the following code:
ALTER TABLE SALES TRUNCATE PARTITION P2;
Truncate the subpartition by the following code:
ALTER TABLE SALES TRUNCATE SUBPARTITION P2SUB2;
Fourth, merge partitions
Merge partitions merge adjacent partitions into one partition, and the resulting partition will use higher Bounds of partitions, it is worth noting that partitions cannot be merged into partitions with lower bounds. The following code realizes the merging of P1 P2 partitions:
ALTER TABLE SALES MERGE PARTITIONS P1, P2 INTO PARTITION P2;
5. Split partitions
Split partitions Split one partition into two new partitions, and the original partitions no longer exist after the split. Note that partitions of HASH type cannot be split.
ALTER TABLE SALES SBLIT PARTITION P2 AT(TO_DATE('2003-02-01','YYYY-MM-DD')) INTO (PARTITION P21,PARTITION P22);
Six, coalesca
combined partition is to hash partition The data in the partition is joined to other partitions. When the data in the hash partition is relatively large, the hash partition can be added and then joined. It is worth noting that the joined partition can only be used in the hash partition. Join the partition by the following code:
ALTER TABLE SALES COALESCA PARTITION;
7. Rename the table partition
The following code changes P21 to P2
ALTER TABLE SALES RENAME PARTITION P21 TO P2;
8. Related queries
Cross-partition query
select sum( *) from
(select count(*) cn from t_table_SS PARTITION (P200709_1)
union all
select count(*) cn from t_table_SS PARTITION (P200709_2)
) ;
Query how many partitions there are on the table
SELECT * FROM useR_TAB_PARTITIONS WHERE TABLE_NAME='tableName'Query
index information
select object_name,object_type,tablespace_name,sum(value)
from v$segment_statistics
where statistic_name IN ('physical reads','physical write',' logical reads')and object_type='INDEX'
group by object_name,object_type,tablespace_name
order by 4 desc --Display
 
information about all partition tables in the database:
select * from DBA_PART_TABLES --Display
 
information about all partition tables accessible by the current user:
select * from ALL_PART_TABLES --Display
 
information about all partition tables of the current user:
select * from USER_PART_TABLES --Display
 
table partition information Display detailed partition information of all partition tables in the database:
select * from DBA_TAB_PARTITIONS --Display
 
all partition tables accessible by the current user Detailed partition information:
select * from ALL_TAB_PARTITIONS
 
-- Display detailed partition information of all partition tables of the current user:
select * from USER_TAB_PARTITIONS
 
-- Display sub-partition information Display sub-partition information of all combined partition tables of the database:
select * from DBA_TAB_SUBPARTITIONS
 
-- Display Subpartition information of all combined partition tables accessible by the current user:
select * from ALL_TAB_SUBPARTITIONS --Display subpartition information of all combined partition tables of the
 
current user:
select * from USER_TAB_SUBPARTITIONS --Display
 
partition column Display partition column information of all partition tables in the database :
select * from DBA_PART_KEY_COLUMNS
 
-- Displays the partition column information of all partition tables accessible by the current user:
select * from ALL_PART_KEY_COLUMNS
 
-- Displays the partition column information of all partition tables of the current user:
select * from USER_PART_KEY_COLUMNS --Display subpartition
 
columns Display subpartition column information of all partition tables in the database: select * from DBA_SUBPART_KEY_COLUMNS --Display subpartition column information
of
 
all partition tables accessible to the current user:
select * from ALL_SUBPART_KEY_COLUMNS --Display
 
current Subpartition column information of all partitioned tables of the user:
select * from USER_SUBPART_KEY_COLUMNS --how
 
to query all partitioned tables in the oracle database
select * from user_tables a where a.partitioned='YES' --deleting
 
a table's data is
truncate table table_name; --Delete
 
the data of a partition of a partition table is
alter table table_name truncate partition p5;
 
 
[PS]: When update changes the partition to which the data belongs, the table row movement permission must be granted.
alter table test_partition_tab enable/disable row movement;
update the partition field, change the partition to which it belongs, and the internal change of the data is through the steps of inserting the source data into the new partition, updating the new data, and deleting the old data of the source, so the rowid will change; the data index will also be changed. will go again.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325917934&siteId=291194637