前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >11g的延迟段功能

11g的延迟段功能

作者头像
bisal
发布2019-01-29 11:59:10
4760
发布2019-01-29 11:59:10
举报
文章被收录于专栏:bisal的个人杂货铺

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提供了禁用延迟段选项的目的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014年08月08日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档