您的位置:首页 > 数据库 > Oracle

Oracle逻辑存储结构1—概述

2015-08-15 10:49 666 查看
友情提示:笔者英语很渣,阅读时建议保持极强的耐心,同时欢迎提出各种意见~~

本篇是摘取自Oracle11gR2官方文档(Concepts)的第十二章节Logical Storage Structures。

粗糙的译文:

12章 Oracle逻辑存储结构

这一章描述的是在Oracle逻辑存储内部之间的关系。这些结构是Oracle数据库创建的,和操作系统无关。

这章包括以下几节:

• 介绍逻辑存储结构(本篇译文就是)

• 数据块的概述(可参考《Oracle逻辑存储结构5—块》译文)

• 区的概述(可参考《Oracle逻辑存储结构4—区》译文)

• 段的概述(可参考《Oracle逻辑存储结构3—段》译文)

• 表空间的概述(可参考《Oracle逻辑存储结构2—表空间》译文)

介绍逻辑存储结构

数据库为数据库中所有的数据分配逻辑空间,这个逻辑空间是由数据块、区、段和表空间组成的。从物理存储来看(可参考《Oracle物理存储结构1—概述》译文),这些数据是存放在磁盘的数据文件中的,存储在操作系统的块中。

图12-1 是逻辑存储和物理存储的关系图表。



逻辑存储体系

图12-2 展示了在表空间内部的数据块、区和段的层级关系。在这个例子中,一个段有两个区分别存放在不同的数据文件中。



最小颗粒度级别,Oracle数据库的数据存储在数据块中。一个逻辑的数据块相当于一个指定的几bytes的物理磁盘空间。例如,2KB的数据块是Oracle数据库可用或者可分配的最小单位。

一个区就是由逻辑上连续的数据库组成的来存储指定信息的。在图12-2中,24KB的区包含有12个数据块,72KB的区包含有36个数据块。

一个段就是由一组区组成的来服务一个指定的数据库对象的,例如一个表。举个例子吧,employees表的数据存放在它自己的数据段中,索引数据存放在它自己的索引段中。每一个数据库对象存储在对应一个段中。

每一个段只属于一个表空间,所以,在一个段内的所有区也都只属于一个表空间。如图12-2所示,在一个表空间内,一个段可以包含多个数据文件中的多个区。例如,一个段内的一个区可以存放在users01.dbf数据文件中,另一个区可以存放在users02.dbf数据文件中。一个区不能跨越不同的数据文件存放。

逻辑空间管理

Oracle数据库在表空间中必须用逻辑空间管理来追踪和分配区。当一个数据库对象请求一个区时,数据库必须有找到和提供的措施。同样的,当一个对象不再需要一个区时,数据库必须让这个空闲的区可被再利用。

你可以创建管理Oracle数据库表空间的方式,有以下两种方式:

• 本地管理表空间 (默认的方式)

数据库在表空间中使用位图来管理区。所以,本地管理表空间方式会在表空间中留有一个位图的区域。在这种表空间,数据库可以使用自动段空间管理(ASSM)或者手动段空间管理(MSSM)。

• 字典管理表空间

数据库用数据字典的方式去管理区

图12-3所示表空间的逻辑空间管理的优选方案。



本地管理表空间(Locally Managed Tablespaces简称LMT)

一个本地管理表空间在数据文件头部有一个位图区域去追踪数据文件中空闲和使用的空间。位图中每个bit代表着一组数据块。当一个空间被使用,或者被释放以供重新使用时,Oracle数据库都会更新数据文件头部的这个记录,反映这个变化。

下图是一个位图管理存储的概念图。在头部的1表示在使用的空间,0表示可用空间。



本地管理表空间有以下优势:

• 避免使用数据字典管理区

在数据字典管理表空间是可以发生递归操作 如果在区中消耗或者释放空间,在数据字典表或者回滚段中也要消耗或者释放空间。

• 自动跟踪表空间中可用空间

用这种方式的话,数据库会减少合并可用空间的操作。

• 表空间的区大小由数据库自己决定。

在本地管理表空间中,所有的区有统一的大小,避免空间碎管理问题。

注释:

Oracle存储推荐使用本地管理表空间的自动段空间管理。

段空间管理是表空间包含的段的一种属性。用本地管理表空间,数据库可以自动或者手动的管理段。例如,当在表空间中段是手动管理是时,用户可以使用自动段管理空间。

自动段空间管理(ASSM)

ASSM使用位图管理空间。位图可以有以下几个优势:

• 使管理简单化

ASSM避免了需要手工的设定一些存储的参数。 只有一个重要的PCTFREE 参数需要设置。这个参数指定的是在数据块中保留用来更新操作的空间百分比的值

• 提高并发性

多个事务可以寻找不同的空闲数据块, 减少竞争和等待。应用ASSM比应用MSSM的性能更好。

• 在RAC环境中也减少了竞争

ASSM 使得本地管理表空间更加有效。

手动段空间管理(MSSM)

MSSM使用一个叫做free list的列表结构来管理段中的可用空间。因为数据库对象需要空闲的空间,free list追踪在高水位线(HWM)下的块 ,高水位线就是区分已经使用的段的空间和没使用的。当块被使用时,数据库需要把块放在free list上或者从free list上删除。

除了PCTFREE 参数之外,MSSM还要去设置PCTUSED、FREELISTS、 和FREELIST GROUPS 这几个参数。PCTUSED参数是设定当前块中使用空间的百分比。当块使用空间小于PCTUSED这个值,数据库才会把这个块放到free list 上。例如,如果你在建表时设置PCTUSED参数 为40,那么只有当块里的数据小于40%时,才可以插入数据。

举例说明:假设你向一个表中插入数据。数据库会查看这个表中free list 来用于第一个可用的块。如果行不适合这个块大小,且块中使用率大于等于这个PCTUSED 参数的话,数据库会将这个块从free list移出,继续寻找下一个块来到free list中。如果删除这个块中的行,数据库会检查块的使用率是否小于PCTUSED 参数,如果小于的话,就会把这个块放回到free list 中。

一个对象可以有多个free lists 。在这种方式,在一张表中的多个DML操作可以使用不同的lists,可以减少竞争。数据库中的每个session在生命周期里只可以使用一个free list。

如图12-4所示,你可以创建一个或者多个list groups(包含有free lists ) 的对象。每个组里都有一个master free list 来管理这个组内的free list ,这对于free lists空间的消耗特别是free list groups的消耗是非常有好处的。



手动段空间管理比较复杂。你必须调整PCTFREE 和PCTUSED参数来减少行迁移和避免浪费空间。例如,在段中的每个块使用的空间为一半,PCTUSED参数为40,这样的话数据库就不允许向任何块中插入数据了。调整空间分配比较困难,Oracle存储推荐的方案是ASSM。在ASSM中,只要关心PCTFREE 这个是否可以向块中插入新数据的参数就可以了,free lists 和PCTUSED参数可以不用考虑。

注释:

Oracle 空闲列表管理机制与pctfree和pctused参数

Oracle中有空闲列表(free list)的概念.空闲列表中存储的是可供插入新行的块信息

所以新的行数据只会插入到那些存在于空闲列表中的块.

空闲列表的管理机制与建表时pctfree和pctused参数两个参数有紧密关系.

当一个块第一次开辟的时候,当然是在空闲列表中的.

随着不断地插入行数据,当使用率达到或者超过 1-PCTFREE%的时候,该块从空闲列表中移出.

所以这时候新的行数据不可能再存放到该块中. 那这个剩余的PCTFREE%部分岂不是浪费了,当然不是了.

随着UPDATE 活动的增多,某些存在该块中的行数据的就会变大,变大部分的数据就存放在PCTFREE部分中.

Delete活动,会将行数据从块中抹去,这时候块的使用率可能会低于1-pctfree%, 但是该块还是不会

立即回到空闲列表,也就是该块这时候不会接受新的行数据. 那什么时候该块会重新回到空闲列表中呢?

直到该块的使用率低于PCTUSED%的时候,才会回到空闲列表中,也就是说这时候可以接受新的行数据了.

所以当你的系统有较多的update活动并且行数据的大小变化较大的时候,应该预留较多的pctfree.

例如一个公文审批系统,一个公文在流转的过程中,数据在不断地变大。

对于一个没有update活动的表,可以将其设置为0.如系统中的组织架构表,更新的可能性几乎为0。

如果你想充分地利用块,则将PCTUsed 设置地高一点,以便在发生Delete活动后,快速地回到空闲列表中.

字典管理表空间(Dictionary-Managed Tablespaces简称DMT)

字典管理表空间使用的是数据字典去管理区。一个区中有分派或者释放以供重新使用时,Oracle数据库会更新在数据字典中的表。例如,当一个表需要一个区时,数据库请求数据字典表,寻找空闲的区。如果数据库找到了空间,它会修改一个数据字典表插入一行数据到另一个表中。用这种方式,数据库通过修改和移动数据来管理空间。

SQL在数据库后台执行来获取空间是递归SQL。因为更新数据字典是串行的,所以大量的递归SQL调用容易出现竞争问题,性能变差。本地管理表空间是默认的方式,可以避免此性能的问题。

精美的原文:

12 Logical Storage Structures

This chapter describes the nature of and relationships among logical storage structures. These structures are created and recognized by Oracle Database and are not known to the operating system.

This chapter contains the following sections:

• Introduction to Logical Storage Structures

• Overview of Data Blocks

• Overview of Extents

• Overview of Segments

• Overview of Tablespaces

Introduction to Logical Storage Structures

Oracle Database allocates logical space for all data in the database. The logical units of database space allocation are data blocks, extents, segments, and tablespaces. At a physical level, the data is stored in data files on disk (see Chapter 11, “Physical Storage Structures”). The data in the data files is stored in operating system blocks.

Figure 12-1 is an entity-relationship diagram for physical and logical storage. The crow’s foot notation represents a one-to-many relationship.



Logical Storage Hierarchy

Figure 12-2 shows the relationships among data blocks, extents, and segments within a tablespace. In this example, a segment has two extents stored in different data files.



At the finest level of granularity, Oracle Database stores data in data blocks. One logical data block corresponds to a specific number of bytes of physical disk space, for example, 2 KB. Data blocks are the smallest units of storage that Oracle Database can use or allocate.

An extent is a set of logically contiguous data blocks allocated for storing a specific type of information. In Figure 12-2, the 24 KB extent has 12 data blocks, while the 72 KB extent has 36 data blocks.

A segment is a set of extents allocated for a specific database object, such as a table. For example, the data for the employees table is stored in its own data segment, whereas each index for employees is stored in its own index segment. Every database object that consumes storage consists of a single segment.

Each segment belongs to one and only one tablespace. Thus, all extents for a segment are stored in the same tablespace. Within a tablespace, a segment can include extents from multiple data files, as shown in Figure 12-2. For example, one extent for a segment may be stored in users01.dbf, while another is stored in users02.dbf. A single extent can never span data files.

Logical Space Management

Oracle Database must use logical space management to track and allocate the extents in a tablespace. When a database object requires an extent, the database must have a method of finding and providing it. Similarly, when an object no longer requires an extent, the database must have a method of making the free extent available.

Oracle Database manages space within a tablespace based on the type that you create. You can create either of the following types of tablespaces:

• Locally managed tablespaces (default)

The database uses bitmaps in the tablespaces themselves to manage extents. Thus, locally managed tablespaces have a part of the tablespace set aside for a bitmap. Within a tablespace, the database can manage segments with automatic segment space management (ASSM) or manual segment space management (MSSM).

• Dictionary-managed tablespaces

The database uses the data dictionary to manage extents (see “Overview of the Data Dictionary”).

Figure 12-3 shows the alternatives for logical space management in a tablespace.



Locally Managed Tablespaces

A locally managed tablespace maintains a bitmap in the data file header to track free and used space in the data file body. Each bit corresponds to a group of blocks. When space is allocated or freed, Oracle Database changes the bitmap values to reflect the new status of the blocks.

The following graphic is a conceptual representation of bitmap-managed storage. A 1 in the header refers to used space, whereas a 0 refers to free space.



A locally managed tablespace has the following advantages:

• Avoids using the data dictionary to manage extents

Recursive operations can occur in dictionary-managed tablespaces if consuming or releasing space in an extent results in another operation that consumes or releases space in a data dictionary table or undo segment.

• Tracks adjacent free space automatically

In this way, the database eliminates the need to coalesce free extents.

• Determines the size of locally managed extents automatically

Alternatively, all extents can have the same size in a locally managed tablespace and override object storage options.

Note:

Oracle strongly recommends the use of locally managed tablespaces with Automatic Segment Space Management.

Segment space management is an attribute inherited from the tablespace that contains the segment. Within a locally managed tablespace, the database can manage segments automatically or manually. For example, segments in tablespace users can be managed automatically while segments in tablespace tools are managed manually

Automatic Segment Space Management

The ASSM method uses bitmaps to manage space. Bitmaps provide the following advantages:

• Simplified administration

ASSM avoids the need to manually determine correct settings for many storage parameters. Only one crucial SQL parameter controls space allocation: PCTFREE. This parameter specifies the percentage of space to be reserved in a block for future updates (see “Percentage of Free Space in Data Blocks”).

• Increased concurrency

Multiple transactions can search separate lists of free data blocks, thereby reducing contention and waits. For many standard workloads, application performance with ASSM is better than the performance of a well-tuned application that uses MSSM.

• Dynamic affinity of space to instances in an Oracle Real Application Clusters (Oracle RAC) environment

ASSM is more efficient and is the default for permanent, locally managed tablespaces.

Note:

This chapter assumes the use of ASSM in all of its discussions of logical storage space.

Manual Segment Space Management

The legacy MSSM method uses a linked list called a free list to manage free space in the segment. For a database object that has free space, a free list keeps track of blocks under the high water mark (HWM), which is the dividing line between segment space that is used and not yet used. As blocks are used, the database puts blocks on or removes blocks from the free list as needed.

In addition to PCTFREE, MSSM requires you to control space allocation with SQL parameters such as PCTUSED, FREELISTS, and FREELIST GROUPS. PCTUSED sets the percentage of free space that must exist in a currently used block for the database to put it on the free list. For example, if you set PCTUSED to 40 in a CREATE TABLE statement, then you cannot insert rows into a block in the segment until less than 40% of the block space is used.

As an illustration, suppose you insert a row into a table. The database checks a free list of the table for the first available block. If the row cannot fit in the block, and if the used space in the block is greater than or equal to PCTUSED, then the database takes the block off the list and searches for another block. If you delete rows from the block, then the database checks whether used space in the block is now less than PCTUSED. If so, then the database places the block at the beginning of the free list.

An object may have multiple free lists. In this way, multiple sessions performing DML on a table can use different lists, which can reduce contention. Each database session uses only one free list for the duration of its session.

As shown in Figure 12-4, you can also create an object with one or more free list groups, which are collections of free lists. Each group has a master free list that manages the individual process free lists in the group. Space overhead for free lists, especially for free list groups, can be significant.



Managing segment space manually can be complex. You must adjust PCTFREE and PCTUSED to reduce row migration (see “Chained and Migrated Rows”) and avoid wasting space. For example, if every used block in a segment is half full, and if PCTUSED is 40, then the database does not permit inserts into any of these blocks. Because of the difficulty of fine-tuning space allocation parameters, Oracle strongly recommends ASSM. In ASSM, PCTFREE determines whether a new row can be inserted into a block, but it does not use free lists and ignores PCTUSED.

Dictionary-Managed Tablespaces

A dictionary-managed tablespace uses the data dictionary to manage its extents. Oracle Database updates tables in the data dictionary whenever an extent is allocated or freed for reuse. For example, when a table needs an extent, the database queries the data dictionary tables, and searches for free extents. If the database finds space, then it modifies one data dictionary table and inserts a row into another. In this way, the database manages space by modifying and moving data.

The SQL that the database executes in the background to obtain space for database objects is recursive SQL. Frequent use of recursive SQL can have a negative impact on performance because updates to the data dictionary must be serialized. Locally managed tablespaces, which are the default, avoid this performance problem.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: