zoukankan      html  css  js  c++  java
  • ORACLE管理存储结构之物理机构+逻辑结构【weber出品】

    一、数据库的存储结构有物理结构和逻辑结构组成的

    物理结构:物理上,oracle是由一些操作系统文件组成的

    SQL> select name from v$datafile;
    
    NAME
    --------------------------------------------------------------------------------
    /u01/app/oracle/oradata/orcl/system01.dbf
    /u01/app/oracle/oradata/orcl/undotbs01.dbf
    /u01/app/oracle/oradata/orcl/sysaux01.dbf
    /u01/app/oracle/oradata/orcl/users01.dbf
    /u01/app/oracle/oradata/orcl/example01.dbf

    如何更好的访问和管理这些文件以及文件中的数据呢?
    必须使用逻辑方法,比如表就是一种逻辑方式

    逻辑结构:从逻辑上分,一个oracle数据库是由多个表空间组成的

    表空间的作用:根据实际的需要,对物理文件进行分组,比如系统数据单独放到一个表空间中,用户数据可以放到单独的
    一个表空间中

    数据库中有哪些表空间?

    SQL> col ts for a15
    SQL> col file_name for a45
    SQL> /
    
    SQL> select tablespace_name ts,file_id,file_name from dba_data_files;
    
    TS                 FILE_ID FILE_NAME
    --------------- ---------- ---------------------------------------------
    USERS                    4 /u01/app/oracle/oradata/orcl/users01.dbf
    SYSAUX                   3 /u01/app/oracle/oradata/orcl/sysaux01.dbf
    UNDOTBS1                 2 /u01/app/oracle/oradata/orcl/undotbs01.dbf
    SYSTEM                   1 /u01/app/oracle/oradata/orcl/system01.dbf
    EXAMPLE                  5 /u01/app/oracle/oradata/orcl/example01.dbf

    SYSTEM:系统表空间,存储系统数据,比如数据字典信息,oracle预定义的包

    SYSAUX:系统辅助表空间,是oracle10g新增加的一个表空间,要来分担SYSTEM表空间的压力,主要存储
    自动性能诊断数据

    UNDOTBS1:undo表空间,主要是存储undo数据(是数据被修改之前的原值)

    USERS:用户表空间,主要是存储普通用户的数据

    EXAMPLE:示例表空间,比如hr、oe等示例方案的数据

    段:表空间从逻辑上是由段组成的,在创建对象的时候,如果这个对象需要空间,oracle都会为这个对象分配一个段

    查看当前数据库中的段的类型:

    SQL> select distinct segment_type from dba_segments;
    
    SEGMENT_TYPE
    ------------------
    LOBINDEX
    INDEX PARTITION
    TABLE PARTITION
    NESTED TABLE
    ROLLBACK
    LOB PARTITION
    LOBSEGMENT
    INDEX
    TABLE
    CLUSTER
    TYPE2 UNDO

    从以上的输出可以看出:表、索引需要段

    区:段是由区组成的,区是为段进行空间分配的单位,当段的空间不足的时候,oracle会以区为单位为段分配空间

    oracle数据块:一个区是由连续的n个oracle数据块组成的

    os块:一个oracle数据块是由n个连续的os块组成的

    oracle最小的I/O单位是一个oracle数据块

    实验:

    在a会话:

    SQL> create table t as select * from emp;
    
    Table created.
    
    SQL> insert into t select * from t;
    
    /

    ---不停的翻倍

    在b会话:

    查看区的分配以及区的大小:

    SQL> col owner for a5
    SQL> col segment_name for a14
    SQL>  set linesize 100
    
    SQL> select owner,segment_name,segment_type,extent_id,file_id,blocks 
             from dba_extents where owner='SCOTT' and segment_name='T';
    
    SQL> /   
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS
    ----- -------------- ------------------ ---------- ---------- ----------
    SCOTT T              TABLE                       0          4          8
    
    
    
    SQL> /
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS
    ----- -------------- ------------------ ---------- ---------- ----------
    SCOTT T              TABLE                       0          4          8
    SCOTT T              TABLE                       1          4          8
    
    select owner,segment_name,segment_type,extent_id,file_id,blocks,block_id 
    from dba_extents where owner='SCOTT' and segment_name='T'
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS   BLOCK_ID
    ----- -------------- ------------------ ---------- ---------- ---------- ----------
    SCOTT T              TABLE                       0          4          8        385
    SCOTT T              TABLE                       1          4          8        393
    
    SQL> /
    
    .
    .
    .
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS   BLOCK_ID
    ----- -------------- ------------------ ---------- ---------- ---------- ----------
    SCOTT T              TABLE                       0          4          8        385
    SCOTT T              TABLE                       1          4          8        393
    SCOTT T              TABLE                       2          4          8        401
    SCOTT T              TABLE                       3          4          8        409
    SCOTT T              TABLE                       4          4          8        417
    SCOTT T              TABLE                       5          4          8        425
    SCOTT T              TABLE                       6          4          8        433
    SCOTT T              TABLE                       7          4          8        441
    SCOTT T              TABLE                       8          4          8        449
    SCOTT T              TABLE                       9          4          8        457
    SCOTT T              TABLE                      10          4          8        465
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS   BLOCK_ID
    ----- -------------- ------------------ ---------- ---------- ---------- ----------
    SCOTT T              TABLE                      11          4          8        473
    SCOTT T              TABLE                      12          4          8        481
    SCOTT T              TABLE                      13          4          8        489
    SCOTT T              TABLE                      14          4          8        497
    SCOTT T              TABLE                      15          4          8        505
    SCOTT T              TABLE                      16          4        128        521
    
    17 rows selected.

    手动为段分配区:

    SQL> create table e as select * from emp;
    
    Table created.
    
    SQL> desc user_extents
     Name                                      Null?    Type
     ----------------------------------------- -------- ----------------------------
     SEGMENT_NAME                                       VARCHAR2(81)
     PARTITION_NAME                                     VARCHAR2(30)
     SEGMENT_TYPE                                       VARCHAR2(18)
     TABLESPACE_NAME                                    VARCHAR2(30)
     EXTENT_ID                                          NUMBER
     BYTES                                              NUMBER
     BLOCKS                                             NUMBER
    
    select segment_name,segment_type,extent_id,blocks from user_extents where segment_name='E'
    
    SEGMENT_NAME         SEGMENT_TYPE        EXTENT_ID     BLOCKS
    -------------------- ------------------ ---------- ----------
    E                    TABLE                       0          8
    
    SQL> alter table e allocate extent;
    
    Table altered.
    
    SQL> select segment_name,segment_type,extent_id,blocks from user_extents where segment_name='E';
    
    SEGMENT_NAME         SEGMENT_TYPE        EXTENT_ID     BLOCKS
    -------------------- ------------------ ---------- ----------
    E                    TABLE                       0          8
    E                    TABLE                       1          8
    
    SQL> alter table e deallocate unused;
    
    Table altered.
    
    SQL> select segment_name,segment_type,extent_id,blocks from user_extents where segment_name='E';
    
    SEGMENT_NAME         SEGMENT_TYPE        EXTENT_ID     BLOCKS
    -------------------- ------------------ ---------- ----------
    E                    TABLE                       0          8
    
    SQL> conn /as sysdba
    SQL> alter tablespace users add datafile '/u01/app/oracle/oradata/orcl/users02.dbf' size 100M;
    
    SQL> conn scott/tiger
    Connected.
    
     alter table e allocate extent(size 1M datafile '/u01/app/oracle/oradata/orcl/users02.dbf')
    
    SQL> select segment_name,segment_type,extent_id,blocks from user_extents where segment_name='E';
    
    SEGMENT_NAME         SEGMENT_TYPE        EXTENT_ID     BLOCKS
    -------------------- ------------------ ---------- ----------
    E                    TABLE                       0          8
    E                    TABLE                       1          8
    E                    TABLE                       2          8
    E                    TABLE                       3          8
    E                    TABLE                       4          8
    E                    TABLE                       5          8
    E                    TABLE                       6          8
    E                    TABLE                       7          8
    E                    TABLE                       8          8
    E                    TABLE                       9          8
    E                    TABLE                      10          8
    
    SEGMENT_NAME         SEGMENT_TYPE        EXTENT_ID     BLOCKS
    -------------------- ------------------ ---------- ----------
    E                    TABLE                      11          8
    E                    TABLE                      12          8
    E                    TABLE                      13          8
    E                    TABLE                      14          8
    E                    TABLE                      15          8
    E                    TABLE                      16        128
    
    17 rows selected.
    
    SQL> conn /as sysdba
    Connected.
    SQL> col owner for a5
    SQL> col segment_name for a14
    SQL> set linesize 100
    
      1  select owner,segment_name,segment_type,extent_id,file_id,blocks,block_id
      2* from dba_extents where owner='SCOTT' and segment_name='E'
    SQL> /
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS   BLOCK_ID
    ----- -------------- ------------------ ---------- ---------- ---------- ----------
    SCOTT E              TABLE                       0          4          8        385
    SCOTT E              TABLE                       1          6          8          9
    SCOTT E              TABLE                       2          6          8         17
    SCOTT E              TABLE                       3          6          8         25
    SCOTT E              TABLE                       4          6          8         33
    SCOTT E              TABLE                       5          6          8         41
    SCOTT E              TABLE                       6          6          8         49
    SCOTT E              TABLE                       7          6          8         57
    SCOTT E              TABLE                       8          6          8         65
    SCOTT E              TABLE                       9          6          8         73
    SCOTT E              TABLE                      10          6          8         81
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS   BLOCK_ID
    ----- -------------- ------------------ ---------- ---------- ---------- ----------
    SCOTT E              TABLE                      11          6          8         89
    SCOTT E              TABLE                      12          6          8         97
    SCOTT E              TABLE                      13          6          8        105
    SCOTT E              TABLE                      14          6          8        113
    SCOTT E              TABLE                      15          6          8        121
    SCOTT E              TABLE                      16          6        128        137
    
    SQL> alter table scott.e deallocate unused;
    
    Table altered.
    
    SQL> select owner,segment_name,segment_type,extent_id,file_id,blocks,block_id
      2  from dba_extents where owner='SCOTT' and segment_name='E';
    
    OWNER SEGMENT_NAME   SEGMENT_TYPE        EXTENT_ID    FILE_ID     BLOCKS   BLOCK_ID
    ----- -------------- ------------------ ---------- ---------- ---------- ----------
    SCOTT E              TABLE                       0          4          8        385
  • 相关阅读:
    ASP.NET存储Session的StateServer
    EasyUI中DataGrid构建复合表头
    EasyUI使用DataGrid向服务器传参
    CentOS 7 U盘安装问题解决
    对指定表的某一列数据合并单元格
    常用的一些sql
    Oracle数据库表空间常用操作
    修改Oracle redo.log文件的大小
    UOJ 非传统题配置手册
    UOJ552 【UNR #4】同构判定鸭【线性代数,哈希】
  • 原文地址:https://www.cnblogs.com/yaoweber/p/3984989.html
Copyright © 2011-2022 走看看