Oracle OCP笔记(13)表空间和数据文件

本文介绍了Oracle数据库的存储结构,包括逻辑结构和物理结构,并详细解析了表空间、数据文件及其管理方式。同时提供了查询表空间信息及管理表空间的具体SQL语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oracle OCP笔记(13)表空间和数据文件



1.数据库存储结构
    实例由进程和内存结构组成,所有数据处理都在内存中执行,数据存储发生在磁盘的数据库中。
    数据库包含三类文件: 控制文件,联机重做日志文件和数据文件。数据存储在数据文件中。
    数据库将逻辑存储从物理存储中抽象出来。DBA必须了解逻辑存储和物理存储之间的关系。


    逻辑存储结构: 表空间(tablespace) -< 段(segment) -< 区间(extent) -< 块(block)
    物理存储结构: 数据文件(datafile) -< 操作系统块(block)


    程序设计人员只处理逻辑结构,而让数据库去管理逻辑结构到物理结构的映射。
    从逻辑上讲,数据在段(segment)中存储;从物理上讲,数据在数据文件(datafile)中存储。
    表空间(tablespace)实体消除了段与数据文件之间的多对多关联(由数据库管理,程序设计人员不用关心),一个表空间可能包含多个段,而且可以由多个数据文件组成。这意味着,一个段可以分布在多个数据文件中,而任一数据文件可以包含多个段的全部和一部分。
    段(segment)实体表示存储数据从而需要表空间的任何数据库对象,如:表段、索引段、撤消段。段和数据文件没有直接关系。数据文件可以作为文件系统中的文件存在,也可以作为自动存储管理(ASM)设备上的文件存在。
    块(block)是数据库的I/O单元。数据文件被设置为连续编号的Oracle数据库块。块由数据文件与块编号唯一标识。块大小固定不变。
    在Linux或Windows上,块大小一般是2-16KB,其它系统上可达到32KB,默认块大小由参数db_block_size控制. 一个Oracle块对应一个或多个操作系统块。
    区间(extent)是一个数据文件中一组连续编号的Oracle块。


    每个段将由一个或多个连续编号的区间组成,这些区间可能位于构成表空间的任何或所有数据文件中。
    区间可以根据段的维度或数据文件的维度确定。
    段的维度: 区间根据每个段连续编号,从零开始.
    数据文件的维度: 每个区间在一个文件中,从某个Oracle块编号开头。


    selec * from dba_segments;
    select tablespace_name,file_id,extent_id,block_id,block,butes from dba_extents;
    select tablespace_name,file_name,file_id from dba_data_files;


2.查询存储结构的视图
    控制文件的名称和大小
    select name,block_size*file_size_blks bytes from v$controlfile;
    联机重做日志文件成员的名称和大小
    select member,bytes from v$log join v$logfile using (group#);
    数据文件和临时文件的名称和大小
    select name,bytes from v$datafile;
    select name,bytes from v$tempfile;


3.数据文件的大小限制:
    (1)smallfile表空间的数据文件:
    Rowid包括四部分组成: 对象号+相对文件号+块号+行号(32bit object# + 10bit rfile# + 22bit block# + 16 bit row#),10Bytes.
    select dbms_rowid.rowid_object(rowid) object_id,
           dbms_rowid.rowid_relative_fno(rowid) relative_fno,
           dbms_rowid.rowid_block_number(rowid) block_number,
           dbms_rowid.rowid_row_number(rowid) row_number,           
      from mms.usr_mstr
     where rownum < 10;


    每个文件都包括两个编号,一个是绝对文件编号file_id,另一个是相对文件编号relative_fno。
    select dbms_rowid.rowid_relative_fno(rowid) relative_fno,                     -- 相对文件编号(最大1024)
           dbms_rowid.rowid_to_absolute_fno(rowid,'MMS','USR_MSTR') absolute_fno  -- 绝对文件编号
      from mms.usr_mstr
     where rownum < 10;


    在smallfile表空间的数据文件中,Oracle利用22位进行块地址存储,22位最多只能代表2^22-1=4194303=4M个数据块,如果数据块大小为8k,则数据文件的理论大小上限为32G。如果最大数据块大小为32K,则数据文件的理论大小上限为128G。一个表空间只能有1023个数据文件。
    因此,数据文件大小和db_block_size有关,数据块大小与数据文件最大理论值的对应关系如下:
    2KB       8GB
    4KB      16GB
    8KB      32GB
    16KB     64GB
    32KB    128GB


    (2)bigfile表空间的数据文件
    在bigfile表空间的数据文件中,由于一个表空间只允许有一个数据文件,事情会有所不同。
    select dbms_rowid.rowid_object(rowid) object_id, 
           dbms_rowid.rowid_relative_fno(rowid,'BIGFILE') relative_fno,
           dbms_rowid.rowid_block_number(rowid) block_number,
           dbms_rowid.rowid_row_number(rowid) row_number
      from mms.usr_mstr
     where rownum < 10;


    bigfile表空间只能有一个数据文件,Rowid的文件号就没有意义了,直接就是1024。由于没有relative_fno的问题,这样rowid中就不需要保存relative_fno的最多1024的数值。这样就节省出10位二进制位给数据块定位,相同长度的rowid就可以进行32位二进制长度的数据块寻址。每个数据文件中,最多可以包含2^32-1=4G个数据块。如果数据块大小8K,则数据文件理论大小上限为32TB。如果数据块大小为32K,则数据文件理论大小上限为128TB。
    
*** 数据文件大小也与操作系统的限制有关。


4.表空间的统计信息
    SELECT d.status "Status", 
           d.tablespace_name "Name", 
           d.contents "Type", 
           d.extent_management "Extent Management", 
           NVL(a.bytes / 1024 / 1024, 0) "Size (M)", 
           TO_CHAR(NVL(a.bytes - NVL(f.bytes, 0), 0)/1024/1024,'99999999.999') "Used (M)", 
           ROUND(NVL((a.bytes - NVL(f.bytes, 0)) / a.bytes * 100, 0),2) "Used (%)" 
      FROM sys.dba_tablespaces d, 
           (select tablespace_name, sum(bytes) bytes from dba_data_files group by tablespace_name) a, 
           (select tablespace_name, sum(bytes) bytes from dba_free_space group by tablespace_name) f 
     WHERE d.tablespace_name = a.tablespace_name(+) 
       AND d.tablespace_name = f.tablespace_name(+) 
       AND NOT (d.extent_management like 'LOCAL' AND d.contents like 'TEMPORARY') 
     UNION ALL
    SELECT d.status "Status", 
           d.tablespace_name "Name", 
           d.contents "Type", 
           d.extent_management "Extent Management", 
           NVL(a.bytes / 1024 / 1024, 0) "Size (M)", 
           TO_CHAR(NVL(t.bytes,0)/1024/1024,'99999999.999') "Used (M)", 
           ROUND(NVL(t.bytes / a.bytes * 100, 0),2) "Used (%)" 
      FROM sys.dba_tablespaces d, 
           (select tablespace_name, sum(bytes) bytes from dba_temp_files group by tablespace_name) a,
           (select tablespace_name, sum(bytes_cached) bytes from v$temp_extent_pool group by tablespace_name) t 
     WHERE d.tablespace_name = a.tablespace_name(+) 
       AND d.tablespace_name = t.tablespace_name(+) 
       AND d.extent_management like 'LOCAL' 
       AND d.contents like 'TEMPORARY'
     ORDER BY 2;


    select t.tablespace_name name, d.allocated, u.used, f.free, t.status, d.datafiles, contents, 
           t.extent_management extman, t.segment_space_management segman
      from dba_tablespaces t,
           (select tablespace_name, sum(bytes) allocated, count(file_id) datafiles from dba_data_files group by tablespace_name) d,
           (select tablespace_name, sum(bytes) free from dba_free_space group by tablespace_name) f,
           (select tablespace_name, sum(bytes) used from dba_segments group by tablespace_name) u
     where t.tablespace_name = d.tablespace_name(+)
       and t.tablespace_name = f.tablespace_name(+)
       and t.tablespace_name = u.tablespace_name(+);


5.管理表空间
    (1).创建表空间
    create [smallfile|bigfile] tablespace tablespace_name
      datafile '/u01/app/oracle/oradata/SID/***.dbf'
      size 100M autoextend on next 10M maxsize 200M
      logging
      extent management local
      segment space management auto
      default nocompress;


    v$tablespace查看smallfile或bigfile;


    (2).更改表空间
    alter tablespace tablespaceoldname rename to tablespacenewname;          --重命名表空间
    alter tablespace tablespacename offline [normal|immediate|temporary];    --使表空间脱机
    alter tablespace tablespacename [read only|read write];                  --表空间标记只读和读写
    alter database datafile filename resize n[M|G|T];                        --重新调整表空间数据文件大小
    alter tablespace tablespacename add datafile datafilename size n[M|G|T]; --添加数据文件进表空间
    alter database datafile filename autoextend on next 100M maxsize 4G;     --修改表空间数据文件的自动扩展


    (3).重命名表空间和数据文件
    alter tablespace tablespacename rename to newtablespacename;
    alter tablespace tablespacename offline;
    host rename xxxxx.dbf xxxxxxx.dbf
    alter database rename file 'oldfilename' to 'newfilename';
    alter tablespace tablespacename online;


    (4).删除表空间
    drop tablespace tablespacenmae [including contents [and datafiles]];


    使用OMF特性管理数据库文件的创建,设置以下参数
    db_create_file_dest                   --数据文件的默认位置
    db_create_online_log_dest_1           --联机重做日志的默认位置
    db_create_online_log_dest_2
    db_create_online_log_dest_3
    db_create_online_log_dest_4
    db_create_online_log_dest_5
    db_recovery_file_dest                 --快速恢复区默认位置、归档日志文件和备份文件的默认位置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值