Oracle分区表技术 (Partitioned Tables)

自Oracle 8(1997年左右)就引入了分区表&分区索引(Partitioned Tables & Indexes)的概念来调整大表和大索引,提升性能,提升运维管理的能力。分区表和分区索引机制是海量数据库管理(Very Large Databases ,即VLDB) 中一个重要的提升性能的机制。

Oracle分区技术的历史

Oracle 8引入了分区的概念,后续的每个版本都有对分区机制优化和改进。

使用分区表/索引的好处

  • 性能提升: Select语句只检索本分区的记录,减少了记录总数,可有效的提升了查询性能。另外,可以通过表空间将分区映射到不同的物理磁盘上,分散设备IO,提升性能;
  • 分区运维:可以针对不同分区,管理数据的加载、索引的创建(以及重建)、数据备份与恢复。因为可针对分区的管理,所以可以有效的降低了分区间的干扰、影响。

更多分区的好处可以阅读Oracle Partitioning这篇文章,写的更加详细些。

什么时候使用分区表

什么时候使用分区表,并没有一个精确的界限,Oracle只有一些通用的建议,具体使用需要自行评估:

  • Tables greater than 2GB should always be considered for partitioning.
  • Tables containing historical data, in which new data is added into the newest partition. A typical example is a historical table where only the current month‘s data is updatable and the other 11 months are read only.

Tip: 计算表大小的SQL
SELECT B.OWNER,
       B.TABLESPACE_NAME,
       B.TABLE_NAME,
       ROUND (SUM (BYTES) / 1024 / 1024 / 1024, 6) GIGS
FROM   SYS.DBA_EXTENTS A, SYS.DBA_TABLES B
WHERE ((B.TABLESPACE_NAME = A.TABLESPACE_NAME)
                AND (B.OWNER = UPPER (‘&OWNER‘)) AND (B.TABLE_NAME = ‘&TABLE‘))
GROUP BY B.OWNER, B.TABLESPACE_NAME, B.TABLE_NAME;

分区表的索引可以是分区索引,也可以是非分区索引(普通索引)。

如何分区(分区的方法)

Partition Key

分区表/索引是以一个或多个字段为依据来决定记录存储在哪个分区,被选用的分区字段称为Partition Key,Oracle会根据Partition Key自动找到对应的分区中做insert, update, delete操作。

分区的方法

Oracle提供了6种分区方法:



Partitioning Method


Brief Description


Range Partitioning


Used when there are logical ranges of data. Possible usage: dates, part numbers, and serial numbers


Hash Partitioning


Used to spread data evenly over partitions. Possible usage: data has no logical groupings


List Partitioning


Used to list together unrelated data into partitions. Possible usage: a number of states list partitioned into a region


Composite Range-Hash Partitioning


Used to range partition first, then spreads data into hash partitions. Possible usage: range partition by date of birth then hash partition by name; store the results into the hash partitions


Composite Range-List Partitioning


Used to range partition first, then spreads data into list partitions.Possible usage: range partition by date of birth then list partition by state, then store the results into the list partitions

Examples

Range Partitioning Example

--创建表空间

create tablespace invoices_2010 datafile ‘e:\app\TianPan\oradata\tbs01.dbf‘ size 50m;
create tablespace invoices_2011 datafile ‘e:\app\TianPan\oradata\tbs02.dbf‘ size 50m;
create tablespace invoices_2012 datafile ‘e:\app\TianPan\oradata\tbs03.dbf‘ size 50m;
create tablespace invoices_2013 datafile ‘e:\app\TianPan\oradata\tbs04.dbf‘ size 50m;
--创建Range类型的分区表
CREATE TABLE Invoices(
    Invoice_NO VARCHAR2(10),
    Invoice_Creation_Day DATE,
    Invoice_Data VARCHAR2(30)
)
PARTITION BY RANGE(Invoice_Creation_Day)
(  
PARTITION part01 VALUES LESS THAN(TO_DATE(‘2010-12-31 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)) TABLESPACE invoices_2010,
PARTITION part02 VALUES LESS THAN(TO_DATE(‘2011-12-31 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)) TABLESPACE invoices_2011,
PARTITION part03 VALUES LESS THAN(TO_DATE(‘2012-12-31 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘)) TABLESPACE invoices_2012,
PARTITION part04 VALUES LESS THAN (MAXVALUE) TABLESPACE invoices_2013
);
--按年插入测试数据
insert into Invoices values(‘1‘,TO_DATE(‘2010-06-10 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘), ‘2010 invoice‘);
insert into Invoices values(‘2‘,TO_DATE(‘2011-07-20 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘), ‘2011 invoice‘);
insert into Invoices values(‘3‘,TO_DATE(‘2012-08-25 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘), ‘2012 invoice‘);
insert into Invoices values(‘4‘,TO_DATE(‘2013-08-25 00:00:00‘,‘yyyy-mm-dd hh24:mi:ss‘), ‘2013 invoice‘);
commit;
--查询确认,是不是数据被存储到正确的分区

SQL> select * from Invoices;
INVOICE_NO INVOICE_CREATION_DAY INVOICE_DATA
---------- -------------------- ------------------------------
1          2010/6/10            2010 invoice
2          2011/7/20            2011 invoice
3          2012/8/25            2012 invoice
4          2013/8/25            2013 invoice

SQL> select * from Invoices partition(part01);
INVOICE_NO INVOICE_CREATION_DAY INVOICE_DATA
---------- -------------------- ------------------------------
1          2010/6/10            2010 invoice

SQL> select * from Invoices partition(part02);
INVOICE_NO INVOICE_CREATION_DAY INVOICE_DATA
---------- -------------------- ------------------------------
2          2011/7/20            2011 invoice

List Partitioning Example

--创建表空间
create tablespace TS01 datafile ‘e:\app\TianPan\oradata\list\tbs01.dbf‘ size 50m;
create tablespace TS02 datafile ‘e:\app\TianPan\oradata\list\tbs02.dbf‘ size 50m;
create tablespace TS03 datafile ‘e:\app\TianPan\oradata\list\tbs03.dbf‘ size 50m;
create tablespace TS04 datafile ‘e:\app\TianPan\oradata\list\tbs04.dbf‘ size 50m;
create tablespace TS05 datafile ‘e:\app\TianPan\oradata\list\tbs05.dbf‘ size 50m;
--把不同的州划分到不同的分区(表空间)中
CREATE TABLE PARTITION_BY_LIST
(DEPTID            NUMBER,
DEPTNAME        VARCHAR2(15),
STATE            VARCHAR2(2) ,
CONSTRAINT PARTITION_BY_LIST_PK PRIMARY KEY (DEPTID))
PARTITION BY LIST (STATE)
(PARTITION DEPTS_IN_NORTH VALUES (‘AK‘)                    TABLESPACE TS01,
PARTITION DEPTS_IN_EAST  VALUES (‘NY‘, ‘NJ‘, ‘VA‘, ‘CT‘)  TABLESPACE TS02,
PARTITION DEPTS_IN_SOUTH VALUES (‘TX‘, ‘MS‘, ‘GA‘, ‘KY‘)  TABLESPACE TS03,
PARTITION DEPTS_IN_WEST  VALUES (‘CA‘, ‘AZ‘, ‘OR‘, ‘NV‘)  TABLESPACE TS04,
PARTITION DEPTS_WITH_NO_REGION VALUES (DEFAULT)           TABLESPACE TS05)
ENABLE ROW MOVEMENT;
--检查分区是否创建正确
SELECT TABLE_NAME, PARTITION_NAME, HIGH_VALUE, TABLESPACE_NAME
FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = ‘PARTITION_BY_LIST‘
ORDER BY TABLESPACE_NAME;
--插入测试数据
INSERT INTO PARTITION_BY_LIST (DEPTID, DEPTNAME, STATE) VALUES (1,‘ANCHORAGE‘  , ‘AK‘);
INSERT INTO PARTITION_BY_LIST (DEPTID, DEPTNAME, STATE) VALUES (2,‘NEW YORK‘   , ‘NY‘);
INSERT INTO PARTITION_BY_LIST (DEPTID, DEPTNAME, STATE) VALUES (3,‘DALLAS‘     , ‘TX‘);
INSERT INTO PARTITION_BY_LIST (DEPTID, DEPTNAME, STATE) VALUES (4,‘LOS ANGELES‘, ‘CA‘);
INSERT INTO PARTITION_BY_LIST (DEPTID, DEPTNAME, STATE) VALUES (5,‘WAIKIKI‘    , ‘HI‘);
COMMIT;
--查询确认,是不是数据被存储到正确的分区
SQL> select * from PARTITION_BY_LIST;
    DEPTID DEPTNAME        STATE
---------- --------------- -----
         1 ANCHORAGE       AK
         2 NEW YORK        NY
         3 DALLAS          TX
         4 LOS ANGELES     CA
         5 WAIKIKI         HI

SQL> select * from PARTITION_BY_LIST partition (depts_in_north);
    DEPTID DEPTNAME        STATE
---------- --------------- -----
         1 ANCHORAGE       AK

SQL> select * from PARTITION_BY_LIST partition (depts_in_east);
    DEPTID DEPTNAME        STATE
---------- --------------- -----
         2 NEW YORK        NY

SQL> select * from PARTITION_BY_LIST partition (depts_with_no_region);
    DEPTID DEPTNAME        STATE
---------- --------------- -----
         5 WAIKIKI         HI

Useful Query

[Query]如何知道一个表或索引是否有被分区

SELECT * FROM dba_part_tables;
SELECT * FROM dba_part_indexes;

[Query]如何列出分区表或索引的分区对象

SELECT * FROM dba_tab_partitions WHERE table_name = ‘<table_name>‘;
SELECT * FROM dba_ind_partitions WHERE index_name = ‘<index_name>‘;

分区表的数据导出导入

全表导出

C:\Users\TianPan>exp system/[email protected] file=c:\test.dmp tables=Invoices

Export: Release 11.2.0.1.0 - Production on Fri Jan 9 16:18:05 2015

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set

About to export specified tables via Conventional Path ...
. . exporting table                       INVOICES
. . exporting partition                         PART01          1 rows exported
. . exporting partition                         PART02          1 rows exported
. . exporting partition                         PART03          1 rows exported
. . exporting partition                         PART04          1 rows exported
Export terminated successfully without warnings.

分区表导出

C:\Users\TianPan>exp system/[email protected] file=c:\test_part.dmp tables=Invoices:part03

Export: Release 11.2.0.1.0 - Production on Fri Jan 9 16:21:04 2015

Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in ZHS16GBK character set and AL16UTF16 NCHAR character set

About to export specified tables via Conventional Path ...
. . exporting table                       INVOICES
. . exporting partition                         PART03          1 rows exported
Export terminated successfully without warnings.

全表导入

imp system/[email protected] file=test.dmp full=y


分区表导入

imp system/[email protected]file=test_part.dmp full=y

数据泵expdp/impdp的方式

导出整个表
expdp scott/tiger directory=dmp dumpfile=tb_pt.dmp logfile=tb_pb.log tables=tb_pt parallel=3
导出多个分区
expdp scott/tiger directory=dmp dumpfile=tb_pts.dmp logfile=tb_pt.log tables=(tb_pt:sal_16,tb_pt:sal_other) parallel=2

导入单个分区
impdp scott/tiger directory=dmp dumpfile=tb_pts.dmp logfile=tb_pt_imp.log tables=tb_pt:sal_other skip_unusable_indexes=y table_exists_action=replace
导入整个表
impdp scott/tiger directory=dmp dumpfile=tb_pt.dmp logfile=tb_pt_fullimp.log tables=tb_pt skip_unusable_indexes=y table_exists_action=replace

参考:

Partitioned Tables And Indexes

Partitioning an Oracle table Tips

Oracle Partitioned Tables & Indexes

Oracle® Database VLDB and Partitioning Guide

All about Partitions

Oracle Partitioning

Partitioning

Partitioning FAQ

New Whitepaper: Database Partitioning for the E-Business Suite

Using Database Partitioning with the E-Business Suite

Oracle Metalink Note: 554539.1 - Using Database Partitioning with Oracle E-Business Suite

时间: 2024-08-10 15:09:45

Oracle分区表技术 (Partitioned Tables)的相关文章

深入学习Oracle分区表及分区索引

关于分区表和分区索引(About Partitioned Tables and Indexes)对于10gR2而言,基本上可以分成几类: •       Range(范围)分区 •       Hash(哈希)分区 •       List(列表)分区 •       以及组合分区:Range-Hash,Range-List. 对于表而言(常规意义上的堆组织表),上述分区形式都可以应用(甚至可以对某个分区指定compress属性),只不过分区依赖列不能是lob,long之类数据类型,每个表的分区

oracle 分区表

一. 分区表理论知识 Oracle提供了分区技术以支持VLDB(Very Large DataBase).分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明. Oracle的分区表可以包括多个分区,每个分区都是一个独立的段(SEGMENT),可以存放到不同的表空间中.查询时可以通过查询表来访问各个分区中的数据,也可以通过在查询时直接指定分区的方法来进行查询. When to Partition a Table什么时候需要分区表,官网的2个建议如下: (1)Tabl

Oracle 分区表的新增、修改、删除、合并。普通表转分区表方法

一. 分区表理论知识 Oracle提供了分区技术以支持VLDB(Very Large DataBase).分区表通过对分区列的判断,把分区列不同的记录,放到不同的分区中.分区完全对应用透明.Oracle的分区表可以包括多个分区,每个分区都是一个独立的段(SEGMENT),可以存放到不同的表空间中.查询时可以通过查询表来访问各个分区中的数据,也可以通过在查询时直接指定分区的方法来进行查询. When to Partition a Table什么时候需要分区表,官网的2个建议如下: Tables g

我对oracle分区技术的学习

分区技术纷繁复杂,从11g以后有更多的分区方式,但是在考虑使用分区之前要想好是否需要历史数据清理. 比如罗敏的<品悟性能优化>书中P138那个案例.如果使用命名方式分区,那么以后清理历史数据无法根据分区特性进行清理,原因在于可能同一个名字的分区中包含好几年的数据. Oracle 分区表简单而言就是将一张大表按一定规则分为物理上的很多小表,而逻辑上仍然维持为一张大表.对应用是透明的. 分区索引分为以下几种情况 ·        表和索引都不分区 ·        表分区,索引没分区 ·     

转:深入学习Oracle分区表及分区索引

转自:http://database.ctocio.com.cn/tips/286/8104286.shtml 关于分区表和分区索引(About Partitioned Tables and Indexes)对于10gR2而言,基本上可以分成几类: Range(范围)分区 Hash(哈希)分区 List(列表)分区 以及组合分区:Range-Hash,Range-List. 对于表而言(常规意义上的堆组织表),上述分区形式都可以应用(甚至可以对某个分区指定compress属性),只不过分区依赖列

oracle分区表按时间自动创建

表分区是一种思想,分区表示一种技术实现.当表的大小过G的时候可以考虑进行表分区,提高查询效率,均衡IO.oracle分区表是oracle数据库提供的一种表分区的实现形式.表进行分区后,逻辑上仍然是一张表,原来的查询SQL同样生效,同时可以采用使用分区查询来优化SQL查询效率,不至于每次都扫描整个表. 根据年: INTERVAL(NUMTOYMINTERVAL(1,'YEAR'))根据月: INTERVAL(NUMTOYMINTERVAL(1,'MONTH'))根据天: INTERVAL(NUMT

oracle分区表和分区索引概述

㈠ 分区表技术概述            ⑴ Range 分区            ① 例子                  create table t         (...列定义...)         partition by range (week_num)         (partition p1 values less than (4)tablespace data0,           partition p2 values less than (5)tablespac

【三思笔记】 全面学习Oracle分区表及分区索引

[三思笔记]全面学习Oracle分区表及分区索引 2008-04-15 关于分区表和分区索引(About PartitionedTables and Indexes) 对于 10gR2 而言,基本上可以分成几类: v  Range(范围)分区 v  Hash(哈希)分区 v  List(列表)分区 v  以及组合分区:Range-Hash,Range-List. 对于表而言(常规意义上的堆组织表),上述分区形式都可以应用(甚至可以对某个分区指定 compress 属性),只不过分区依赖列不能是

Oracle索引技术研究

Oracle索引类型 B树索引 特定类型索引 确定索引列 主键和唯一键值列的索引 外键索引 其他合适的索引列 B树索引 B树索引算法 B树是指B-tree(Balanced Tree),B树的存在是为了存储设备而设计的一种多分叉的树.B树中 每个节点至多含有m个子节点 每个非叶子节点除了根节点之外至少含有⌈m/2⌉个子节点 根节点至少含有2个子节点否则它是叶子节点 有k个子节点的非叶子节点包含k-1个键值 所有叶子节点出现在同一层 其中,m称为该B树的阶,一个3阶B树的节点的排列如同 [指针]键