Oracle 11gR2的延迟段功能

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');

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/5ee69fc06d747ef9ce204dc82930a90d.html