重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3142|回复: 0
打印 上一主题 下一主题

[认证考试] OCP课程29:管理Ⅰ之存储结构

  [复制链接]
跳转到指定楼层
楼主
发表于 2016-1-21 09:17:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

课程目标:

  • 块中表行数据的存储
  • 创建和管理表空间
  • 获取表空间信息

1、如何存储表数据

当创建表时,将创建一个段以保存其数据。一个表空间包含一组段。

在逻辑上,一个表包含行和列。行最终以行块的形式存储在数据库块中。因为在某些情况下,整行可能不会被存储在一个地方,故称之为行块。当插入行太大以至于一个块装不下(行链接)或者更新导致行增长超过当前块的空闲空间(行迁移),就会出现这种情况。当一个表超过255列时也会使用行块。


2、数据块

数据块可以分为三部分:

  • 块头:块头包含段类型(比如表或索引),数据块地址,表目录,行目录,以及约23个字节的事务槽,用于块内行的修改。块头从顶部向下增长。
  • 行数据:这是块中的行的实际数据。行数据从底部向上增长。
  • 空闲空间:空闲空间在块的中间,使块头和行数据都能增长。插入新行或者使用更大的值更新现有行都将占用空闲空间

块头增长的情况:

  • 行目录需要更多的行
  • 需要比最初配置更多的事务槽

最初,块中的空闲空间是连续的。然而,删除和更新可能会产生碎片。可以在必要的时候进行合并。


3、查看存储结构

逻辑数据结构存储在数据库的物理文件中。可以通过EM查看您的数据库的逻辑结构。通过点击服务器页面存储区域的链接进行查看。


4、创建表空间

(1)单击“服务器”选项卡,然后单击存储的标题下“表空间”。

(2)单击“创建”。 注:如果你想创建一个与现有表空间类似的表空间,选择一个现有的表空间,然后从动作菜单选择“Create Like”。

(3)输入表空间名称。在区管理标题下,选择“本地管理”。使用本地管理方式管理表空间的区更加高效,“字典管理”是使用数据字典来进行区管理,Oracle不推荐使用,此处是为了向后兼容。在“类型”标题下选择“永久”, 永久存储由系统或用户创建的数据库对象。在”状态”,选择“读写”, 读写状态是默认选项,用户可以读写创建后的表空间写。

(4)在“数据文件”区域,单击“添加”按钮为表空间增加数据文件。

一个表空间必须至少有一个数据文件。根据你的环境选择合适的存储类型。非常大的数据库可以使用大文件表空间,其中ASM或其他逻辑卷管理器支持条带或独立磁盘冗余阵列(RAID)和动态扩展逻辑卷。在“添加数据文件”页中选择所需的存储类型和输入所需的信息。如果是ASM,则磁盘组是必需的。如果是文件系统,则需要输入数据文件的文件名和路径。然后输入文件大小。在”存储“”部分,选择“数据文件满后自动扩展”,然后指定的增量大小。空间用完后会自动扩展,但受物理磁盘大小限制。可以指定数据文件的最大值也可以不指定。

点击“显示SQL”可以看到创建这个表空间的SQL语句:

CREATESMALLFILETABLESPACE"TS03"DATAFILE'+DATA'SIZE100MAUTOEXTENDONNEXT10MMAXSIZEUNLIMITEDLOGGINGEXTENTMANAGEMENTLOCALSEGMENTSPACEMANAGEMENTAUTO

点击“确定”完成表空间创建。


4、表空间的存储选项

区分配:“本地管理”表空间的区分配有下面两种方式:

  • 自动:表空间的区大小由系统管理。不能为临时表空间(temporary tablespace)指定自动方式。
  • 统一:表空间的区大小一致,默认大小为1MB。所有的临时表空间都采用这种方式。不能为撤销表空间(undo tablespace)指定统一方式。

段空间管理:“本地管理”表空间的段空间管理有下面两种方式:

  • 自动:Oracle数据库使用位图来管理段空闲空间。该位图描述了在一个段中的每个数据块的状态,表示可用于行插入的块的数量。数据块可用数量的多少会反应在位图中。通过使用位图,Oracle更加自动的管理空闲空间,因此,这种空间管理称为自动段空间管理(ASSM)。
  • 手动:使用空闲列表来管理段中的空闲空间。空闲列表是可用于插入行的数据块的列表。这种形式的段空间管理叫手动段空间管理。需要指定和调整表空间模式对象的PCTUSED,TREELISTS以及FREELIST GROUPS存储参数。这种方式主要用于向后兼容;建议使用ASSM。

压缩选项:默认情况下禁用数据段压缩。启用数据段压缩可以节省磁盘空间的使用,减少在缓存中的内存使用,并在读取时加快查询执行。然而,在数据加载和DML会增加CPU的开销。在有长时间读取操作的联机分析处理(OLAP)系统特别有用,但也可以用于联机事务处理(OLTP)系统。

日志:表空间中的段默认都是记录日志的,将对表空间对象的更改写入到联机重做日志文件。如果没有启用日志,对象的数据丢失是不可恢复的。当创建对象而不启用日志记录时,如果想让对象可恢复,则必须备份这些对象。

如果数据库是在FORCE LOGGING模式,将覆盖此处日志的设置。可以在数据库创建或数据库创建后使用alter database force logging语句将数据库置于FORCE LOGGING模式。

块信息:可以选择表空间块的大小。如果没有设置其他块大小的初始化参数(db_nk_cache_size),则不能选择其他值。如果设置了其他块大小的初始化参数(db_nk_cache_size),则可以选择设置的值。


5、数据库自带的表空间

创建数据库的时候系统创建的表空间:

  • SYSTEM:Oracle服务器使用系统表空间来管理数据库。 它包括包含数据库管理信息的数据字典和表,这些都包含在SYS模式下,并且只能由SYS用户或其他有相关权限的管理用户访问。
  • SYSAUX:SYSTEM表空间的一个辅助表空间。以前SYSTEM表空间中的一些部件和产品现在放到了SYSAUX表空间,10g及以后的版本的数据库必须有一个SYSAUX表空间。
  • TEMP:当执行一个需要创建临时段(如大的排序或索引的创建)的SQL语句就需要临时表空间。与为每个用户分配一个默认的表空间存储创建的数据对象一样,也会为每个用户分配一个临时表空间。最好的做法是定义数据库的默认临时表空间,新创建的用户无需设置就可以使用。在预先设定的数据库,TEMP表空间就是默认的临时表空间。这意味着,如果创建用户的时候没有指定临时表空间,Oracle数据库的就将TEMP分配给该用户。
  • UNDOTBS1:用于数据库服务器存储undo信息。如果数据库使用自动undo管理,那么在任何时候只能使用一个undo表空间。这个表空间是在创建数据库时创建的。
  • USERS:用于存储用户对象和数据。在创建用户的时候如果没有指定默认表空间,则该用户的默认表空间就是USERS。对于SYS和SYSTEM用户,默认的永久表空间是SYSTEM。
  • EXAMPLE:示例表空间。

例子:使用EM查看SYSAUX表空间使用情况


6、修改表空间

创建表空间后,可以进行如下修改:

  • 重命名:输入一个新名称。
  • 改变状态:一个表空间可以有三种不同的状态。不同表空间的类型有不同的状态。
    • Read Write:表空间联机,可以读取和写入。
    • Read Only:现有的事务可以完成(提交或回滚),但不允许新的DML操作。只读状态的表空间是联机的。不能设置SYSTEM和SYSAUX表空间为只读。也不能设置撤销和临时表空间为只读。
    • Offline:可以把一个在线表空间脱机,让这部分数据暂时不可用,其他数据还是可用的。offline有以下选项:
      • Normal:如果表空间的所有数据文件脱机时没有出现错误则可以正常脱机。在脱机的时候通过对所有数据文件发出检查点确保所有数据写入到磁盘。
      • Temporary:如果表空间的数据文件脱机时出现错误可以临时脱机。Oracle将数据文件脱机并发出检查点。如果表空间的一个或多个文件由于写错误脱机,然后使用offline temporary将表空间脱机,再联机就需要进行恢复。
      • Immediate:表空间可以采取脱机而不对数据库文件发出检查点。如果使用offline immediate将表空间脱机,再联机就需要先对表空间进行介质恢复。如果数据库运行在NOARCHIVELOG模式下,则不能使用immediate选项。
      • For Recover:已经废弃。
      • 注意:系统表空间不能脱机。
  • 改变大小:可以向现有的表空间增加数据文件或者更改数据文件的大小。
  • 存储选项:更改表空间的日志行为。
  • 阈值:改变警告点或空间使用的临界水平,有三个选择:
    • 使用数据库的默认阈值:默认值,可以设置这些默认值。
    • 指定阈值:为特定的表空间设置阈值。
    • 禁用阈值:关闭该表空间的告警。

例子:修改表空间


7、表空间的操作

  • 添加数据文件:添加一个数据文件到表空间。
  • 类似创建:用现有表空间作为模板创建另一个表空间。
  • 生成DDL:生成创建表空间的DDL语句。
  • 本地管理:将字典管理转换成本地管理。这种转换是单向的,不能转换回字典管理。如果需要,也可以使用PL/SQL包dbms_space_admin.tablespace_migrate_from_local转换为字典管理。
  • 只读:停止对表空间的所有写入。目前的事务都可以完成,但不允许新的DML操作。
  • 可写:允许DML操作。
  • 联机:脱机表空间联机。
  • 重组:移动对象回收不再使用的空间,不要在系统高峰期进行。
  • 运行段指导:启动段指导确定对象是否有空间可用于回收。
  • 显示相关性:显示对象的依赖表空间或者表空间依赖的对象。
  • 显示表空间内容:显示表空间所有段的信息,包括区位图。
  • 脱机:是联机表空间不可用。

8、删除表空间

可以删除表空间及其内容(在表空间中的段),需要DROP TABLESPACE系统权限。

当删除一个表空间,控制文件的文件指针被删除。如果使用Oracle-managed files(OMF),对应操作系统文件(数据文件)也会被删除。如果没有使用OMF,可以选择让Oracle服务器去删除对应的操作系统文件,也可以手动删除操作系统文件。

不能删除包含活动段的表空间。最好在删除前将表空间脱机。

例子:删除表空间


9、查看表空间信息

点击”查看”查看选定的表空间信息。在查看表空间的页,可以点击编辑修改表空间。

可以通过数据字典查询表空间和数据文件的信息:

  • 表空间信息:
    • dba_tablespaces
    • v$tablespace
  • 数据文件信息:
    • dba_data_files
    • v$datafile
  • 临时文件信息:
    • dba_temp_files
    • v$tempfile

例子:查看表空间及对应的数据文件

SQL> select tablespace_name,file_name from dba_data_files;

TABLESPACE_NAME                FILE_NAME

------------------------------ ------------------------------------------------------------

USERS                          +DATA/stone/datafile/users.259.893370691

UNDOTBS1                       +DATA/stone/datafile/undotbs1.258.893370691

SYSAUX                         +DATA/stone/datafile/sysaux.257.893370691

SYSTEM                         +DATA/stone/datafile/system.256.893370691

EXAMPLE                        +DATA/stone/datafile/example.265.893370807

TS02                           +DATA/stone/datafile/ts02.268.898602153

TS01                           +DATA/stone/datafile/ts01.dbf_reorg0

7 rows selected.


10、查看表空间内容

“显示表空间内容”页面可以看到表空间详细信息,段的信息以及区映射。

例子:查看表空间内容


11、Oracle-Managed Files(OMF)

使用OMF避免了在Oracle数据库中直接管理操作系统文件,特定的操作只需要使用数据库对象而不需要使用文件名,下面的数据库结构都可以使用OMF:

  • 表空间
  • 重做日志文件
  • 控制文件
  • 归档文件
  • 块改变跟踪文件
  • 闪回日志
  • RMAN备份

例子:查看参数DB_CREATE_FILE_DEST,并创建表空间不指定数据文件,使用默认值自动创建数据文件(大小100M,自动扩展,增量100M)

SQL> show parameter db_create_file_dest

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

db_create_file_dest                  string      +DATA

SQL> create tablespace tbs_1;

Tablespace created.

SQL> select tablespace_name,file_name,bytes/1024/1024 from dba_data_files where tablespace_name='TBS_1';

TABLESPACE_NAME                FILE_NAME                                          BYTES/1024/1024

------------------------------ -------------------------------------------------- ---------------

TBS_1                          +DATA/stone/datafile/tbs_1.269.898717219                       100

不要对OMF管理的文件重命名,不然数据库识别不到。ASM默认使用OMF,但是如果在表空间创建的时候给ASM数据文件指定了别名或者给现有的表空间增加了一个ASM数据文件,则该文件将不使用OFM。


12、扩展数据库

可以通过如下方式扩展数据库空间:

  • 创建一个新的表空间
  • 为现有的小文件表空间增加数据文件
  • 增大数据文件的大小
  • 为数据文件指定自动扩展选项

13、相关习题

(1)You executed the following command to create a tablespace called SALES_DATA:
   SQL> CREATE TABLESPACE sales_data  
            DATAFILE SIZE 100M
            SEGMENT SPACE MANAGEMENT AUTO;
Which two statements are true about the SALES_DATA tablespace? (Choose two)
A.  The database automatically determines the extent-sizing policy for the tablespace.
B.  The segments are automatically shrunk when the contents are removed from them.
C.  The allocation of extents within the tablespace is managed through the dictionary tables.
D.  The space utilization description of the data blocks in segments is recorded in bitmap blocks.
E.  The space utilization description of the data blocks in segments is managed through free lists.

答案:AD

(2)See the Exhibit:

Which statements are true regarding the USERS tablespace? (Choose all that apply.)   
A.  A bitmap is used to record free extents
B.  Free extents information is managed within the tablespace
C.  Free extents information is managed in the SYSAUX tablespace
D.  The data dictionary tables are updated when extents are allocated or deallocated

答案:AB

(3)SQL> CREATE BIGFILE TABLESPACE MRKT
2 DATAFILE '/u01/app/oracle/oradata/orcl/mrkt.dbf' size 10M LOGGING
3 EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
Tablespace created.
SQL> ALTER DATABASE DEFAULT TABLESPACE MRKT;
Database altered.
Which two statements are true regarding the MRKT tablespace? (Choose two.)
A. No more data files can be added to the tablespace.
B. Segment space is managed by free lists in the tablespace.
C. A user created without being assigned a default tablespace uses this tablespace.
D. The tablespace can be dropped with the current setting with segments present in it.

答案:AC

(4)Examine the command that is used to create a table:
SQL> CREATE TABLE orders (
oid NUMBER(6) PRIMARY KEY,
odate DATE,
ccode NUMBER (6),
oamt NUMBER(10,2)
) TABLESPACE users;
Which two statements are true about the effect of the above command? (Choose two.)
A. A CHECK constraint is created on the OID column.
B. A NOT NULL constraint is created on the OID column.
C. The ORDERS table is the only object created in the USERS tablespace.
D. The ORDERS table and a unique index are created in the USERS tablespace.
E. The ORDERS table is created in the USERS tablespace and a unique index is created on the OID column in the SYSTEM tablespace.

答案:BD

(5)You configured the Flash Recovery Area for your database. The database instance has been started in ARCHIVELOG mode and the LOG_ARCHIVE_DEST_1 parameter is not set.
What will be the implications on the archiving and the location of archive redo log files?
A.  Archiving will be disabled because the destination for the redo log files is missing
B.  The database instance will shut down and the error details will be logged in the alert log file
C.  Archiving  will  be  enabled  and  the  destination for  the  archived  redo  log file  will  be  set to the  Flash  Recovery Area implicitly
D.  Archiving  will  be  enabled  and  the location for  the  archive  redo  log file  will  be  created  in  the  default location $ORACLE_HOME/log

答案:C

(6)Identify two situations in which the block header grows in a data block. (Choose two.)
A.  When row directories need more row entries
B.  When there is row migration in the data block
C.  When there is an increase in the PCTFREE value for the data block
D.  When more transaction slots are required than are initially configured

答案:AD

(7)View  the  Exhibit.  You  want  to  create  a tablespace  to contain  objects  with  block  size  16  KB. But  while  configuring the  storage  you  find  that the  block  size  that  you  can  provide is  only  8 KB.
Which configuration could have enabled the block selection of 16 KB?
此主题相关图片如下:


A.  Choosing the extent allocation type to uniform
B.  Choosing the Segment Space Management option to manual
C.  Setting autoextension on for the data file mentioned for the tablespace
D.  Setting the DB_16K_CACHE_SIZE parameter for the database instance to a nonzero value

答案:D

(8)User  SCOTT  wants  to  perform  a  bulk  insert  operation  in  the  EMP_DEP  table.  SCOTT receives the following error after the INSERT statement is issued and few rows are inserted:
   INSERT INTO EMP_DEP (emp_id,name,salary,dep_name,mgr_id)
      *
   ERROR at line 1:
   ORA-01653: unable to extend table SCOTT.EMP_DEP by 128 in tablespace USERS  
Identify two actions either of which will help you resolve this problem. (Choose two.)
A.  Grant the RESOURCE role to SCOTT.
B.  Add data files to the USERS tablespace.
C.  Grant the CREATE ANY TABLE privilege to SCOTT.
D.  Increase the space for SCOTT on the USERS tablespace.
E.  Increase the size of the data file associated with the USERS tablespace.

答案:BE

(9)You execute the following command to change the status of the SALES tablespace:
   SQL> ALTER TABLESPACE sales OFFLINE;
Which statements describe the effect of the command? (Choose all that apply.)
A.  The tablespace would require recovery to go back online.
B.  A checkpoint is taken on all data files that are associated with the SALES tablespace.
C.  The sessions that subsequently try to access the objects in the SALES tablespace receive an error.
D.  The new status of the SALES tablespace is recorded in the control file when the database instance is closed.

答案:BC

(10)You configured the Flash Recovery Area (FRA) for your database. The database instance is running  in  ARCHIVELOG  mode.  The  default  location  for  the  archived  redo  log  files  is  the  Flash Recovery Area.
Which two files are removed automatically if the space is required in the FRA as per the retention policy?  (Choose two.)
A.  Flashback log files
B.  Backups that have become obsolete
C.  User managed backups of the data files and control files
D.  Archived redo log files that have multiple copies in a different archive location and not backed up

答案:AB

(11)The user HR receives the following error while inserting data into the TTK table:
   ERROR at line 1:
   ORA-01653: unable to extend table HR.TTK by 128 in tablespace SMD
Upon investigation, you find that SMD is a small file tablespace.
Which three action would allow the user to insert data? (Choose three.)
A.  Add a data file to the SMD tablespace.
B.  Add a data file to the temporary tablespace associated with the user HR.
C.  Resize the data file associated with the SMD tablespace to make it larger.
D.  Alter the data file associated with the SMD tablespace to grow automatically.
E.  Change the segment space management for the SMD tablespace to automatic.

答案:ACD


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 支持支持1 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-5-6 01:30 , Processed in 0.106753 second(s), 20 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表