Oracle database table partitioning

1. The concept and understanding of Oracle database table partitioning
         1.1. There is no way for an existing table to be directly converted into a partitioned table.
         1.2. Do not create a partition index on the partition field, and create an index on other fields is equivalent to a global index. low efficiency.
         1.3. The concept of table space: A
  table space refers to a collection of one or more data files. All data objects are stored in the specified table space, but the main storage is the table, so it is called a table space.
        1.4. The concept of 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. Several types of Oracle database table partitions
         2.1. Range partition (Range partition)
         Range partition 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 commonly used, and the partition key is often date
         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 by time

  1 CREATE TABLE ORDER_ACTIVITIES
  2  (
  3  ORDER_ID      NUMBER(7) NOT NULL,
  4      ORDER_DATE    DATE,
  5      TOTAL_AMOUNT NUMBER,
  6      CUSTOTMER_ID NUMBER(7),
  7      PAID           CHAR(1)
  8  )
  9   PARTITION BY RANGE (ORDER_DATE)
 10  (
 11    PARTITION ORD_ACT_PART01 VALUES LESS THAN (TO_DATE('01- MAY -2003','DD-MON-YYYY')),
 12    PARTITION ORD_ACT_PART02 VALUES LESS THAN (TO_DATE('01-JUN-2003','DD-MON-YYYY')) ,
 13    PARTITION ORD_ACT_PART02 VALUES LESS THAN (TO_DATE('01-JUL-2003','DD-MON-YYYY'))
 14  );


       Example MAXVALUE

  1 CREATE TABLE RangeTable
  2  (
  3    idd   INT PRIMARY KEY ,
  4    iNAME VARCHAR(10),
  5    grade INT
  6  )
  7  PARTITION  BY  RANGE (grade)
  8  (
  9        PARTITION  part1 VALUES  LESS  THEN (1000) ,
 10        PARTITION  part2 VALUES  LESS  THEN (MAXVALUE)
 11  );

3. Some operations of partition
        3.1. View partition status

  1 select * from user_tab_partitions where table_name ='tableName';


        3.2. View partition data

  1 select * from tablename partiton(p1);


         3.3, modify the partition

  1 Add: alter table tablename add partition p4 values ​​less than(value);
   2 Delete: alter table tablename drop partiton p4;
   3 Truncate a partition Truncate a partition means to delete data in a partition, it will not delete the partition, nor Data in other partitions will not be deleted.
  4 alter table tablename truncate partiton p2;


         3.4. Merging partitions
         Merging partitions is merging 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 merging of P1 P2 partitions:

  1 ALTER TABLE TABLENAME MERGE PARTITIONS P1,P2 INTO PARTITION P2;


        3.5. Split partition
        Split partition One partition will be split into two new partitions, and the original partition will no longer exist after the split. Note that partitions of HASH type cannot be split.
       

  1 ALTER TABLE TABLENAME SBLIT PARTITION P2 AT(TO_DATE('2003-02-01','YYYY-MM-DD')) INTO (PARTITION P21,PARTITION P22);


         When updating data, it is not possible to perform cross-partition operations, and an error will occur. You need to set a movable partition to perform cross-partition query.

  1 alter table tablename enable row movement;


4. Interval partitioning between new features of Oracle11g database
         In Oracle10g, interval partitioning is not defined, and the interval partitioning function can only be realized through range partitioning. If you want to realize automatic creation of partitions, you can only achieve it by creating JOB or scheduler; and in 11g, Oracle directly provides the function of interval partitioning, which greatly simplifies the implementation of interval partitioning.
The grammar
        is mainly implemented through the INTERVAL keyword

  1 CREATE TABLE interval_sales
  2       ( prod_id NUMBER(6)
  3       , cust_id NUMBER
  4       , time_id DATE
  5       , channel_id CHAR(1)
  6      , promo_id NUMBER(6)
  7      , quantity_sold NUMBER(3)
  8      , amount_sold NUMBER(10,2)
  9        )
 10       PARTITION BY RANGE (time_id)
 11        INTERVAL(NUMTOYMINTERVAL(1, \'MONTH\'))
 12      ( PARTITION p0 VALUES LESS THAN (TO_DATE(\'1-1-2008\', \'DD-MM-YYYY\')),
 13        PARTITION p1 VALUES LESS THAN (TO_DATE(\'1-1-2009\', \'DD-MM-YYYY\')),
 14        PARTITION p2 VALUES LESS THAN (TO_DATE(\'1-7-2009\', \'DD-MM-YYYY\')),
 15        PARTITION p3 VALUES LESS THAN (TO_DATE(\'1-1-2010\', \'DD-MM-YYYY\')) )


The above sql statement creates 4 partitions with unequal intervals, which are all data before January 1, 2008, all data from 2008 to 2009, all data in the first half of 2009, and all data in the second half of 2009. ; At the same time, it also stipulates that after January 1, 2010, a separate partition will be created every month. It should be noted that the key value of interval partition can only be one column, and the column can only be of Date type or number type.
5. Oracle database table partition index After a brief introduction to
         partitioning, although the query efficiency can be improved, it only provides the range of data. Therefore, if necessary, we need to build an index in the partition to further improve the efficiency. There are two types of partitioned indexes. One class is called local. One class is called global.
         local: build an index on each partition (local index)
         global: one is to build an index globally. This way, the distribution of the partitions is the same, and is generally not used.

        Prefix index: Another is the index of the custom data interval. Also known as prefix index, this is very meaningful, custom interval value must use MAXVALUE. Another point is that the index built on the partition must be the partition field column.
         local index

  1 create index grade_index on tablename(grade)  local


        Example: Build indexes on three partitions p1, p2 and p3 respectively

  1 create index grade_index on studentgrade(grade)
   2      local --create local index partition
   3 based on table partition      (
   4       partition p1,
   5         partition p2,
   6         partition p3
   7       );


        View partition index

  1 select * from user_ind_partitions;


        global index

  1 create index idxname on tablename (field) global;


        prefix index

  1 create index idxname on tablename(field) global partition by range(field){
  2     partition p1 values less than(value),
  3     partition p2 values less than(maxvalue)
  4 };

Guess you like

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