Oracle 11g新特性 -- 延迟段

11gR2之前的版本中,当创建一张表时,会自动分配段空间,这样做有几个弊端:

1. 初始创建表时就需要分配空间,自然会占用一些时间,如果初始化多张表,这种影响就被放大。

2. 如果很多表开始的一段时间都不需要,那么就会浪费这些空间。

为此,从11gR2开始,有一种新特性,叫延迟段,即延迟分配段空间。简单讲,默认将表(以及索引、LOB)的物理空间分配推迟到第一条记录插入到表中时。即有实际的数据插入表中时,再为每个对象初始化空间分配。其中11.2.0.1不支持分区表 、bitmap join indexes和domain indexes。11.2.0.2版本开始支持分区表。

注:使用特性的前提是COMPATIBLE参数是11.2.0及以上。

 
实验:

1. 首先我们看下11g之前版本对表空间时段空间的分配:
SQL> select version from v$instance;
 VERSION
 -----------------
 10.2.0.4.0

 
SQL> create table tbl_seg(reg_id number, reg_name varchar2(200));
 Table created.


SQL> select count(*) from user_segments where segment_name='TBL_SEG';
   COUNT(*)
 ----------
          1

SQL> select count(*) from user_extents where segment_name='TBL_SEG';
   COUNT(*)
 ----------
          1


SQL> select segment_name, segment_type, bytes, blocks, extents, initial_extent, next_extent, max_extents
   2  from user_segments where segment_name='TBL_SEG';
 SEGMENT_NAME       SEGMENT_TYPE       BYTES     BLOCKS    EXTENTS INITIAL_EXTENT NEXT_EXTENT MAX_EXTENTS
 ----------------------------- ---------- ---------- ---------- -------------- ----------- --------------------- ----------------- ------------------
 TBL_SEG                   TABLE                     65536     8               1            65536                                       2147483645

可以看到,TBL_SEG表创建后,user_segments和user_extents已经有记录了,说明已经为TBL_SEG分配了段和区的空间。1个EXTENTS的大小是64K。


2、接下来看下11g的分配:

 
[oracle@riserver1 ~]$ sqlplus / as sysdba
 SQL*Plus: Release 11.2.0.1.0 Production on Mon Aug 4 07:53:24 2014
 Copyright (c) 1982, 2009, Oracle.  All rights reserved.
 Connected to:
 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
 With the Partitioning, OLAP, Data Mining and Real Application Testing options

 
SQL> select version from v$instance;
 VERSION
 -----------------
 11.2.0.1.0

 
SQL> create table tbl_seg(reg_id number, reg_name varchar2(200));
 Table created.

 SQL> select count(*) from user_segments where segment_name='TBL_SEG';
   COUNT(*)
 ----------
          1

 SQL> select count(*) from user_extents where segment_name='TBL_SEG';
   COUNT(*)
 ----------
          1

 
为什么仍分配了呢?

IOTs and other special tables like clustered tables, global temporary tables, session-specific temporary tables, internal tables, typed tables, AQ tables, external tablesare not supported. Tables owned by SYS,SYSTEM, PUBLIC, OUTLN, and XDB are also excluded.

这里解释了原因,SYS的表是不能使用延迟段的,因此创建时还是立即分配段空间。

那么接下来使用非系统账户建表:

SQL> create user user01 identified by user01;

User created.

 
SQL> grant connect, resource to user01;
 Grant succeeded.

 
[oracle@riserver1 ~]$ sqlplus user01/user01
 SQL*Plus: Release 11.2.0.1.0 Production on Mon Aug 4 08:32:06 2014
 Copyright (c) 1982, 2009, Oracle.  All rights reserved.
 Connected to:
 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
 With the Partitioning, OLAP, Data Mining and Real Application Testing options

 
SQL> show user
 USER is "USER01"


SQL> select username,default_tablespace from user_users;
 USERNAME                       DEFAULT_TABLESPACE
 ------------------------------ ------------------------------
 USER01                         USERS

 
SQL> create table tbl_seg(reg_id number, reg_name varchar2(200));
 Table created.

SQL> select count(*) from user_segments where segment_name='TBL_SEG';
   COUNT(*)
 ----------
          0

SQL> select count(*) from user_extents where segment_name='TBL_SEG';
   COUNT(*)
 ----------
          0

此时可以看到创建表后,段和区是没有分配空间的,那么插入记录:

SQL> insert into tbl_seg values(1, 'BRDSTN');

1 row created.

SQL> select count(*) from user_segments where segment_name='TBL_SEG';

  COUNT(*)
----------
         1

SQL> select count(*) from user_extents where segment_name='TBL_SEG';

  COUNT(*)
----------
         1

可以看到段和区已经分配了空间,user_segments和user_extents表均有了TBL_SEG对应的记录。

3. 禁用延迟段:
可以禁用延迟段,是否使用延迟段是由DEFERRED_SEGMENT_CREATION参数定义的,该参数可以在会话级别修改,如果想彻底删除延迟段,可以在spfile中修改,本次以及下次启动后就会一直生效了,例如:

SQL> alter session set DEFERRED_SEGMENT_CREATION=false;

Session altered.

SQL> create table tbl_seg(reg_id number, reg_name varchar2(200));
Table created.

SQL> select count(*) from user_segments where segment_name='TBL_SEG';
  COUNT(*)
----------
         1

SQL> select count(*) from user_extents where segment_name='TBL_SEG';
  COUNT(*)
----------
         1

4. 使用SEGMENT CREATION子句:
即使禁用了延迟段,还是可以使用SEGMENT CREATION在创建表时指定是否使用延迟段,例如:


SQL> create table tbl_seg(
  2  reg_id number,
  3  reg_name varchar2(200))
  4   segment creation immediate;
Table created.

SQL> select count(*) from user_segments where segment_name='TBL_SEG';
  COUNT(*)
----------
         1

SQL> select count(*) from user_extents where segment_name='TBL_SEG';
  COUNT(*)
----------
         1


这可以看到立即为段和区分配了空间。如果使用SEGMENT CREATION DEFERRED则会使用延迟段的功能。


5. 从USER_TABLES看变化:
11g的USER_TABLES比之前的版本会多一些字段,其中有一项就是SEGMENT_CREATED(VARCHAR2(3)),这样就能知道某个段是否已经分配了空间。

6. 导入导出的影响:

11.2.0.1版本下,如果某张表还未分配段,使用exp到导出时是不会导出未分配段的表,解决方法是可以在导出前使用ALLOCATE EXTENT手工分配段,或者使用数据泵expdp导出未分配段的表。

总结:

这种延迟段的新特性的好处是显而易见的,弊端也很明显,至于是否应该使用,则需要根据实际业务来决定,这也是Oracle提供了禁用延迟段选项的目的。

原文地址:https://www.cnblogs.com/login2012/p/5754925.html