[bbk2902] 第58集 - Chapter 14-Using Oracle Dasta Storage Structures Efficiently(04)
Hash Partitioning Overview
- Easy to Implement
- Enables better performance for PDML and partion-wise joins
- PDML->P DML并发的修改数据
- partition-wise join只能的Join,只需要有用的Partition 进行Join,没用的partition就不参加Join
- Inserts rows into partitions automatically based on the hash of the partion key
- Supports(hash) local indexes
- partition table索引分为local indexes和global indexes
- Does not support(hash) global indexes(oracle 10g以后,不再适用)
Hash partitions 的目的,就是使数据能够均匀的分布在不同的分区或者不同的磁盘上.
不像partition range一样机械的将数据存放在不同的分区磁盘上.
里面的hash函数是由Oracle 提供的,用户是无法修改的.
hash partition table 在设计分区数量时,Oracle 建议分配2的幂次方倍个(2,4,8,16,32...)
create hash partition table
CREATE TABLE emp ( empno INT, ename VARCHAR2(20) ) PARTITION BY HASH(empno) ( --分区的个数最好是2的幂次方倍2,4,8,16,32............... partition part1 tablespace ts1, partition part2 tablespace ts2 );
insert data into hash partition table to emp;
SQL> show user USER is "U3" SQL> select object_name,object_type from user_objects; OBJECT_NAME OBJECT_TYPE ------------------------------ ------------------- EMP TABLE EMP TABLE PARTITION EMP TABLE PARTITION RANGE1 TABLE PARTITION RANGE1 TABLE RANGE1 TABLE PARTITION RANGE1 TABLE PARTITION RANGE1 TABLE PARTITION RANGE1 TABLE PARTITION HC CLUSTER 10 rows selected. SQL> insert into emp select empno,ename from SCOTT.emp; 14 rows created. SQL> commit; Commit complete. SQL> select * from emp; EMPNO ENAME ---------- -------------------- 7369 SMITH 7499 ALLEN 7654 MARTIN 7698 BLAKE 7782 CLARK 7839 KING 7876 ADAMS 7934 MILLER 7521 WARD 7566 JONES 7788 SCOTT EMPNO ENAME ---------- -------------------- 7844 TURNER 7900 JAMES 7902 FORD 14 rows selected.
select data from hash partition table;
SQL> select * from emp partition(part1); EMPNO ENAME ---------- -------------------- 7369 SMITH 7499 ALLEN 7654 MARTIN 7698 BLAKE 7782 CLARK 7839 KING 7876 ADAMS 7934 MILLER 8 rows selected. SQL> SQL> select * from emp partition(part2); EMPNO ENAME ---------- -------------------- 7521 WARD 7566 JONES 7788 SCOTT 7844 TURNER 7900 JAMES 7902 FORD 6 rows selected.
以上实验一切正常.创建hash partition table emp;设计两个tablespace(ts1,ts2)存放数据;
现在进行一场破坏性实验,模拟tb1损坏(以SYSDBA用户登陆,设置ts1 offline)
SQL> alter tablespace ts1 offline; Tablespace altered.
再查询位于ts1上的数据(也就是part1),会出现如下错误(因为位于ts1上的磁盘已经损坏)
SQL> select * from emp; select * from emp * ERROR at line 1: ORA-00376: file 9 cannot be read at this time ORA-01110: data file 9: '/RealData/oradata/DATACENTER/ts1_01.dbf' SQL> select * from emp partition(part1); select * from emp partition(part1) * ERROR at line 1: ORA-00376: file 9 cannot be read at this time ORA-01110: data file 9: '/RealData/oradata/DATACENTER/ts1_01.dbf'
再查询位于ts2上的数据(也就是part2),一切正常(因为ts2所在的磁盘没有损坏)
SQL> select * from emp partition(part2); EMPNO ENAME ---------- -------------------- 7521 WARD 7566 JONES 7788 SCOTT 7844 TURNER 7900 JAMES 7902 FORD 6 rows selected.
恢复ts1 online状态,然后再次查询一切正常.
SQL> alter tablespace ts1 online; Tablespace altered.
SQL> select * from emp partition(part2); EMPNO ENAME ---------- -------------------- 7521 WARD 7566 JONES 7788 SCOTT 7844 TURNER 7900 JAMES 7902 FORD 6 rows selected. SQL> select * from emp; EMPNO ENAME ---------- -------------------- 7369 SMITH 7499 ALLEN 7654 MARTIN 7698 BLAKE 7782 CLARK 7839 KING 7876 ADAMS 7934 MILLER 7521 WARD 7566 JONES 7788 SCOTT EMPNO ENAME ---------- -------------------- 7844 TURNER 7900 JAMES 7902 FORD 14 rows selected. SQL> select * from emp partition(part2); EMPNO ENAME ---------- -------------------- 7521 WARD 7566 JONES 7788 SCOTT 7844 TURNER 7900 JAMES 7902 FORD 6 rows selected.
List Partitioning Example
创建List Partitioning script
CREATE TABLE locations ( location_id int, street_address VARCHAR2(32), postal_code VARCHAR2(16), city VARCHAR2(16), state_province VARCHAR2(24), country_id int ) STORAGE(INITIAL 10K NEXT 20K) TABLESPACE users PARTITION BY LIST(state_province) ( PARTITION regeion_east('MA','NY','CT','ME','MD'), PARTITION regeion_west('ca','AZ','NM','OR','WA'), PARTITION regeion_south('TX','KY','TN','LA','MS'), PARTITION regeion_central'OH','ND','SD','MO','IL') );
Default Partition
Create a DEFAULT list patition for all values not convered by other partitions:
如果在定义的时候,定了DEFAULT PARTITION,以后再想插入新的分区(不同通过ALTER TABLE ...),基本上就无法插入新的分区了.如果要想插入新的分区,就只能先将DEFAULT PARTITION给干掉,然后插入新的分区,再然后加入DEFAULT PARTITION;
CREATE TABLE customer ... PARTITION BY LIST(state) ( PARTITION P1 VALUES('CA','CO'), PARTITION P2 VALUES('FL','TX'), PARTITION P3 VALUES(DEFAULT) );
CREATE TABLE list1 ( stateid VARCHAR2(2), data VARCHAR2(100) ) PARTITION BY LIST(stateid) ( PARTITION p1 VALUES('TX','MA','NY') TABLESPACE TS0, PARTITION p2 VALUES('CA','PA','LA') TABLESPACE TS0, PARTITION p3 VALUES(DEFAULT) TABLESPACE TS0 );
Composite Partitioning
- Ideal for both historical data and data placement
- Provides high availability and manageability,like range partitioning.
- Improves performance for parallel DML and supports partition-wise joins
- Allows more granular partition elimination
- Supports composite local indexes
- Dose not support composite global indexes
create composite partitions table
CREATE TABLE composite1 ( range_key DATE, hash_key INT, data VARCHAR2(100) ) PARTITION BY RANGE(range_key) SUBPARTITION BY HASH(hash_key) subpartitions 2 ( PARTITION part1 VALUES LESS THAN (TO_DATE('01/01/2014','dd/mm/yyyy')) ( subpartition h1, subpartition h2 ), PARTITION part2 VALUES LESS THAN (TO_DATE('01/01/2015','dd/mm/yyyy')) ( subpartition h11, subpartition h22 ) );
如何删除一个composite partition table?是同删除普通Heap表一样吗?