oracle -- partition (extent)

Basic relationship: database---tablespace---data segment---partition---data block


1. Partition (extent) A
partition extent is a storage structure one level larger than a data block, and is logically adjacent to several A combination of data blocks. We know that physical storage is usually a random read and write process. Even in the same file, we cannot guarantee that the same information is stored in absolutely continuous physical storage space. The same is true for Oracle datastores.

A partition extent is the smallest unit of disk space allocation. Disks are divided into zones, and at least one zone is allocated at a time. Regions are stored in segments, which consist of contiguous blocks of data. During the allocation process of the area, 5 areas are allocated each time. If the remaining free space is not enough for 5 extents, an error occurs: ORA-01653.

When storing data information, Oracle will allocate data blocks for storage, but it cannot guarantee that all allocated data blocks are continuous structures. Therefore, the concept of partition extent appears, which represents a series of consecutive sets of data blocks.

SQL>  desc dba_extents; Name Type Nullable Default Comments                                                  --------------- ------------- -------- ---- --- ----------------------------------------------- ----------OWNER VARCHAR2(128) Y Owner of the segment associated with the extent           



SEGMENT_NAME     VARCHAR2(128)    Y                Name of the segment associated with the extent            
PARTITION_NAME    VARCHAR2(128)    Y                Partition/Subpartition Name, if any, of the segment       
SEGMENT_TYPE      VARCHAR2(18)     Y                Type of the segment                                       
TABLESPACE_NAME VARCHAR2(30)    Y                Name of the tablespace containing the extent              
EXTENT_ID          NUMBER           Y                Extent number in the segment                              
FILE_ID             NUMBER          Y                Name of the file containing the extent                    
BLOCK_ID          NUMBER            Y                Starting block number of the extent                       
BYTES                 NUMBER           Y                Size of the extent in bytes                               
BLOCKS              NUMBER           Y                Size of the extent in ORACLE blocks                       
RELATIVE_FNO       NUMBER           Y                Relative number of the file containing the segment header

Second, when to use the 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. 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.



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.
When using range partitioning, please 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 less then specified in any partition, including null values.

 

Suppose there is a customer table with 200,000 rows of data. We partition this table, each partition stores 100,000 rows, and we save each partition to a separate tablespace, so that data files can span multiple physical disks . The following is the code for creating a table and partition, as follows:
Example 1: Partition by customer_id
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
)
Example 2: Divide by time
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
)
例三: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 partitioning:
List partitioning explicitly specifies partitioning based on a specific value of a field, rather than partitioning based on the value range of a field like range partitioning. Based on such 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,
    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 2
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
);
)
 


3. Hash partitioning:
This type of partitioning uses a hashing algorithm on column values ​​to determine which partition to put the row into. Hash partitioning is recommended when column values ​​do not have suitable conditions.
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 the i/o device.
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
)
:
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 partition is to calculate which partition a specific record should be inserted into according to the hash algorithm. The most important thing in the hash algorithm is hash function, if you want to use hash partitions 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 is first partitioned by a certain column, and then the table is partitioned by a certain column. The partitions in 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)
 );

 

6. 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;
deletes the p4sub1 subpartition in the following code:
alter table sales drop subpartition p4sub1;
Note: If the deleted partition is the only partition in the table, then this The partition will not be able to be dropped, to drop the partition, the table must be dropped.

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;

4. Merging Partitions
Merging partitions is to merge adjacent partitions into one partition, and the resulting partition will adopt the boundaries of higher partitions. It is worth noting that partitions cannot be merged into partitions with lower boundaries. The following code implements the merge of p1 p2 partitions:
alter table sales merge partitions p1,p2 into partition p2;

5. Split partition
Split partition One partition will be split into two new partitions. After the split, the original partition no longer exists. 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);

6. Coalesca Coalescing
partition is to combine the data in the hash partition into other partitions. When the data in the hash partition is relatively large, the hash partition can be added, and then coalesced. It is worth noting that coalescing Partitioning can only be used in hash partitioning. Join partition by the following code:
alter table sales coalesca partition;

Seven, rename the table partition
The following code will change 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 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 for 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 detailed partition information of all partition tables accessible by the current user:
select * from all_tab_partitions
 
-- Display detailed partition table information of all partition tables of the current user Partition information:
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 columns Display database Partition column information of all partition tables:
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 sub-partition columns Display sub-partition column information of all partition tables in the database:
select * from dba_subpart_key_columns
 
-- Display sub-partition column information of all partition tables accessible by the current user:
select * from all_subpart_key_columns
 
-- Display all partition tables of the current user Subpartition column information:
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
 
partition table data of a partition is
alter table table_name truncate partition p5;

Guess you like

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