暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle 分区攻略

东面而视 2018-01-11
388

说到大数据,在传统数据库里面,分区是首要的解决方案。那么,我总结了一下公司里面的培训资料,做了一份Oracle分区攻略。仅供学习参考。 

首先,回顾一下Oracle的表空间创建和表创建。 

--表空间创建create tablespace user_temp  
tempfile 'D:\oracle\oradata\Oracle9i\user_temp1.dbf'size 50m,	 'D:\oracle\oradata\Oracle9i\user_temp1.dbf'size 50m   --指出表空间包含什么文件,默认大小autoextend on    --是否自动扩展next 50m maxsize 20480m  --每次扩大多少,扩张到最大是多少extent management local;--创建表create table X_SMALL_AREA  
(  
  SMALL_AREA_ID NUMBER(10) not null  )  tablespace TBSL_SDDQ --表段X_SMALL_AREA放在表空间TBSL_SDDQ中
  pctfree 10 --指定一个百分比 比如说10% 那么当某个数据块使用率超过百分之80的时候系统就会停止往这个数据块里
   --插入新的数据 剩下10% 空间留给将来对数据的更新使用 这样可以防止迁移行和链接行的出现
  initrans 1 --初始化事务槽的个数
  maxtrans 255 --指定最多有多少个事务可以并发操作此数据块
  storage --存储参数
  (  
  initial 64k --区段(extent)一次扩展64k
  minextents 1 --最小区段数
  maxextents unlimited --最大区段无限制  
  );
复制



表空间和表建好了,就要正式开始我们的分区攻略了。 
范围分区: 

drop table DEPT_RNG;CREATE TABLE DEPT_RNG
(DEPTNO NUMBER(2),
 DEPTNAME  VARCHAR2(30)) PARTITION BY RANGE(DEPTNO)
      (PARTITION D1 VALUES   LESS THAN (10) tablespace ts1,       PARTITION D2 VALUES   LESS THAN (20),       PARTITION D3 VALUES   LESS THAN (30) tablespace ts3);select segment_name,partition_name,tablespace_name from dba_segmentswhere owner='TEST' and segment_name='DEPT_RNG';select table_name,partition_name,high_valuefrom user_tab_partitionswhere table_name='DEPT_RNG';insert into DEPT_RNG values(1,'Fin');insert into DEPT_RNG values(10,'Tech');insert into DEPT_RNG values(25,'HR');commit;select * from DEPT_RNG;select * from DEPT_RNG partition (d1);select * from DEPT_RNG partition (d2);select * from DEPT_RNG partition (d3);--如果插入的数据在规定分区以外就会出现	ORA-1440insert into DEPT_RNG values(null,'Office');	
--ORinsert into DEPT_RNG values(40,'Office');
ERROR at line 1:
ORA-14400: inserted partition key does not map to any partition--这个时候就要求添加一个分区:alter table DEPT_RNG add partition Dmax values less than (maxvalue);--为了更好第解决这种分区问题,Oracle 11g推出了Interval Partition(区间分区)CREATE TABLE DEPT_NEW2
(DEPTNO NUMBER(2),
DEPT_NAME VARCHAR2(30))PARTITION BY RANGE(DEPTNO)INTERVAL(10)
(PARTITION D1 VALUES LESS THAN (10),PARTITION D2 VALUES LESS THAN (20),PARTITION D3 VALUES LESS THAN (30))
;
这时候,再插入insert into DEPT_RNG values(40,'Office');
通过查询:select segment_name, partition_namefrom dba_segmentswhere segment_name = 'DEPT_NEW2'就会由dbms自动创建一个SYS_开头的分区,根据INTERVAL(10)的设置,RANGE每增加,新增一个分区。
根据日期的范围分区使用以下:interval (numtoyminterval (1,’month’)) --每一个月份增加一个分区interval (numtoyminterval (1,’year’))  --每一年增加一个分区这样创建的表分区会默认分配在SYSTEM表空间。
可以使用store in (test,PERFSTAT)放在interval语句后面,如:INTERVAL(10) store in (test,PERFSTAT) --则会均匀地放在预设的表空间里面(  PARTITION D1 VALUES LESS THAN (10),  PARTITION D2 VALUES LESS THAN (20),  PARTITION D3 VALUES LESS THAN (30)
)
复制



Hash 分区:

drop table product;CREATE TABLE product
(id     NUMBER(5), name   VARCHAR2(30)) 
PARTITION BY HASH(id) PARTITIONS 16 STORE IN (ts1,ts2,ts3,ts4);insert into product values(10,0);insert into product values(11,1);insert into product values(22,2);insert into product values(33,3);insert into product values(44,4);insert into product values(55,5);insert into product values(66,6);insert into product values(77,7);insert into product values(88,8);insert into product values(99,9);insert into product values(10,10);insert into product values(11,11);commit;select segment_name,partition_name,tablespace_name from dba_segments where owner='TEST' and segment_name='PRODUCT';select * from product partition(SYS_P67);select * from product partition(SYS_P68);select * from product partition(SYS_P66);
复制



列分区: 

CREATE TABLE DEPT_LIST (
	DeptNo  NUMBER(2),
	Dname   VARCHAR2(14), 
	Loc    VARCHAR2(13))PARTITION BY LIST (Loc)
  (PARTITION D_East VALUES ('Shanghai', 'Nanjing'),   PARTITION D_West VALUES ('Chengdu', 'Chongqing'),   PARTITION D_South VALUES ('Guangzhou', 'Fuzhou'),   PARTITION D_North VALUES ('Beijing', 'Shenyang')
);insert into DEPT_LIST values (1,'GZ','Guangzhou');insert into DEPT_LIST values (1,'GZ','GuangZhou');insert into DEPT_LIST values (2,'BJ','Beijing');select * from DEPT_LIST;select * from DEPT_LIST partition(D_South);
复制


原文地址:http://dacoolbaby.iteye.com/blog/1635143



文章转载自东面而视,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论