Oracle 11gR2 deferred segment creation 与 exp/imp 说明
一. deferred segment creation 说明
从Oracle 11.2.0.1版本开始,Oracle又提供了一种新的空间分配方法: Create一个非分区表时,这个Table Segment并没有立刻创建, 而是直到有第一行记录插入的时候才去创建这个Segment,这和我们以前的Segment的创建和空间分配方法是不一样.这样的段也被称为延迟段.
从11.2.0.2版本开始,增加了对分区表和LOB字段的支持。并且功能得到增强:
(1)既支持分区对象也支持非分区对象,同时对于分区表,新的segments创建时缺省的extent size为8M,而不再是以前的64K。
(2)对于从11.2.0.2之前版本升级过来的系统,如果有empty tables,可以通过dbms_space_admin.drop_empty_segments 过程清除这些segments.
(3) 11.2.0.2中,truncate 命令得到了一些增强, truncate table中的"DROP ALL STORAGE" 选项可以让你象删除extents一样删除segments.
(4)借助可以dbms_space_admin.materialize_deferred_segments可以实例化tables、partitions、and dependent objects for whichsegment creation was deferred
Deferred segment 的优点:
(1)降低空间的开销:当一次创建成百上千个表时,因为很多表短时间内根本不会用到,所以可以节约大量的磁盘开销
(2)加快应用的部署:因为没有分配Segment,所以建表的时候仅仅是操作数据字典而已,不设计空间的分配,所以效率自然就高了很多。
以上内容From:http://tomszrp.itpub.net/post/11835/510259
注意:
Thisnew feature in not applicable to SYS and the SYSTEM users as the segment to thetable is created along with the table creation.
--该特性不适用SYS 和 SYSTEM 用户
控制deferred segment 的参数是: DEFERRED_SEGMENT_CREATION,默认为true.
官网对这个参数的说明如下:
http://download.oracle.com/docs/cd/E11882_01/server.112/e17110/initparams073.htm#REFRN10307
DEFERRED_SEGMENT_CREATION
Property | Description |
Parameter type | Boolean |
Default value | true |
Modifiable | ALTER SESSION, ALTER SYSTEM |
Range of values | true | false |
Basic | No |
DEFERRED_SEGMENT_CREATIONspecifies the semantics of deferred segment creation. Ifset to true, then segments for tables and their dependent objects (LOBs,indexes) will not be created until the first row is inserted into the table.
--如果deferred_segment_creation 设置为true,那么表的segments和相关的对象(索引,lobs)都会在insert 之后才创建。
Beforecreating a set of tables, if it is known that a significant number of them willnot be populated, then consider setting this parameter to true. This saves disk space and minimizes install time.
--设置该参数的目的是为了减少对磁盘空间的占用和创建时间
二. Exp/imp与deferred segment 说明
在MOS 上有2篇文档对这个问题进行了说明:[ID1178343.1] 和 [ID960216.1]。
当启用deferred segment 之后,如果有空表,在使用exp进行导出时, 会报:EXP-00011: 'Table Name' does not exist。即空表不被导出。 这个问题在11.2.0.2 中已经修复。
Expdp/impdp对deferred segment是支持的,在11gr2及后续的版本中尽量使用expdp/impdp.
如果想在创建表时就分配segment,可以使用如下SQL:
createtable b_tab (id number, text varchar2(10)) segment creation immediate;
也可以通过修改deferred_segment_creation 为false 来禁用这个功能,修改只对以后创建的table 生效。 对于已经存在的table不受影响。
那么对于已经存在的空表,可是使用以下2个命令来手动的分配segment。
SQL>alter table table_name move;
Table altered.
OR
SQL> alter table table_nameallocate extent;
Table altered.
或者直接向空表里insert一条数据。
三. 测试
参考Thomas Zhang 的blog:
http://tomszrp.itpub.net/post/11835/520574
SQL> select * from v$version whererownum=1;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise EditionRelease 11.2.0.1.0 - Production
SQL> show parameterdeferred_segment_creation
NAME TYPE VALUE
----------------------------------------------- ------------------------------
deferred_segment_creation boolean TRUE
SQL> create table t1 as select * fromdba_users;
Table created.
SQL> create table t2 as select * from dba_userswhere 1=2;
Table created.
SQL> create table t3 as select * fromdba_users where 1=2;
Table created.
SQL> select table_name from tabs where table_namein ('T1','T2','T3');
TABLE_NAME
------------------------------
T1
T2
T3
--TABS is a synonym for USER_TABLES.
SQL> select segment_name fromuser_segments where segment_name in ('T1','T2','T3');
SEGMENT_NAME
--------------------------------------------------------------------------------
T1
-- 这里只有T1 表分配了segment
SQL> select table_name
2 from tabs t
3 where not exists (select segment_name from user_segments swhere s.segment_name=t.table_name);
TABLE_NAME
------------------------------
T3
T2
--查看未分配segment的table
--使用exp 导出3张表
C:\Users\Administrator.DavidDai>expicd/icd tables=(t1,t2,t3) file='D:\temp.dmp';
Export: Release 11.2.0.1.0 - Production onWed Jul 13 17:13:22 2011
Copyright (c) 1982, 2009, Oracle and/or itsaffiliates. All rights reserved.
Connected to: Oracle Database 11gEnterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Miningand Real Application Testing options
Export done in ZHS16GBK character set andAL16UTF16 NCHAR character set
About to export specified tables viaConventional Path ...
. . exporting table T1 35 rows exported
EXP-00011: ICD.T2 doesnot exist
EXP-00011: ICD.T3 doesnot exist
Export terminated successfully withwarnings.
--提示我们t2 和 t3表不存在
--手工分配segment。 这里方法很多,insert一条数据,或者使用alter 命令来操作。
--方法1 使用allocateextent,这里我们只对t2 操作
SQL> select 'alter table'||table_name||' allocate extent(size 64k);'
2 from tabs t
3 where not exists (selectsegment_name from user_segments s where s.segment_name=t.table_name);
'ALTERTABLE'||TABLE_NAME||'ALLOCATEEXTENT(SIZE64K);'
---------------------------------------------------------------------
alter table T3 allocate extent(size 64k);
alter table T2 allocate extent(size 64k);
SQL> alter table T2 allocate extent(size64k);
Table altered.
--方法2,使用altertable move
SQL> alter table T3 move;
Table altered.
--确认segment分配情况
SQL> select segment_name fromuser_segments where segment_name in ('T1','T2','T3');
SEGMENT_NAME
--------------------------------------------------------------------------------
T1
T2
T3
--再次exp
C:\Users\Administrator.DavidDai>expicd/icd tables=(t1,t2,t3) file='D:\temp1.dmp';
Export: Release 11.2.0.1.0 - Production onWed Jul 13 17:18:29 2011
Copyright (c) 1982, 2009, Oracle and/or itsaffiliates. All rights reserved.
Connected to: Oracle Database 11gEnterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Miningand Real Application Testing options
Export done in ZHS16GBK character set andAL16UTF16 NCHAR character set
About to export specified tables via ConventionalPath ...
. . exporting table T1 35 rows exported
. . exporting table T2 0 rows exported
. . exporting table T3 0 rows exported
Export terminated successfully withoutwarnings.
在11.2.0.2 中,deferred segment可以使用增加的materialize_deferred_segments和drop_empty_segments 来分配和drop segment。 因为手头没有11.2.0.2的环境,所以这部分测试可以参考Thomas Zhang的blog。
相关语法如下:
--创建Table
SQL> CREATE TABLE t (
2 id NUMBER,
3 c CLOB,
4 CONSTRAINT t_pk PRIMARY KEY(id) USING INDEX LOCAL
5 )
6 SEGMENT CREATION DEFERRED
7 PARTITION BY HASH(id)PARTITIONS 4;
--查看segment
SQL> SELECT segment_name, segment_type,bytes, extents
2 FROM user_segments
3 WHERE segment_name IN('T','T_PK')
4 OR segment_name IN (SELECTsegment_name
5 FROM user_lobs
6 WHEREtable_name = 'T')
7 ORDER BY 1,2;
--用materialize_deferred_segments对该分区表的segment进行实例化
SQL> BEGIN
2 sys.dbms_space_admin.materialize_deferred_segments(
3 schema_name => 'STUDY',
4 table_name => 'T'
5 );
6 END;
7 /
--用drop_empty_segments将空的segment删除
SQL> BEGIN
2 dbms_space_admin.drop_empty_segments(
3 schema_name => 'STUDY',
4 table_name => 'T'
5 );
6 END;
7 /
-------------------------------------------------------------------------------------------------------
Blog: http://blog.csdn.net/tianlesoftware
Email: dvd.dba@gmail.com
DBA1 群:62697716(满); DBA2 群:62697977(满) DBA3 群:62697850(满)
DBA 超级群:63306533(满); DBA4 群: 83829929 DBA5群: 142216823
DBA6 群:158654907 聊天 群:40132017 聊天2群:69087192
--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请