分区的方法
1range分区2hash分区3list分区4rang-hash组合分区5range-list组合分区
全局索引可以使range或hash分区
什么时候使用range分区
使用range分区基于列值得范围,这个类型的分区对于能按日期分布的数据来说很有用,当数据能平均分布的时候,性能是最好的,如果列的数据很不平衡,那么要考虑别的分区方式。
当创建range分区时候,你要指定:
分区方法range
分区列
分区范围
如
CREATE TABLE sales ( invoice_no NUMBER, sale_year INT NOT NULL, sale_month INT NOT NULL, sale_day INT NOT NULL ) PARTITION BY RANGE (sale_year, sale_month, sale_day) ( PARTITION sales_q1 VALUES LESS THAN (1999, 04, 01) TABLESPACE tsa, PARTITION sales_q2 VALUES LESS THAN (1999, 07, 01) TABLESPACE tsb, PARTITION sales_q3 VALUES LESS THAN (1999, 10, 01) TABLESPACE tsc, PARTITION sales_q4 VALUES LESS THAN (2000, 01, 01) TABLESPACE tsd );
什么时候使用hash分区
hash分区提供了一个平衡数据分布的方法,下面的例子中,数据被分布在4个表空间中
CREATE TABLE scubagear (id NUMBER, name VARCHAR2 (60)) PARTITION BY HASH (id) PARTITIONS 4 STORE IN (gear1, gear2, gear3, gear4);什么时候使用list分区
你要控制数据分布的时候,使用list分区,多列的分区对于List是不支持的,分区列只能有1列。
CREATE TABLE q1_sales_by_region (deptno number, deptname varchar2(20), quarterly_sales number(10, 2), state varchar2(2)) PARTITION BY LIST (state) (PARTITION q1_northwest VALUES ('OR', 'WA'), PARTITION q1_southwest VALUES ('AZ', 'UT', 'NM'), PARTITION q1_northeast VALUES ('NY', 'VM', 'NJ'), PARTITION q1_southeast VALUES ('FL', 'GA'), PARTITION q1_northcentral VALUES ('SD', 'WI'), PARTITION q1_southcentral VALUES ('OK', 'TX'));
什么时候使用rang-hash组合分区
rang-hash分区使用range方法,对于子分区使用hash方法。下面的例子有3个分区,每个分区有8个子分区,有4个是命名了的,另外的由系统命名
CREATE TABLE scubagear (equipno NUMBER, equipname VARCHAR(32), price NUMBER) PARTITION BY RANGE (equipno) SUBPARTITION BY HASH(equipname) SUBPARTITIONS 8 STORE IN (ts1, ts2, ts3, ts4) (PARTITION p1 VALUES LESS THAN (1000), PARTITION p2 VALUES LESS THAN (2000), PARTITION p3 VALUES LESS THAN (MAXVALUE));
什么时候使用range-list分区
先range分区,然后在list分区。例子:
CREATE TABLE quarterly_regional_sales (deptno number, item_no varchar2(20), txn_date date, txn_amount number, state varchar2(2)) TABLESPACE ts4 PARTITION BY RANGE (txn_date) SUBPARTITION BY LIST (state) (PARTITION q1_1999 VALUES LESS THAN (TO_DATE('1-APR-1999','DD-MON-YYYY')) (SUBPARTITION q1_1999_northwest VALUES ('OR', 'WA'), SUBPARTITION q1_1999_southwest VALUES ('AZ', 'UT', 'NM'), SUBPARTITION q1_1999_northeast VALUES ('NY', 'VM', 'NJ'), SUBPARTITION q1_1999_southeast VALUES ('FL', 'GA'), SUBPARTITION q1_1999_northcentral VALUES ('SD', 'WI'), SUBPARTITION q1_1999_southcentral VALUES ('OK', 'TX') ), PARTITION q2_1999 VALUES LESS THAN ( TO_DATE('1-JUL-1999','DD-MON-YYYY')) (SUBPARTITION q2_1999_northwest VALUES ('OR', 'WA'), SUBPARTITION q2_1999_southwest VALUES ('AZ', 'UT', 'NM'), SUBPARTITION q2_1999_northeast VALUES ('NY', 'VM', 'NJ'), SUBPARTITION q2_1999_southeast VALUES ('FL', 'GA'), SUBPARTITION q2_1999_northcentral VALUES ('SD', 'WI'), SUBPARTITION q2_1999_southcentral VALUES ('OK', 'TX') ), PARTITION q3_1999 VALUES LESS THAN (TO_DATE('1-OCT-1999','DD-MON-YYYY')) (SUBPARTITION q3_1999_northwest VALUES ('OR', 'WA'), SUBPARTITION q3_1999_southwest VALUES ('AZ', 'UT', 'NM'), SUBPARTITION q3_1999_northeast VALUES ('NY', 'VM', 'NJ'), SUBPARTITION q3_1999_southeast VALUES ('FL', 'GA'), SUBPARTITION q3_1999_northcentral VALUES ('SD', 'WI'), SUBPARTITION q3_1999_southcentral VALUES ('OK', 'TX') ), PARTITION q4_1999 VALUES LESS THAN ( TO_DATE('1-JAN-2000','DD-MON-YYYY')) (SUBPARTITION q4_1999_northwest VALUES ('OR', 'WA'), SUBPARTITION q4_1999_southwest VALUES ('AZ', 'UT', 'NM'), SUBPARTITION q4_1999_northeast VALUES ('NY', 'VM', 'NJ'), SUBPARTITION q4_1999_southeast VALUES ('FL', 'GA'), SUBPARTITION q4_1999_northcentral VALUES ('SD', 'WI'), SUBPARTITION q4_1999_southcentral VALUES ('OK', 'TX') ) );
创建分区表
可以为常规或索引组织表创建分区,但是除了包含log或long raw的表。你可以创建非分区全局索引,range或hash分区全局索引,和本地索引在分区表上面。
对range和hash分区表来说,可以指定16个分区列,对多列分区键,当第一值无法确定分区时,使用第二个值以此类推。
CREATE TABLE supplier_parts ( supplier_id NUMBER, partnum NUMBER, price NUMBER) PARTITION BY RANGE (supplier_id, partnum) (PARTITION p1 VALUES LESS THAN (10,100), PARTITION p2 VALUES LESS THAN (10,200), PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE));
The following three records are inserted into the table:
INSERT INTO supplier_parts VALUES (5,5, 1000); INSERT INTO supplier_parts VALUES (5,150, 1000); INSERT INTO supplier_parts VALUES (10,100, 1000);
SELECT * FROM supplier_parts PARTITION (p1); SUPPLIER_ID PARTNUM PRICE ----------- ---------- ---------- 5 5 1000 5 150 1000 SELECT * FROM supplier_parts PARTITION (p2); SUPPLIER_ID PARTNUM PRICE ----------- ---------- ---------- 10 100 1000
这个列子说明在第一个分区要满足id<10,要是id>=10就到第二个分区,num要是>=100就到第二个分区,num>=200就到第三个分区,这个要注意了10 100 1000不是放到第三个分区的。
交换分区
你可以转换一个分区或子分区到一个非分区表中,非分区表转到分区或子分区表中。也可以交换hash分区表到range-hash分区表,或是range-hash分区表到hash分区表中,可以讲list分区交换到rang-list分区,或range-list交换到list分区中。
当你的应用使用非分区表,你想要转换分区的时候很有用,你可以指定索引是否也被交换,是否行被有效的映射。
为了避免验证的额外消耗,使用下面的遇见来禁用约束,然后在交换分区。
ALTER TABLE table_name DISABLE CONSTRAINT constraint_name KEEP INDEX
Then, enable the constraints after the exchange
交换range,hash,list分区ALTER TABLE stocks
EXCHANGE PARTITION p3 WITH TABLE stock_table_3;
交换range-hash分区中的hash分区
First, create a hash-partitioned table:
CREATE TABLE t1 (i NUMBER, j NUMBER) PARTITION BY HASH(i) (PARTITION p1, PARTITION p2);
Populate the table, then create a range-hash partitioned table as shown:
CREATE TABLE t2 (i NUMBER, j NUMBER) PARTITION BY RANGE(j) SUBPARTITION BY HASH(i) (PARTITION p1 VALUES LESS THAN (10) SUBPARTITION t2_pls1 SUBPARTITION t2_pls2, PARTITION p2 VALUES LESS THAN (20) SUBPARTITION t2_p2s1 SUBPARTITION t2_p2s2));
It is important that the partitioning key in table t1
is the same as the subpartitioning key in table t2
.
To migrate the data in t1
to t2
, and validate the rows, use the following statement:
alter table t2 exchange partiton p1 with table t1 with validattion;
合并分区
使用alter table ..merge partition来合并两个分区
你可以合并相邻的两个分区到一个分区中,不相邻的分区不能合并。
The following scripts create an example of merging range partitions.
First, create a partitioned table and create local indexes.
-- Create a Table with four partitions each on its own tablespace -- Partitioned by range on the data column. -- CREATE TABLE four_seasons ( one DATE, two VARCHAR2(60), three NUMBER ) PARTITION BY RANGE ( one ) ( PARTITION quarter_one VALUES LESS THAN ( TO_DATE('01-apr-1998','dd-mon-yyyy')) TABLESPACE quarter_one, PARTITION quarter_two VALUES LESS THAN ( TO_DATE('01-jul-1998','dd-mon-yyyy')) TABLESPACE quarter_two, PARTITION quarter_three VALUES LESS THAN ( TO_DATE('01-oct-1998','dd-mon-yyyy')) TABLESPACE quarter_three, PARTITION quarter_four VALUES LESS THAN ( TO_DATE('01-jan-1999','dd-mon-yyyy')) TABLESPACE quarter_four ); -- -- Create local PREFIXED index on Four_Seasons -- Prefixed because the leftmost columns of the index match the -- Partition key -- CREATE INDEX i_four_seasons_l ON four_seasons ( one,two ) LOCAL ( PARTITION i_quarter_one TABLESPACE i_quarter_one, PARTITION i_quarter_two TABLESPACE i_quarter_two, PARTITION i_quarter_three TABLESPACE i_quarter_three, PARTITION i_quarter_four TABLESPACE i_quarter_four );
Next, merge partitions.
-- -- Merge the first two partitions -- ALTER TABLE four_seasons MERGE PARTITIONS quarter_one, quarter_two INTO PARTITION quarter_two UPDATE INDEXES;
If you omit the UPDATE
INDEXES
clause from the preceding statement, then you must rebuild the local index for the affected partition.
-- Rebuild index for quarter_two, which has been marked unusable -- because it has not had all of the data from Q1 added to it. -- Rebuilding the index will correct this. -- ALTER TABLE four_seasons MODIFY PARTITION quarter_two REBUILD UNUSABLE LOCAL INDEXES;
分区的拆分,删除之类的就不翻译了,直接看在线文档吧。
视图:
The following views display information specific to partitioned tables and indexes:
View | Description |
---|---|
DBA_PART_TABLES
|
DBA view displays partitioning information for all partitioned tables in the database. ALL view displays partitioning information for all partitioned tables accessible to the user. USER view is restricted to partitioning information for partitioned tables owned by the user. |
DBA_TAB_PARTITIONS
|
Display partition-level partitioning information, partition storage parameters, and partition statistics generated by the DBMS_STATS package or the ANALYZE statement. |
DBA_TAB_SUBPARTITIONS
|
Display subpartition-level partitioning information, subpartition storage parameters, and subpartition statistics generated by the DBMS_STATS package or the ANALYZE statement. |
DBA_PART_KEY_COLUMNS
|
Display the partitioning key columns for partitioned tables. |
DBA_SUBPART_KEY_COLUMNS
|
Display the subpartitioning key columns for composite-partitioned tables (and local indexes on composite-partitioned tables). |
DBA_PART_COL_STATISTICS
|
Display column statistics and histogram information for the partitions of tables. |
DBA_SUBPART_COL_STATISTICS
|
Display column statistics and histogram information for subpartitions of tables. |
DBA_PART_HISTOGRAMS
|
Display the histogram data (end-points for each histogram) for histograms on table partitions. |
DBA_SUBPART_HISTOGRAMS
|
Display the histogram data (end-points for each histogram) for histograms on table subpartitions. |
DBA_PART_INDEXES
|
Display partitioning information for partitioned indexes. |
DBA_IND_PARTITIONS
|
Display the following for index partitions: partition-level partitioning information, storage parameters for the partition, statistics collected by the DBMS_STATS package or the ANALYZE statement. |
DBA_IND_SUBPARTITIONS
|
Display the following information for index subpartitions: partition-level partitioning information, storage parameters for the partition, statistics collected by the DBMS_STATS package or the ANALYZE statement. |
DBA_SUBPARTITION_TEMPLATES
|