标签:
1、增强可用性:如果表的某个分区出现故障,表在其他分区的数据仍然可用;
2、维护方便:如果表的某个分区出现故障,需要修复数据,只修复该分区即可;
3、均衡I/O:可以把不同的分区映射到磁盘以平衡I/O,改善整个系统性能;
4、改善查询性能:对分区对象的查询可以仅搜索自己关心的分区,提高检索速度。
PARTITION BY RANGE (column_name) ( PARTITION part1 VALUES LESS THAN(range1), PARTITION part2 VALUES LESS THAN(range2), ... [PARTITION partN VALUES LESS THAN(MAXVALUE)] );
CREATE TABLE Sales ( Product_ID varchar2 (5), Sales_Cost number (10) ) PARTITION BY RANGE (Sales_Cost) --根据 Sales_Cost 创建分区 ( PARTITION P1 VALUES LESS THAN (1000), --包含销售成本低于1000 的所有产品的值 PARTITION P2 VALUES LESS THAN (2000), PARTITION P3 VALUES LESS THAN (3000) ); --P1,P2,P3分区的名称
CREATE TABLE SALES2 ( PRODUCT_ID VARCHAR2(5), SALES_DATE DATE NOT NULL, SALES_COST NUMBER(10)) PARTITION BY RANGE (SALES_DATE) ( PARTITION P1 VALUES LESS THAN (DATE ‘2003-01-01‘), PARTITION P2 VALUES LESS THAN (DATE ‘2004-01-01‘), PARTITION P3 VALUES LESS THAN (MAXVALUE) );
PARTITION BY HASH (column_name) PARTITIONS number_of_partitions; 或 PARTITION BY HASH (column_name) ( PARTITION part1 [TABLESPACE tbs1], PARTITION part2 [TABLESPACE tbs2], ... PARTITION partN [TABLESPACE tbsN]);
散列分区示例
CREATE TABLE Employee ( Employee_ID varchar2 (5), Employee_Name varchar2(20), Department varchar2 (10) ) PARTITION BY HASH (Department) --在表 Employee上创建分区键 Department ( --创建 3 个分区 Partition D1, Partition D2, Partition D3 ); --D1,D2,D3分区的名称
CREATE TABLE EMPLOYEE ( EMP_ID NUMBER(4), EMP_NAME VARCHAR2(14), EMP_ADDRESS VARCHAR2(15), DEPARTMENT VARCHAR2(10) ) PARTITION BY HASH (DEPARTMENT) PARTITIONS 4;
PARTITION BY LIST (column_name) ( PARTITION part1 VALUES (values_list1), PARTITION part2 VALUES (values_list2), ... PARTITION partN VALUES (DEFAULT) );
列表分区示例
CREATE TABLE Employee ( Emp_ID number (4), Emp_Name varchar2 (14), Emp_Address varchar2 (15) ) --根据职员住址在表上创建的列表分区 PARTITION BY LIST (Emp_Address) ( Partition north values (‘芝加哥‘), --包含住在芝加哥的职员的记录 Partition west values (‘旧金山’, ‘洛杉矶‘), Partition south values (‘亚特兰大‘, ‘达拉斯‘, ‘休斯顿‘), Partition east values (‘纽约‘, ‘波斯顿‘) ); --north,west......分区的名称
PARTITION BY RANGE (column_name1) SUBPARTITION BY HASH (column_name2) SUBPARTITIONS number_of_partitions ( PARTITION part1 VALUE LESS THAN(range1), PARTITION part2 VALUE LESS THAN(range2), ... PARTITION partN VALUE LESS THAN(MAXVALUE) );
SQL> CREATE TABLE SALES ( PRODUCT_ID VARCHAR2 (5), SALES_DATE DATE NOT NULL, SALES_COST NUMBER (10) ) PARTITION BY RANGE (SALES_DATE) --在表的 Sales_Date 列中创建范围分区 SUBPARTITION BY HASH (PRODUCT_ID) --在表的 Product_ID 列创建散列子分区 SUBPARTITIONS 5 --在每个范围分区中 创建 5 个散列子分区 ( PARTITION S1 VALUES LESS THAN (TO_DATE(‘01/4月/2001‘, ‘DD/MON/YYYY‘)), PARTITION S2 VALUES LESS THAN (TO_DATE(‘01/7月/2001‘, ‘DD/MON/YYYY‘)), PARTITION S3 VALUES LESS THAN (TO_DATE(‘01/9月/2001‘, ‘DD/MON/YYYY‘)), PARTITION S4 VALUES LESS THAN (MAXVALUE) ); --S1,S2,S3创建的四个范围分区的名称
INSERT INTO SALES3 VALUES (‘P001‘, ‘02-3月-2001‘, 2000); INSERT INTO SALES3 VALUES (‘P002‘, ‘10-5月-2001‘, 2508); INSERT INTO SALES3 VALUES (‘P003‘, ‘05-7月-2001‘, 780); INSERT INTO SALES3 VALUES (‘P004‘, ‘12-9月-2001‘, 1080); SELECT * FROM SALES3 PARTITION (P3); DELETE FROM SALES3 PARTITION (P2);
SELECT * FROM useR_TAB_PARTITIONS WHERE TABLE_NAME=‘tableName‘
ALTER TABLE SALES ADD PARTITION P4 VALUES LESS THAN (4000);
ALTER TABLE SALES DROP PARTITION P4;
截断分区 – 删除指定分区中的所有记录
ALTER TABLE SALES TRUNCATE PARTITION P3;
ALTER TABLE SALES MERGE PARTITIONS S1, S2 INTO PARTITION S2;
ALTER TABLE SALES SPLIT PARTITION P2 AT (1500) INTO (PARTITION P21, PARTITION P22);
标签:
原文地址:http://www.cnblogs.com/hopeblog/p/5003319.html