Oracle 11gR2的延遲段功能

來源:互聯網
上載者:User

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');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提供了禁用延遲段選項的目的。

Oracle 11g 在RedHat Linux 5.8_x64平台的安裝手冊

Linux-6-64下安裝Oracle 12C筆記

在CentOS 6.4下安裝Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虛擬機器中安裝步驟

Debian 下 安裝 Oracle 11g XE R2

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.