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);