Database partition table of commonly used commands

A, Oracle Partitions 
ORACLE technical partition is a very large tables, indexes, and other processing. Partitioning is a "divide and conquer" technique, by the large tables and indexes into manageable pieces, thus avoiding each table, as a large, separate objects managed, provides scalable performance for large amounts of data . By partitioning the dispensing operation to the memory cell smaller, reducing the time required for management operations, and improves performance by enhancing parallel processing, fault data by masking the partition, also increased the usability. 
Two, Oracle partitions the advantages and disadvantages 
? Advantages: 
Enhanced usability: If a partition table fails, the table of data in other partitions are still available; 
Easy maintenance: if a partition table fails, the data needs to be repaired, only repair the partition you can; 
balanced I / O: different partitions can be mapped to the disk to balance the I / O, improving overall system performance; 
improve query performance: the partition object query to search only their own partition of concern to improve the retrieval speed. 
? Disadvantages: 
partition table Related: table that already exists there is no way directly into the partition table. However, Oracle provides the ability to redefine the online tables. 
Three, Oracle partitioning methods 
range partitioning:? 
Range partitioning is a range of values in the data table partition, according to a range of values, which determine the data stored on the partition. According to numbers such as partitions, partition and other business records of the creation date. 
? The Hash partition (hash partition): 
Hash partition by specifying the partition number to the uniform distribution of a zoned type of data to hash partitions because the I / O devices, such that these partitions the same size. 
? List partitions (partition list): 
When you need to explicitly control how rows map to partitions, use list partitioning method. And range partitioning and hash partitioning is different, does not support multi-column list partitioning partition. If you want to list all the column column-wise, then the partition key can only, however, can be partitioned using range partitioning or hash partitioning method consists of a single-column table can be partitioned using the list partitioning methods. 
? Range - hash partitioning (composite partition): 
Sometimes we need to range partition after the data for each partition re-hash distributed in several table spaces, so we will use the composite partition. : Partition is the first use of composite range partition, and then uses a hash partitioning method partitions (be sure to pay attention to range partitioning) in each sub-region 
: - list partitioning (composite partitions) range? 
Combined range and a list of technologies first on the table range partitioning, and then again with a list of technical partition partition for each range. And combination range - different hash partitioning, all the contents of each sub-partition represents a logical subset of the data, is described by a list of the proper range and partitioning settings. (Note: Be sure to first range-partitioned)

Four, Oracle table partition table operations 
--Partitioning whether to true 
the SELECT * from v $ S the Order by the Option s.PARAMETER desc

--创建表空间 
CREATE TABLESPACE "PARTION_03" 
LOGGING 
DATAFILE 'D:ORACLEORADATAJZHUAPARTION_03.dbf' SIZE 50M 
EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO

- Delete table space 
drop tablespace partion_01


--范围 分区技术 
create table Partition_Test 

PID number not null, 
PITEM varchar2(200), 
PDATA date not null 

partition by range(PID) 

partition part_01 values less than(50000) tablespace dinya_space01, 
partition part_02 values less than(100000) tablespace dinya_space02, 
partition part_03 values less than(maxvalue) tablespace dinya_space03 
)

create table Partition_TTest 

PID number not null, 
PITEM varchar2(200), 
PDATA date not null 

partition by range(PDATA) 

partition part_t01 values less than(to_date('2004-01-01','yyyy-mm-dd')) tablespace dinya_space01, 
partition part_t02 values less than(to_date('2008-01-01','yyyy-mm-dd')) tablespace dinya_space02, 
partition part_t03 values less than(maxvalue) tablespace dinya_space03 
)

insert into Partition_Test(PID,PITEM,PDATA) select h.id,h.userid,h.rectime from st_handle h

select * from Partition_Test partition(part_01) t where t.pid = '1961'

--hash 分区技术 
create table Partition_HashTest 

PID number not null, 
PITEM varchar2(200), 
PDATA date not null 

partition by hash(PID) 

partition part_h01 tablespace dinya_space01, 
partition part_h02 tablespace dinya_space02, 
partition part_h03 tablespace dinya_space03 
)

insert into Partition_HashTest(PID,PITEM,PDATA) select h.id,h.userid,h.rectime from st_handle h

select * from Partition_HashTest partition(part_h03) t where t.pid = '1961'


--复合分区技术 
create table Partition_FHTest 

PID number not null, 
PITEM varchar2(200), 
PDATA date not null 

partition by range(PDATA) subpartition by hash(PID) subpartitions 3 store in (dinya_space01,dinya_space02,dinya_space03) 

partition part_fh01 values less than(to_date('2004-01-01','yyyy-mm-dd')) tablespace dinya_space01, 
partition part_fh02 values less than(to_date('2008-01-01','yyyy-mm-dd')) tablespace dinya_space02, 
partition part_fh03 values less than(maxvalue) tablespace dinya_space03 
)

insert into Partition_FHTest(PID,PITEM,PDATA) select h.id,h.userid,h.rectime from st_handle h

select * from Partition_FHTest partition(part_fh02) t where t.pid = '1961'

select * from Partition_FHTest partition(part_fh03) t

--速度比较 
select * from st_handle h where h.rectime > to_date('2008-01-01','yyyy-mm-dd');

select * from Partition_FHTest partition(part_fh03) t where t.pdata > to_date('2008-01-01','yyyy-mm-dd');


- Partition Table Operations

- increasing a partition 
alter table Partition_Test add partition part_05 values less than (10020) tablespace dinya_space03

- partition data query 
select * from Partition_FHTest partition (part_fh02) t

- Modify partition data 
update Partition_FHTest partition (part_fh02) t set t.PITEM = 'JZHUA' where t.pid = '1961'

- delete partition data 
delete from Partition_FHTest partition (part_fh02) t where t.pid = '1961'

--合并分区 
create table Partition_HB 

PID number not null, 
PITEM varchar2(200), 
PDATA date not null 

partition by range(PID) 

partition part_01 values less than(50000) tablespace dinya_space01, 
partition part_02 values less than(100000) tablespace dinya_space02, 
partition part_03 values less than(maxvalue) tablespace dinya_space03 
)

insert into Partition_HB(PID,PITEM,PDATA) select h.id,h.userid,h.rectime from st_handle h

select * from Partition_HB partition(part_03) t where t.pid = '100001'

alter table Partition_HB merge partitions part_01,part_02 into partition part_02;

- Split Partition 
- spilt partition partition name at (this is a critical area, such as: 50000 that is less than 50000 on part_01, and greater than 50,000 in the part_02) 
ALTER Split the Partition Table Partition_HB part_02 at (50000) into (Partition part_01 tablespace dinya_space01, Partition part_02 tablespace dinya_space02);

- Change the partition name 
alter table Partition_HB rename Partition part_01_test to part_02  ;
five, Oracle index partition operating table 
partition table and the table can still establish the general index, partition table can create local and global indexes. When the partition appears in a number of issues and to ensure that all recorded data partition using the uniqueness of the global index. When global indexing clause global range of allowed values specified index, this range is the range of values of the index field. In fact, in theory, there are three partitions in the index.

? Global Index (global index): 
For global index, you can choose whether to partition, and index partitions may not correspond to the partition table. When the partition maintenance operations, usually results Invalid global index must Rebuild after performing the operation. Update Global Indexes Oracle9i provides a statement, you can partition while performing maintenance rebuild global index. 
1: Partition storage location and the parent index information table (partition) completely irrelevant information. Even the parent table is not partitioned table does not matter.

Create index dinya_idx_t ON dinya_test (item_id) Global Partition by Range (item_id) ( 
Partition idx_1 values less Within last (1000) TABLESPACE dinya_space01, 
Partition idx_2 values less Within last (10000) TABLESPACE dinya_space02, 
Partition idx_3 values less Within last (MAXVALUE) TABLESPACE dinya_space03 
); 
2: but in this case, if the partition table is the parent table, the parent table you want to delete a partition must be updated Global index, or index information is incorrect 
ALTER tABLE TableName DROP pARTITION PartitionName update Global indexes

? Local index (local index):

For local indexes, each corresponding to a partition table index partition (a partition table that is a field can only build a local index), when a change in the partition table, the index maintenance is performed automatically by the Oracle; 
. 1: store the index information position dependent on the parent table partition (partition) information, in other words to create such an index must ensure that the parent table is partition (partition), index information stored in the table space partition where the parent table. 
2: But only you can create in the parent table HashTable or composite partitioned tables. 
3: Only you can create in the parent table HashTable or composite partitioned tables. And specify the number of partitions you want to be consistent with the number of partitions of the parent table.

create index dinya_idx_t on dinya_test(item_id) local ( 
partition idx_1 tablespace dinya_space01, 
partition idx_2 tablespace dinya_space02, 
partition idx_3 tablespace dinya_space03 
);

Do not specify a name index index partition table directly across 
the Create index dinya_idx_t ON dinya_test (item_id); 
------------------------------ partition 2 ----------------------- ------------------------- -------------------------------------------------- ----

 Recently doing a customer relationship management system, the project is to do not very successful, they still learned a lot of knowledge, due to the large amount of data, there is no dedicated staff oracle database support, database management, optimization and only about me who knows a little on. On the database optimization experience to write out a little hope to learn and communicate with everyone together.

  Optimization of large database tables: The nest table (clustered tables) and nest Index (Clustered Index)

  Nest and the nest indexing table is provided by a technique oracle, several basic idea is to have the same data item, and the table of frequently used together with the shared data storage block (data block) mode. Common fields between the tables as a nest key (cluster key), when accessing data in a database, first find the nest key, at the same time in order to obtain the relevant data for a number of tables. Cocooning table can bring the benefits can reduce I / O and reduce storage space, which I pay more attention to the former. Using partition table (Partition)

  Table partitioning the database is very large (the VLDB) is divided in the large tables and indexes in the form of several smaller partitions (Patition), manageable pieces, and each partition can be further divided into smaller sub-partitions (sub partition). And this partitioning is transparent to applications. By partition a table, you can get the following benefits:

  1) to reduce the possibility of data corruption.

  2) Each partition can separate backup and recovery, and enhance the manageability of the database.

  3) can control the distribution of partition on the hard drive, to balance IO, to improve the performance of the database.

  Table focus nest table partitioning vary, focus on improving the efficiency of the former association table between the query, the table partitioning performance can be focused on the management of a large table and local queries. And both of which it is extremely important for my systems. Because I am technical limitations, it is uncertain whether the two can achieve the same time, who have experience in this area to the point guidance would be greatly appreciated.

  In the case of both can not be achieved at the same time, it should have a choice in accordance with the functions to be achieved. Advantages and disadvantages of the two models, I believe that the use of table partitioning technology is more suitable for our application.

  Oracle's partition table has the following types:

  1) Range partitioning: partitioning the table range by a field or several fields.

  2) hash partition: the table by the value of a field to a number of uniformly distributed specified partition.

  3) Composite partition: combines the advantages of the previous two types of partition, the first partition table is a value range, then further evenly distributed hash mode data to the physical storage location.

  All things considered, in a third type is most advantageous. (I really Technology Co., using only the first one kind of range partitioning, because relatively simple, easy to manage)

  Optimization of the specific steps:

  1. Determine the need to optimize the partition table:

  After analysis of the system database table structure and fields of application, and now the big table to determine the need to partition:

  Such as account transaction list acct_detail.

  2. The method of determining the partition table and the partition keys:

  Partition Type: Range partitioning employed.

  Partitioning key:

  Range-partitioned by trans_date (trading hours) field.

  3. Determine the partition key range partitioning, and how many partitions intend points:

  Such as: account transactions list acct_detail.

  The field (TRANS_DATE) partitions into about:

  1). Division 1: 09/01/2003

  2). Subdivision 2: 10/01/2003

  3). Subdivision 3: 11/01/2003

  4). Subdivision 4: 12/01/2003

  5). Subdivision 5: 01/01/2004

  6). Subdivision 6: 02/01/2004

  The partition table is a clear need to increase in the future.

  4. The establishment of the partition table space and index space partition

  1). The establishment of table space for each partition of the table:

  1. Subdivision 1: crm_detail_200309

  CREATE TABLESPACE crm_detail_200309 DATAFILE

  ‘/u1/oradata/orcl/crm_detail_20030901.dbf’

  SIZE 2000M EXTENT MANAGEMENT LOCAL UNIFORM size 16M;

  After another month with more than (I use the method table locally managed oracle on this).

  2) To establish the partition table space index

  1. Subdivision 1: index_detail_200309

  CREATE TABLESPACE index_detail_200309 DATAFILE

  ‘/u3/oradata/orcl/index_detail_20030901.dbf’

  SIZE 2000M EXTENT MANAGEMENT LOCAL UNIFORM size 16M;

  5. Based on the establishment of the partition table:

  create table table name

  (

  ........

  enable row movment - this statement is able to modify the row partition key, that is, if not add this sentence can not modify the partition key record, the record does not partition mobility

  PARTITION BY RANGE (TRANS_DATE)

  (

  PARTITION crm_detail_200309 VALUES LESS THAN

  (TO_DATE (‘09/01/2003’,’mm/dd/yyyy’

  TABLESPACE crm_detail_200309,

  Other partitions .....

  ;

  6. Based on the establishment of an index partition:

  create index index_name on table_name (partitioning key + ...)

  global - global partitioned index is here, you can also build local indexes

  PARTITION BY RANGE (TRANS_DATE)

  (

  PARTITION index_detail_200309 VALUES LESS THAN

  (TO_DATE ('09/01/2003','mm/dd/yyyy' )

  TABLESPACE index_detail_200309,

  Other index partition ...

  ;

  Partition table is so complete, first determine the main partition table partition policy about is the most important, but I find it difficult to partition management table after the above table partition, because as the amount of data increases, the table there must delete the partition, expansion, increase and so on. In these processes, it is also involved in the overall index, etc., because of the partition table for the destruction of global indexes ddl operation, it must be re global index rebuild after ddl.
---------------- -------------------------------------------------- --------------------------------------------- --- partition 3 --------------------

Oracle's ordinary table no way to modify the properties directly into the partition table, must be converted by means of reconstruction, the following three methods is more efficient presentation, and their characteristics described.

Method One: Using the original table to rebuild the partition table.


step:


SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);


Table has been created.


SQL> INSERT INTO T SELECT ROWNUM, CREATED FROM DBA_OBJECTS;


6264 line has been created.


SQL> COMMIT;


Submit completed.

SQL> CREATE TABLE T_NEW (ID, TIME) PARTITION BY RANGE (TIME) 
2 (PARTITION P1 VALUES LESS THAN (TO_DATE(''2004-7-1'', ''YYYY-MM-DD'')), 
3 PARTITION P2 VALUES LESS THAN (TO_DATE(''2005-1-1'', ''YYYY-MM-DD'')), 
4 PARTITION P3 VALUES LESS THAN (TO_DATE(''2005-7-1'', ''YYYY-MM-DD'')), 
5 PARTITION P4 VALUES LESS THAN (MAXVALUE))
6 AS SELECT ID, TIME FROM T;


Table has been created.


SQL> RENAME T TO T_OLD;


Table has been renamed.


SQL> RENAME T_NEW TO T;


Table has been renamed.


SQL> SELECT COUNT(*) FROM T;


COUNT(*)
----------
6264


SQL> SELECT COUNT(*) FROM T PARTITION (P1);


COUNT(*)
----------
0


SQL> SELECT COUNT(*) FROM T PARTITION (P2);


COUNT(*)
----------
6246


SQL> SELECT COUNT(*) FROM T PARTITION (P3);


COUNT(*)
----------
18


Advantages: easy to use method, since DDL statements, will not produce the UNDO, the REDO and only a small amount, efficiency is relatively high, and the construction of the table has been completed the data distributed to the respective partitions.


Inadequate: For consistency of data also requires additional consideration. Because almost no way to ensure the consistency of the way by hand T table locking, statements directly changes may be lost in the CREATE TABLE statement and RENAME T_NEW TO T, if you want to ensure consistency, the data needs to be checked after executing the statement, and this price is relatively large. In addition to access to T performed between the execution of two RENAME statement fails.


For modified infrequent table, operate leisure, the amount of data the table should not be too large.

 


Method two: Use of the swap.


step:


SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);


Table has been created.


SQL> INSERT INTO T SELECT ROWNUM, CREATED FROM DBA_OBJECTS;


6264 line has been created.


SQL> COMMIT;


Submit completed.


SQL> CREATE TABLE T_NEW (ID NUMBER PRIMARY KEY, TIME DATE) PARTITION BY RANGE (TIME) 
2 (PARTITION P1 VALUES LESS THAN (TO_DATE(''2005-7-1'', ''YYYY-MM-DD'')), 
3 PARTITION P2 VALUES LESS THAN (MAXVALUE));


Table has been created.


SQL> ALTER TABLE T_NEW EXCHANGE PARTITION P1 WITH TABLE T;


Table has been changed.


SQL> RENAME T TO T_OLD;


Table has been renamed.


SQL> RENAME T_NEW TO T;


Table has been renamed.


SQL> SELECT COUNT(*) FROM T;


COUNT(*)
----------
6264


Pros: Just to partition the data dictionary and definitions were modified, without modification or copying data, maximum efficiency. If the distribution of the data in the partition is no further requirement, then, it is relatively simple. After performing the RENAME operation, to check whether there is data in T_OLD, if present, is inserted directly into the data in the T, T can be inserted to ensure that the operator will not be lost.


Inadequate: consistency problem still exists, then swap RENAME before T_NEW TO T, query, update, and delete errors can occur or can not access the data. If the required data is distributed to a plurality of partitions, the partitions need to be a SPLIT operation, increases the complexity of the operation, efficiency decreases.


Suitable for large data table containing operating to partition a partition table. It should be operated at leisure.

 


Method three: Oracle9i or later, use the online redefinition feature


step:


SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);


Table has been created.


SQL> INSERT INTO T SELECT ROWNUM, CREATED FROM DBA_OBJECTS;


6264 line has been created.


SQL> COMMIT;


Submit completed.


SQL> EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE(USER, ''T'', DBMS_REDEFINITION.CONS_USE_PK);


PL / SQL procedure successfully completed.


SQL> CREATE TABLE T_NEW (ID NUMBER PRIMARY KEY, TIME DATE) PARTITION BY RANGE (TIME) 
2 (PARTITION P1 VALUES LESS THAN (TO_DATE(''2004-7-1'', ''YYYY-MM-DD'')), 
3 PARTITION P2 VALUES LESS THAN (TO_DATE(''2005-1-1'', ''YYYY-MM-DD'')), 
4 PARTITION P3 VALUES LESS THAN (TO_DATE(''2005-7-1'', ''YYYY-MM-DD'')), 
5 PARTITION P4 VALUES LESS THAN (MAXVALUE));


Table has been created.


SQL> EXEC DBMS_REDEFINITION.START_REDEF_TABLE(USER, ''T'', ''T_NEW'', -
> ''ID ID, TIME TIME'', DBMS_REDEFINITION.CONS_USE_PK);


PL / SQL procedure successfully completed.


SQL> EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE(''YANGTK'', ''T'', ''T_NEW'');


PL / SQL procedure successfully completed.


SQL> SELECT COUNT(*) FROM T;


COUNT(*)
----------
6264


SQL> SELECT COUNT(*) FROM T PARTITION (P2);


COUNT(*)
----------
6246


SQL> SELECT COUNT(*) FROM T PARTITION (P3);


COUNT(*)
----------
18


Advantages: ensure data consistency, most of the time, the table T can perform normal DML operations. Only at the instant of switching the lock table, having a high availability. This method is very flexible, for a variety of needs can be met. Moreover, the switch can be made before the appropriate authority and the establishment of various constraints, can do not need any additional management actions after completion of the handover.


Inadequate: to achieve the slightly more complex than the above two.


Applicable in all situations.


Here only gives a simple example of re-definition table online, the detailed description and examples with reference to the following two articles.


Oracle online redefinition feature list: http://blog.itpub.net/post/468/12855


Oracle's online redefinition table feature (two): http://blog.itpub.net/post/468/12962

 

 

Second, the concept of index partitioning and indexing method


Index partition is after you create the partition table to build the index must be indexed partition. Divided two categories: one is based on the index information of each partition, called a local index partition (or partitions called local index). The other is the index together, called global index.

1, 2 local index and classification.
Established method:

 

ON Dept index ind_1 Create (DEPTNO)
local
(D1 Partition,
Partition D2);
(. 1) and the local prefix index partitions non-local prefix partition. If you are setting the index of the first field, and range partitioning column when the same, that is the local prefix index partitions.
Advantages are: in theory (I think of), say to your years as a range partition, a partition in 2007, the 2008 partition, and then you turn at this time to establish a local prefix index column partition, ORACLE will directly use the index on this zone only search on this partition, it would be very efficient.
20 million queries in table I created in practice, spending cost the local prefix composite index is 5, while the former is no partition 4. Of course, this does not matter. He has conducted several other inquiries, which cost almost the same.
(2) a non-local prefix index. If you create the index's first field column is not a column range, then called a local non-prefixed index.
The advantage is: If you search a phone number, it will appear in the year, when you want to count summary, this index will simultaneously put several partitions for parallel query processing, the speed theoretically faster.
But my trials comparing disappointed me: I built a non-partitioned tables 20 million, and then copy the table and again carried out six partitions. But the result at the time of inquiry statistics on a column, if one partition, the two speed or less, query speed partitions are: 0.25m, no partition query speed is: 0.065m. But when I look at the cross statistics, the first statistical time partition is: 61.875m, the second is: 10m; no partition table only: 3.703m.

2, the global index.
Established method:


ON Sales index ind_2 the Create (AMOUNT_SOLD)
, Ltd. Free Join Partition by range (AMOUNT_SOLD)
(d1 Partition,
Partition D2);
because of the global index of the first field must be a range field, so it does not matter prefix and a non-prefix are prefix.
After testing, I think the establishment of a global index of slightly less favorable than the speed of local prefix index.

I may have the ability, if not now think that the partition will also not be indexed. If you can give me doubts

------------------------------------------- -------------------------------------------------- ---------------------------

the Oracle of ordinary table is no way to modify the properties directly into the partition table, it must be carried out by way of reconstruction change, the following describes three more efficient method, and describes their characteristics.

 

 

 

Method One: Using the original table to rebuild the partition table.


step:


SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);


Table has been created.


SQL> INSERT INTO T SELECT ROWNUM, CREATED FROM DBA_OBJECTS;


6264 line has been created.


SQL> COMMIT;


Submit completed.

SQL> CREATE TABLE T_NEW (ID, TIME) PARTITION BY RANGE (TIME) 
  2  (PARTITION P1 VALUES LESS THAN (TO_DATE('2004-7-1', 'YYYY-MM-DD')), 
  3  PARTITION P2 VALUES LESS THAN (TO_DATE('2005-1-1', 'YYYY-MM-DD')), 
  4  PARTITION P3 VALUES LESS THAN (TO_DATE('2005-7-1', 'YYYY-MM-DD')), 
  5  PARTITION P4 VALUES LESS THAN (MAXVALUE))
  6  AS SELECT ID, TIME FROM T;


Table has been created.


SQL> RENAME T TO T_OLD;


Table has been renamed.


SQL> RENAME T_NEW TO T;


Table has been renamed.


SQL> SELECT COUNT(*) FROM T;


  COUNT(*)
----------
      6264


SQL> SELECT COUNT(*) FROM T PARTITION (P1);


  COUNT(*)
----------
         0


SQL> SELECT COUNT(*) FROM T PARTITION (P2);


  COUNT(*)
----------
      6246


SQL> SELECT COUNT(*) FROM T PARTITION (P3);


  COUNT(*)
----------
        18


Advantages: easy to use method, since DDL statements, will not produce the UNDO, the REDO and only a small amount, efficiency is relatively high, and the construction of the table has been completed the data distributed to the respective partitions.


Inadequate: For consistency of data also requires additional consideration. Because almost no way to ensure the consistency of the way by hand T table locking, statements directly changes may be lost in the CREATE TABLE statement and RENAME T_NEW TO T, if you want to ensure consistency, the data needs to be checked after executing the statement, and this price is relatively large. In addition to access to T performed between the execution of two RENAME statement fails.


For modified infrequent table, operate leisure, the amount of data the table should not be too large.

 


Method two: Use of the swap.


step:


SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);


Table has been created.


SQL> INSERT INTO T SELECT ROWNUM, CREATED FROM DBA_OBJECTS;


6264 line has been created.


SQL> COMMIT;


Submit completed.


SQL> CREATE TABLE T_NEW (ID NUMBER PRIMARY KEY, TIME DATE) PARTITION BY RANGE (TIME) 
  2  (PARTITION P1 VALUES LESS THAN (TO_DATE('2005-7-1', 'YYYY-MM-DD')), 
  3  PARTITION P2 VALUES LESS THAN (MAXVALUE));


Table has been created.


SQL> ALTER TABLE T_NEW EXCHANGE PARTITION P1 WITH TABLE T;


Table has been changed.


SQL> RENAME T TO T_OLD;


Table has been renamed.


SQL> RENAME T_NEW TO T;


Table has been renamed.


SQL> SELECT COUNT(*) FROM T;


  COUNT(*)
----------
      6264


Pros: Just to partition the data dictionary and definitions were modified, without modification or copying data, maximum efficiency. If the distribution of the data in the partition is no further requirement, then, it is relatively simple. After performing the RENAME operation, to check whether there is data in T_OLD, if present, is inserted directly into the data in the T, T can be inserted to ensure that the operator will not be lost.


Inadequate: consistency problem still exists, then swap RENAME before T_NEW TO T, query, update, and delete errors can occur or can not access the data. If the required data is distributed to a plurality of partitions, the partitions need to be a SPLIT operation, increases the complexity of the operation, efficiency decreases.


Suitable for large data table containing operating to partition a partition table. It should be operated at leisure.

 


Method three: Oracle9i or later, use the online redefinition feature


step:


SQL> CREATE TABLE T (ID NUMBER PRIMARY KEY, TIME DATE);


Table has been created.


SQL> INSERT INTO T SELECT ROWNUM, CREATED FROM DBA_OBJECTS;


6264 line has been created.


SQL> COMMIT;


Submit completed.


SQL> EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE(USER, 'T', DBMS_REDEFINITION.CONS_USE_PK);


PL / SQL procedure successfully completed.


SQL> CREATE TABLE T_NEW (ID NUMBER PRIMARY KEY, TIME DATE) PARTITION BY RANGE (TIME) 
  2  (PARTITION P1 VALUES LESS THAN (TO_DATE('2004-7-1', 'YYYY-MM-DD')), 
  3  PARTITION P2 VALUES LESS THAN (TO_DATE('2005-1-1', 'YYYY-MM-DD')), 
  4  PARTITION P3 VALUES LESS THAN (TO_DATE('2005-7-1', 'YYYY-MM-DD')), 
  5  PARTITION P4 VALUES LESS THAN (MAXVALUE));


Table has been created.


SQL> EXEC DBMS_REDEFINITION.START_REDEF_TABLE(USER, 'T', 'T_NEW', -
> 'ID ID, TIME TIME', DBMS_REDEFINITION.CONS_USE_PK);


PL / SQL procedure successfully completed.


SQL> EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE('YANGTK', 'T', 'T_NEW');


PL / SQL procedure successfully completed.


SQL> SELECT COUNT(*) FROM T;


  COUNT(*)
----------
      6264


SQL> SELECT COUNT(*) FROM T PARTITION (P2);


  COUNT(*)
----------
      6246


SQL> SELECT COUNT(*) FROM T PARTITION (P3);


  COUNT(*)
----------
        18


Advantages: ensure data consistency, most of the time, the table T can perform normal DML operations. Only at the instant of switching the lock table, having a high availability. This method is very flexible, for a variety of needs can be met. Moreover, the switch can be made before the appropriate authority and the establishment of various constraints, can do not need any additional management actions after completion of the handover.


Inadequate: to achieve the slightly more complex than the above two.


Applicable in all situations.


Here only gives a simple example of re-definition table online, the detailed description and examples with reference to the following two articles.


Oracle online redefinition feature list: http://blog.itpub.net/post/468/12855


Oracle's online redefinition table feature (two): http://blog.itpub.net/post/468/12962

 


Indexes can be partitioned, partition index There are two types: global and local. For local indexes, each corresponding to a partition table index partition when the partition table is changed, the index is automatically maintained by the Oracle. For global index, you can choose whether the partition, and a partition index may not correspond to the partition table. When the partition maintenance operations, often leads to INVALDED global index must REBUILD after performing the operation. Oracle9i provides UPDATE GLOBAL INDEXES statement, you can make while performing partition maintenance rebuild global index.

Local index global index value can contain multiple partitions easier to manage than the overall index, while the global index faster
Note: You can not create a global index partition hash partitioning or sub


Oracle's partition function is very powerful. But with up to find the two points is not easy:


The first table is not already present method can be converted directly to the partition table. However, Oracle provides the ability to redefine the online tables, regular tables can be done this way to convert the partition table. Can refer to this example: http://blog.itpub.net/post/468/13091


The second point is that if a local partitioned index, the increase in the partition table when the index table space partition is uncontrollable. If you want to separate table and index partitions to a different table space and index partitions are also distributed to various different table space, it can only be increased after the partition of the new partition index rebuild alone.

Oracle maximum allowable number of partitions exist?

We can find this information from the Oracle Concepts manual for Oracle9iR2:

Tables can be partitioned into up to 64,000 separate partitions.

For Oracle10gR2, Oracle enhanced partitioning features:

Tables can be partitioned into up to 1024K-1 separate partitions.

As to when it should be partitioned, Oracle has the following recommendations:

■ Tables greater than 2GB should always be considered for partitioning. 
■ Tables containing historical data, in which new data is added into the newest partition. A typical example is a historical table where only the current month's data is updatable and the other 11 months are read only.

Guess you like

Origin www.cnblogs.com/klb561/p/11329294.html