Oracle分区技术

Oracle分区技术——简化数据库的日常管理维护工作

分区技术可以用来做什么呢?Oracle数据库都使用分区功能来提高查询的性能,并且简化数据库的日常管理维护工作。分区技术可以改善程序的性能、可管理性和可用性,是数据库管理中一个非常关键的技术。

       分区技术的优点,具体如下:

  1. 独立管理分区,减少维护工作量。
  2. 增强数据库可用性。例如:表的一个或几个分区由于系统故障而不能使用,而表其余的分区仍然可以使用;如果系统故障只影响表的一部分分区,那么,只有这部分分区需要修复,这就比修复整个大表耗费的时间少许多。
  3. 均衡I/O,通过把表的不同分区分配到不同的磁盘来平衡I/O从而改善性能。
  4. 分区对用户保持透明,最终用户感觉不到分区的存在。
  5. 提高查询速度:对大表的查询、增加、修改等操作可以分解到表的不同分区中来并行执行,这样就可以加快运行速度,在数据仓库的TP查询特别有用。

Oracle11g相对于其他相对于其它低级版本的Oracle在分区方面增加了很多功能,具体如下:

    • 引入扩展的分区功能;
    • Interval分区;
    • 外键分区;
    • 虚拟列分区;
    • 引入了分区建议器。

 

1.创建分区

1.1范围分区

范围分区的关键是RANGE,创建分区后,可以根据分区键值指定的范围进行分布,当数据在范围内均匀分布时性能最好。当表需要创建分区的时候,首先要考虑百年的数据是否符合范围分区的分区方式。其次要考虑数据值得取值范围;最后就是一个边界值问题。举例来说,一年我们可以平均分为12个月,也可以分4个季度。这需要我们根据数据去决定的分区策略。假设某商品零售表一年的数据。我们就可以按照日期将为其创建4个分区。代码如下:

create table ware_retail_part --创建一个描述商品零售的数据表
(
  id integer primary key,--销售编号
  retail_date date,--销售日期
  ware_name varchar2(50)--商品名称
)
partition by range(retail_date)
(
  --第一个季度为part_01分区
  partition part_01 values less than(to_date('2017-04-01','yyyy-mm-dd')) tablespace TBSP_1,
  --第二个季度为part_02分区
  partition part_02 values less than(to_date('2017-07-01','yyyy-mm-dd')) tablespace TBSP_1,
  --第三个季度为part_03分区
  partition part_03 values less than(to_date('2017-10-01','yyyy-mm-dd')) tablespace TBSP_2,
  --第四个季度为part_04分区
  partition part_04 values less than(to_date('2018-01-01','yyyy-mm-dd')) tablespace TBSP_2 
);

当数据量庞大的时候,范围分区能够极大限度的增快查询速度,使用分区表查询如下:
SQL>select * from ware_retail_part partition(part_01);
--使用分区表查询分区01,前提是有分区表有数据

 

1.2散列分区

散列分区也叫HASH分区。是在列的取值难以确定的情况下采用的分区方法。以下情况可以才用HASH分区:

HASH分区可以有HASH键来分布;

DBA无法获知具体的数据值;

数据的分布由oracle处理;

每个分区都有自己的表空间。  

创建散列分区的方法与上述范围分区的一样,注意关键值hash即可。

        

1.3列表分区

列表分区关键字是list,如果数据表中某列的值是可以枚举的,那么就可以考虑使用列表分区。譬如学生表,学生的所在省份进行分类。    

create table students --创建一个学生表
(
  id integer primary key,--学号
  name varchar2(50),--学生名字
  province varchar2(20)--省份
)
partition by list
(
	Partition 分区名 values(‘省份名’),
Partition 分区名 values(‘省份名’),
Partition 分区名 values(‘省份名’)
);

1.4组合分区

如果前几个都熟悉了的话,组合分区其实就是两种数据分区的方法可以结合为一个分区方法。

2.分区策略

表分区策略是非常重要的,我们需要非常了解数据的类型,同时也要考虑数据量的大小,其次需要为每个分区创建相应的表空间。DBA做表分区是为了提高数据库的性能。所以多方面的考虑是非常严谨的,这就是我们的分区策略。

2.1识别存储大表

简单来说数据量存储比较大的就是大表,其实需要系统架构师来判定那些表属于大表,对于我们公司来说上百G的才是大表。如果需要在目前运行的系统上进行表数据分析,那么建议采用analyze table语句进行分析。对于那些正在进行需求分析的表,只能做大约的估计了。

2.2表如何分区

表一般可以按时间分区,比如月份,年,季度。常规的分区可以通过时间去分类,但是我们需要了解表结构以及表的数据类型。才能针对性的活用分区技术。

2.3分区表空间的规划

分区方法确定后,就要手动创建分区表空间。表空间的大小需要比估算值要大。另外还需要考虑增长。DBA在创建表的时候会设置表空间自动增长,其实优秀的DBA都不会这么设置的。会根据多种情况和经验去分配空间。

                         

DSS queries on very large tables present special performance problems. An ad-hoc query that requires a table scan may take a long time, because it must inspect every row in the table; there is no way to identify and skip subsets of irrelevant rows. The problem is particularly important for historical tables, for which many queries concentrate access on rows that were generated recently. Partitions help solve this problem. An ad-hoc query that requires only rows that correspond to a single partition (or range of partitions) can be executed using a partition scan rather than a table scan. For example, a query that requests data generated in the month of October 1994 can scan just the rows stored in the October 1994 partition, rather than rows generated over years of activity. This improves response time. It may also substantially reduce the temporary disk space requirement for queries that require sorts. Load balancing Many DBAs want to be able to control how data is spread across physical devices. It is not sufficient to allow a table’s data to reside on multiple disks. The DBA must be able to specify where subsets of a table go, so that he can balance I/O utilization. With this level of control, the DBA can accommodate the special needs of applications requiring fast response time by reducing disk contention and utilizing faster devices. On the other hand data, that is accessed infrequently, such as old historical data, can be moved to slow disks or stored in subsystems that support a storage hierarchy. Partitioning satisfies this requirement by allowing partitions of a table or index to be stored in different tablespaces.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值