本文内容主要参考: 老盖的《循序渐进 Oracle数据库管理,优化与备份恢复》
以及博文 http://jolly10.itpub.net/post/7268/472662
1. 本地管理表空间的区大小分配方式
表空间的管理方式主要有:本地管理和数据字典管理。本地管理采用在位图记录区的分配情况,而数据字典管理的方式采用字典表记录区的分配情况,字典管理方式 从10g开始渐渐被淘汰。区作为基本分配单位分配给段时,其大小也有区别的。
- create tablespace lmt_bmb
- datafile 'lmt_bmb' size 100M autoextend off
- logging
- permanent
- extent management local uniform size 1M --uniform方式,区大小固定为1M
- blocksize 8k
- segment space management manual --段管理方式确认后,将无法修改,之能重建
- flashback on;
-
-
- create tablespace lmt_bmb_auto
- datafile 'lmt_bmb_auto.dbf' size 100M autoextend off
- logging permanent
- extent management local autoallocate --autoallocate方式,区大小又系统自动分配
- blocksize 8k
- segment space management manual
- flashback on;
create tablespace lmt_bmb datafile 'lmt_bmb' size 100M autoextend off logging permanent extent management local uniform size 1M --uniform方式,区大小固定为1M blocksize 8k segment space management manual --段管理方式确认后,将无法修改,之能重建 flashback on; create tablespace lmt_bmb_auto datafile 'lmt_bmb_auto.dbf' size 100M autoextend off logging permanent extent management local autoallocate --autoallocate方式,区大小又系统自动分配 blocksize 8k segment space management manual flashback on;
uniform方式按统一的区大小分配给段,而autoallocate方式会逐渐调整分配给段的大小。通过以下语句可以查询表空间的情况:
- SQL> select tablespace_name, extent_management, allocation_type
- 2 from dba_tablespaces;
-
- TABLESPACE_NAME EXTENT_MANAGEMENT ALLOCATION_TYPE
- ------------------------------ ----------------- ---------------
- SYSTEM LOCAL SYSTEM
- SYSAUX LOCAL SYSTEM
- UNDOTBS1 LOCAL SYSTEM
- TEMP LOCAL UNIFORM
- USERS LOCAL SYSTEM
- ADDDATA01 LOCAL SYSTEM
- ADDDATA02 LOCAL SYSTEM
- ADDINDX01 LOCAL SYSTEM
- ADDINDX02 LOCAL SYSTEM
- ADDTEMP01 LOCAL UNIFORM
- UNDOTBS001 LOCAL SYSTEM
- LMTS LOCAL SYSTEM
- LMT_BMB LOCAL UNIFORM
- LMT_BMB_AUTO LOCAL SYSTEM
SQL> select tablespace_name, extent_management, allocation_type 2 from dba_tablespaces; TABLESPACE_NAME EXTENT_MANAGEMENT ALLOCATION_TYPE ------------------------------ ----------------- --------------- SYSTEM LOCAL SYSTEM SYSAUX LOCAL SYSTEM UNDOTBS1 LOCAL SYSTEM TEMP LOCAL UNIFORM USERS LOCAL SYSTEM ADDDATA01 LOCAL SYSTEM ADDDATA02 LOCAL SYSTEM ADDINDX01 LOCAL SYSTEM ADDINDX02 LOCAL SYSTEM ADDTEMP01 LOCAL UNIFORM UNDOTBS001 LOCAL SYSTEM LMTS LOCAL SYSTEM LMT_BMB LOCAL UNIFORM LMT_BMB_AUTO LOCAL SYSTEM
system表示autoallocate方式,后续通过dump数据文件头来了解区的位图信息,以及区分配方式。
2. Uniform方式以及数据文件头的位图信息
- SQL> create table test tablespace lmt_bmb as select * from dba_users where 1=0;
- Table created
- SQL> select tablespace_name, extent_id, block_id, blocks from dba_extents where tablespace_name = 'LMT_BMB';
- TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS
- ------------------------------ ---------- ---------- ----------
- LMT_BMB 0 9 128
SQL> create table test tablespace lmt_bmb as select * from dba_users where 1=0; Table created SQL> select tablespace_name, extent_id, block_id, blocks from dba_extents where tablespace_name = 'LMT_BMB'; TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS ------------------------------ ---------- ---------- ---------- LMT_BMB 0 9 128Test表创建时是从block_id为9的开始的,前8个block都被系统保留了.其中数据块1和2用于记录数据文件头的信息,
数据块3~8用于记录区间的位图信息. 通过dump前3个块查看区分配的问题信息.
- SQL> select file_name, file_id, bytes/1024/1024 from dba_data_files where tablespace_name = 'LMT_BMB';
-
- FILE_NAME FILE_ID BYTES/1024/1024
- -------------------------------------------------------------------------------- ---------- ---------------
- F:\PROGRAM_FILES\ORACLE\ADMINISTRATOR\PRODUCT\11.1.0\DB_1\DATABASE\LMT_BMB.DBF 40 100
-
- SQL> alter system dump datafile 40 block min 1 block max 3;
- System altered
- SQL> select
- 2 d.value || '/' || lower (rtrim (i.instance, chr (0))) || '_ora_' || p.spid || '.trc' trace_file_name
- 3 from (
- 4 select p.spid
- 5 from v$mystat m, v$session s, v$process p
- 6 where m.statistic# = 1 and s.sid = m.sid and p.addr = s.paddr
- 7 ) p ,
- 8 (
- 9 select t.instance
- 10 from v$thread t, v$parameter v
- 11 where v.name = 'thread'
- 12 and (v.value = 0 or t.thread# = to_number (v.value))
- 13 ) i,
- 14 (
- 15 select value
- 16 from v$parameter
- 17 where name = 'user_dump_dest') d;
-
- TRACE_FILE_NAME
- --------------------------------------------------------------------------------
- f:\program_files\oracle\administrator\diag\rdbms\ucs\ucs\trace/ucs_ora_7052.trc
-
- ######### 分析trc文件
- File Space Bitmap Block:
- BitMap Control:
- RelFno: 40, BeginBlock: 9, Flag: 0, First: 1, Free: 63487
- 0100000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
SQL> select file_name, file_id, bytes/1024/1024 from dba_data_files where tablespace_name = 'LMT_BMB'; FILE_NAME FILE_ID BYTES/1024/1024 -------------------------------------------------------------------------------- ---------- --------------- F:\PROGRAM_FILES\ORACLE\ADMINISTRATOR\PRODUCT\11.1.0\DB_1\DATABASE\LMT_BMB.DBF 40 100 SQL> alter system dump datafile 40 block min 1 block max 3; System altered SQL> select 2 d.value || '/' || lower (rtrim (i.instance, chr (0))) || '_ora_' || p.spid || '.trc' trace_file_name 3 from ( 4 select p.spid 5 from v$mystat m, v$session s, v$process p 6 where m.statistic# = 1 and s.sid = m.sid and p.addr = s.paddr 7 ) p , 8 ( 9 select t.instance 10 from v$thread t, v$parameter v 11 where v.name = 'thread' 12 and (v.value = 0 or t.thread# = to_number (v.value)) 13 ) i, 14 ( 15 select value 16 from v$parameter 17 where name = 'user_dump_dest') d; TRACE_FILE_NAME -------------------------------------------------------------------------------- f:\program_files\oracle\administrator\diag\rdbms\ucs\ucs\trace/ucs_ora_7052.trc ######### 分析trc文件 File Space Bitmap Block: BitMap Control: RelFno: 40, BeginBlock: 9, Flag: 0, First: 1, Free: 63487 0100000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 从第3个块开始,记录了位图的信息.
01是16进制转换为二进制就是0000 0001,经过高位低位的字节交换得出的结果是1000 0000表示分配了一个区间。
- SQL> alter table test allocate extent;
- Table altered
- SQL> alter system dump datafile 40 block min 1 block max 3;
- System altered
- SQL>
SQL> alter table test allocate extent; Table altered SQL> alter system dump datafile 40 block min 1 block max 3; System altered SQL> 分析trc信息如下:
- BitMap Control:
- RelFno: 40, BeginBlock: 9, Flag: 0, First: 2, Free: 63486
- 0300000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
BitMap Control: RelFno: 40, BeginBlock: 9, Flag: 0, First: 2, Free: 63486 0300000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000对第一个03转换后就是0000 0011, 进行高地位反转1100 0000,表示分配了2个区间.通过查询也可以看出这种情况:
- SQL> select segment_name, tablespace_name, extent_id, block_id, blocks
- 1 from dba_extents
- 2 where tablespace_name = 'LMT_BMB';
-
- SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS
- ------------------------- ------------------------------ ---------- ---------- ----------
- TEST LMT_BMB 0 9 128
- TEST LMT_BMB 1 137 128
SQL> select segment_name, tablespace_name, extent_id, block_id, blocks 1 from dba_extents 2 where tablespace_name = 'LMT_BMB'; SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS ------------------------- ------------------------------ ---------- ---------- ---------- TEST LMT_BMB 0 9 128 TEST LMT_BMB 1 137 128
注意这里分配的2个区都是128 * 8K = 1M。
- SQL> create table test2 tablespace lmt_bmb as select * from dba_users where 1=0;
- Table created
-
- SQL> select segment_name, tablespace_name, extent_id, block_id, blocks
- 1 from dba_extents
- 2 where tablespace_name = 'LMT_BMB';
-
- SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS
- ----------------- ------------------------------ ---------- ---------- ----------
- TEST LMT_BMB 0 9 128
- TEST LMT_BMB 1 137 128
- TEST2 LMT_BMB 0 265 128
-
- SQL> alter table test2 allocate extent;
- Table altered
- SQL> alter table test2 allocate extent;
- Table altered
- SQL> alter table test2 allocate extent;
- Table altered
-
- SQL> select segment_name, tablespace_name, extent_id, block_id, blocks
- 1 from dba_extents
- 2 where tablespace_name = 'LMT_BMB';
-
- SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS
- ----------------- ------------------------------ ---------- ---------- ----------
- TEST LMT_BMB 0 9 128
- TEST LMT_BMB 1 137 128
- TEST2 LMT_BMB 0 265 128
- TEST2 LMT_BMB 1 393 128
- TEST2 LMT_BMB 2 521 128
- TEST2 LMT_BMB 3 649 128
-
- 6 rows selected
-
- SQL> alter system dump datafile 40 block min 1 block max 3;
- System altered
-
- ######### 分析trace
- BitMap Control:
- RelFno: 40, BeginBlock: 9, Flag: 0, First: 6, Free: 63482
- 3F00000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
-
- 3F转化之后为0011 1111 反转之后1111 0011, 所以是6个区.
SQL> create table test2 tablespace lmt_bmb as select * from dba_users where 1=0; Table created SQL> select segment_name, tablespace_name, extent_id, block_id, blocks 1 from dba_extents 2 where tablespace_name = 'LMT_BMB'; SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS ----------------- ------------------------------ ---------- ---------- ---------- TEST LMT_BMB 0 9 128 TEST LMT_BMB 1 137 128 TEST2 LMT_BMB 0 265 128 SQL> alter table test2 allocate extent; Table altered SQL> alter table test2 allocate extent; Table altered SQL> alter table test2 allocate extent; Table altered SQL> select segment_name, tablespace_name, extent_id, block_id, blocks 1 from dba_extents 2 where tablespace_name = 'LMT_BMB'; SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS ----------------- ------------------------------ ---------- ---------- ---------- TEST LMT_BMB 0 9 128 TEST LMT_BMB 1 137 128 TEST2 LMT_BMB 0 265 128 TEST2 LMT_BMB 1 393 128 TEST2 LMT_BMB 2 521 128 TEST2 LMT_BMB 3 649 128 6 rows selected SQL> alter system dump datafile 40 block min 1 block max 3; System altered ######### 分析trace BitMap Control: RelFno: 40, BeginBlock: 9, Flag: 0, First: 6, Free: 63482 3F00000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 3F转化之后为0011 1111 反转之后1111 0011, 所以是6个区.
drop掉test,观察分区和位图信息
- SQL> drop table test purge;
- Table dropped
-
- SQL> select segment_name, tablespace_name, extent_id, block_id, blocks
- 1 from dba_extents where tablespace_name = 'LMT_BMB';
-
- SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS
- --------------- ----------------- ---------- ---------- ----------
- TEST2 LMT_BMB 0 265 128
- TEST2 LMT_BMB 1 393 128
- TEST2 LMT_BMB 2 521 128
- TEST2 LMT_BMB 3 649 128
-
- SQL> alter system dump datafile 40 block min 1 block max 3;
- System altered
- SQL>
-
- ################ 分析dump文件信息
-
- BitMap Control:
- RelFno: 40, BeginBlock: 9, Flag: 0, First: 0, Free: 63484
- 3C00000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
SQL> drop table test purge; Table dropped SQL> select segment_name, tablespace_name, extent_id, block_id, blocks 1 from dba_extents where tablespace_name = 'LMT_BMB'; SEGMENT_NAME TABLESPACE_NAME EXTENT_ID BLOCK_ID BLOCKS --------------- ----------------- ---------- ---------- ---------- TEST2 LMT_BMB 0 265 128 TEST2 LMT_BMB 1 393 128 TEST2 LMT_BMB 2 521 128 TEST2 LMT_BMB 3 649 128 SQL> alter system dump datafile 40 block min 1 block max 3; System altered SQL> ################ 分析dump文件信息 BitMap Control: RelFno: 40, BeginBlock: 9, Flag: 0, First: 0, Free: 63484 3C00000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000
对3C进行分析,得出0011 1100, 高地位为1100 0011, 也就是有4个区被分配,是一致的.
对于10g开始的回收站情况,可以通过以下sql语句进行查询和清理.
- 1)清除的方法如下:
- 1、purge table origenal_tableName;
- purge index origenal_indexName;
- 2、purge recyclebin;
-
- 2)查询垃圾信息,可以用如下SQL语句:
- select t.object_name,t.type ,t.original_name from user_recyclebin t;
-
- 3)删除Table不进入Recycle的方法:
- drop table tableName purge;
1)清除的方法如下: 1、purge table origenal_tableName; purge index origenal_indexName; 2、purge recyclebin; 2)查询垃圾信息,可以用如下SQL语句: select t.object_name,t.type ,t.original_name from user_recyclebin t; 3)删除Table不进入Recycle的方法: drop table tableName purge;
Autoallocate的区大小管理方式的位图信息和uniform的有稍微区别,uniform的位图表示分配了多少个区,而autoallocate方式则表示大小情况,每个表示64KB。
3.Autoallocate方式以及对应位图信息
先创建表,查看分区情况,dump文件头的3个块。
- SQL> select tablespace_name, extent_management, allocation_type
- 2 from dba_tablespaces
- 3 where tablespace_name = 'LMT_BMB_AUTO';
-
- TABLESPACE_NAME EXTENT_MANAGEMENT ALLOCATION_TYPE
- ------------------------------ ----------------- ---------------
- LMT_BMB_AUTO LOCAL SYSTEM
-
- SQL>
-
- --allocate_type = system 表示是系统自动分配extent的大小
-
- SQL> select file_name, file_id, bytes/1024/1024 from dba_data_files
- 1 where tablespace_name = 'LMT_BMB_AUTO';
-
- FILE_NAME FILE_ID BYTES/1024/1024
- -------------------------------------------------------------------------------- ---------- ---------------
- F:\PROGRAM_FILES\ORACLE\ADMINISTRATOR\PRODUCT\11.1.0\DB_1\DATABASE\LMT_BMB_AUTO.dbf 41 100
-
- SQL> create table test4 tablespace lmt_bmb_auto
- 2 as select * from all_objects where 1=0;
-
- Table created
-
- SQL> select tablespace_name, segment_name, file_id, extent_id, block_id, blocks
- 2 from dba_extents
- 3 where tablespace_name='LMT_BMB_AUTO';
-
- TABLESPACE_NAME SEGMENT_NAME FILE_ID EXTENT_ID BLOCK_ID BLOCKS
- ------------------------------ ------------- ---------- ---------- ---------- ----------
- LMT_BMB_AUTO TEST4 41 0 9 8
-
- SQL>
SQL> select tablespace_name, extent_management, allocation_type 2 from dba_tablespaces 3 where tablespace_name = 'LMT_BMB_AUTO'; TABLESPACE_NAME EXTENT_MANAGEMENT ALLOCATION_TYPE ------------------------------ ----------------- --------------- LMT_BMB_AUTO LOCAL SYSTEM SQL> --allocate_type = system 表示是系统自动分配extent的大小 SQL> select file_name, file_id, bytes/1024/1024 from dba_data_files 1 where tablespace_name = 'LMT_BMB_AUTO'; FILE_NAME FILE_ID BYTES/1024/1024 -------------------------------------------------------------------------------- ---------- --------------- F:\PROGRAM_FILES\ORACLE\ADMINISTRATOR\PRODUCT\11.1.0\DB_1\DATABASE\LMT_BMB_AUTO.dbf 41 100 SQL> create table test4 tablespace lmt_bmb_auto 2 as select * from all_objects where 1=0; Table created SQL> select tablespace_name, segment_name, file_id, extent_id, block_id, blocks 2 from dba_extents 3 where tablespace_name='LMT_BMB_AUTO'; TABLESPACE_NAME SEGMENT_NAME FILE_ID EXTENT_ID BLOCK_ID BLOCKS ------------------------------ ------------- ---------- ---------- ---------- ---------- LMT_BMB_AUTO TEST4 41 0 9 8 SQL>
可以看出这个区大小为64KB,来看具体的位图信息
- SQL> alter system dump datafile 41 block min 1 block max 3;
- System altered
- SQL>
-
- ############################# trc的位图信息
- File Space Bitmap Block:
- BitMap Control:
- RelFno: 41, BeginBlock: 9, Flag: 0, First: 1, Free: 63487
- 0100000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
SQL> alter system dump datafile 41 block min 1 block max 3; System altered SQL> ############################# trc的位图信息 File Space Bitmap Block: BitMap Control: RelFno: 41, BeginBlock: 9, Flag: 0, First: 1, Free: 63487 0100000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 000000000000000001转换成二进制是0000 0001,高低位转换后为1000 0000。由于是自动大小分配管理的区间,这里的1并不能代表分配了一个区间,只说代表分配了64K大小,因为区间尺寸可能是64K,1MB,8MB,64M或更大,不过都有一个通用尺寸64KB,所以64KB就是该表空间的一个位标记大小。通过向表中插入数据,可以查看出来。
- SQL> insert into test4 select * from all_objects;
- 68385 rows inserted
-
- SQL> select segment_name, blocks * 8 extent_size_KB, sum(blocks) * 8 total_KB, count(1) total_extents
- 2 from dba_extents
- 3 where segment_name = 'TEST4' and tablespace_name='LMT_BMB_AUTO'
- 4 group by segment_name, blocks;
-
- SEGMENT_NAME EXTENT_SIZE_KB TOTAL_KB TOTAL_EXTENTS
- --------------- -------------- ---------- -------------
- TEST4 1024 7168 7
- TEST4 64 1024 16
-
- SQL>
-
- ############## trc 位图信息
- File Space Bitmap Block:
- BitMap Control:
- RelFno: 41, BeginBlock: 9, Flag: 0, First: 128, Free: 63360
- FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
- 0000000000000000 0000000000000000 0000000000000000 0000000000000000
SQL> insert into test4 select * from all_objects; 68385 rows inserted SQL> select segment_name, blocks * 8 extent_size_KB, sum(blocks) * 8 total_KB, count(1) total_extents 2 from dba_extents 3 where segment_name = 'TEST4' and tablespace_name='LMT_BMB_AUTO' 4 group by segment_name, blocks; SEGMENT_NAME EXTENT_SIZE_KB TOTAL_KB TOTAL_EXTENTS --------------- -------------- ---------- ------------- TEST4 1024 7168 7 TEST4 64 1024 16 SQL> ############## trc 位图信息 File Space Bitmap Block: BitMap Control: RelFno: 41, BeginBlock: 9, Flag: 0, First: 128, Free: 63360 FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 一共23个区,总大小7个1M的区,16个64K的区, 总大小7168 + 1024 = 8192KB.
可以看出exent的大小从64K变为了1M。
看看位图信息. 也就是 1111 1111 一共16组,每位代表64K,总大小: 16 * 8 * 64KB = 8192KB 跟统计的一样.