2 Data Blocks, Extents, and Segments


001 This chapter describes the nature of and relationships among the logical storage structures in the Oracle database server.
 
本章讲述Oracle数据库中各种逻辑存储结构(logical storage structure)的特性和相互关系。
002 This chapter contains the following topics: 本章包含以下主题:
003

Introduction to Data Blocks, Extents, and Segments

2.1 数据块,数据扩展,段简介

004 Oracle allocates logical database space for all data in a database. The units of database space allocation are data blocks, extents, and segments. Figure 2-1 shows the relationships among these data structures:
 
Oracle数据库负责为所有数据分配逻辑存储空间。数据库空间的分配单位有数据块(Data Block),数据扩展(Extent),和段(Segment)。图2-1 说明了这些数据结构之间的关系。
005 Figure 2-1 The Relationships Among Segments, Extents, and Data Blocks
 
图2-1 段,数据扩展,数据块之间的关系
006

Description of Figure 2-1 follows
 

007 At the finest level of granularity, Oracle stores data in data blocks (also called logical blocks, Oracle blocks, or pages). One data block corresponds to a specific number of bytes of physical database space on disk.
 
Oracle存储数据的最小粒度(finest level of granularity)被称为数据块(data block)(也叫做逻辑块(logical block),Oracle块(Oracle block)或(page))。一个数据块对应于磁盘上数个字节(byte)的物理数据库空间。
008 The next level of logical database space is an extent. An extent is a specific number of contiguous data blocks allocated for storing a specific type of information.
 
处于数据块之上的逻辑数据库空间是数据扩展(extent)。数据扩展是为存储数据而分配的一组连续的数据块。
009 The level of logical database storage greater than an extent is called a segment. A segment is a set of extents, each of which has been allocated for a specific data structure and all of which are stored in the same tablespace. For example, each table's data is stored in its own data segment, while each index's data is stored in its own index segment. If the table or index is partitioned, each partition is stored in its own segment.
 
位于数据扩展之上的逻辑数据库存储结构是(segment)。段由一组数据扩展 (extent)构成,这些数据扩展位于同一表空间(tablespace)中,用于存储各种逻辑数据结构。例如每个表(table)的数据都存储在其自身的数据段(data segment)中,每个索引(index)的数据都存储在其自身的索引段(index segment)中。如果表或索引是分区存储(partitioned)的,则每个分区拥有自己的段。
010 Oracle allocates space for segments in units of one extent. When the existing extents of a segment are full, Oracle allocates another extent for that segment. Because extents are allocated as needed, the extents of a segment may or may not be contiguous on disk.
 
Oracle为段(segment)分配空间时以数据扩展(extent)为单位。当段内已有的数据扩展没有可用空间时,Oracle为此段分配一个新的数据扩展。因为数据扩展是随需分配的,所以一个段内的数据扩展在磁盘上未必是连续的。
011 A segment and all its extents are stored in one tablespace. Within a tablespace, a segment can include extents from more than one file; that is, the segment can span datafiles. However, each extent can contain data from only one datafile.
 
一个段(segment)以及属于她的所有数据扩展(extent)必须包含在同一表空间(tablespace)中。但在一个表空间内,属于同一个段的数据扩展可以分布 在多个数据文件(datafile)上,即段可以跨文件存储。但是每个数据扩展只能包含于同一个数据文件中。
012 Although you can allocate additional extents, the blocks themselves are allocated separately. If you allocate an extent to a specific instance, the blocks are immediately allocated to the free list. However, if the extent is not allocated to a specific instance, then the blocks themselves are allocated only when the high water mark moves. The high water mark is the boundary between used and unused space in a segment.
 
在用户分配一个新的数据扩展(extent)时,其中的数据块(data block)未必被同时分配。如果用户是为某个数据库对象分配数据扩展 ,那么数据块也同时被立即分配并加入可用块列表(free list)中。如果数据扩展并非专为某数据库对象分配,那么数据块只在高水位线(high water mark)移动时才被分配。高水位线是段(segment)中已用和未用空间的边界。
013

Note:

Oracle recommends that you manage free space automatically. See "Free Space Management".

提示:

Oracle建议用户使用可用空间自动管理功能。详见“可用空间管理”。

014

Overview of Data Blocks

2.2 数据块概述

015 Oracle manages the storage space in the datafiles of a database in units called data blocks. A data block is the smallest unit of data used by a database. In contrast, at the physical, operating system level, all data is stored in bytes. Each operating system has a block size. Oracle requests data in multiples of Oracle data blocks, not operating system blocks.
 
Oracle对数据库数据文件(datafile)中的存储空间进行管理的单位是数据块(data block)。数据块是数据库中最小的(逻辑)数据单位。与数据块对应的,所有数据在操作系统级的最小物理存储单位是字节(byte)。每种操作系统都有一个被称为块容量(block size)的参数。Oracle每次获取数据时,总是访问整数个(Oracle)数据块,而不是按照操作系统块的容量访问数据。
016 The standard block size is specified by the DB_BLOCK_SIZE initialization parameter. In addition, you can specify of up to five nonstandard block sizes. The data block sizes should be a multiple of the operating system's block size within the maximum limit to avoid unnecessary I/O. Oracle data blocks are the smallest units of storage that Oracle can use or allocate.
 
数据库中标准的数据块(data block)容量是由初始化参数 DB_BLOCK_SIZE 指定的。除此之外,用户还可以指定五个非标准的数据块容量(nonstandard block size)。数据块容量应该设为操作系统块容量的整数倍(同时小于数据块容量的最大限制),以便减少不必要的I/O操作。Oracle数据块是Oracle可以使用和分配的最小存储单位。
017

See Also:

  • Your Oracle operating system-specific documentation for more information about data block sizes
  • Multiple Block Sizes

另见:

018

Data Block Format

2.2.1 数据块结构

019 The Oracle data block format is similar regardless of whether the data block contains table, index, or clustered data. Figure 2-2 illustrates the format of a data block.
 
在Oracle中,不论数据块中存储的是表(table)、索引(index)或簇表(clustered data),其内部结构都是类似的。图2-2 说明了数据块的结构。
020 Figure 2-2 Data Block Format
 
图2-2 数据块结构
021

Description of Figure 2-2 follows
 

022

This image shows the parts of a database block: common and variable header, table directory, row directory, free space, and row data. The parts are described in subsequent sections. Arrows indicate that the amount of free space in the database block is variable.

本图显示了数据块的各个组成部分,包括:数据块头(包括标准内容和可变内容)(common and variable header),表目录区(table directory),行目录区(row directory),可用空间区(free space),行数据区(row data)。以下各节将分别讲解各个组成部分。图中两个箭头表示一个数据块中的可用空间区的容量是可变的。

023

Header (Common and Variable)

2.2.1.1 数据块头(包括标准内容和可变内容)

024 The header contains general block information, such as the block address and the type of segment (for example, data or index).
 
数据块头(header)中包含了此数据块的概要信息,例如块地址(block address)及此数据块所属的段(segment)的类型(例如,表或索引)。
025

Table Directory

2.2.1.2 表目录区

026 This portion of the data block contains information about the table having rows in this block.
 
如果一个数据表在此数据块中储存了数据行,那么数据表的信息将被记录在数据块的表目录区(table directory)中。
027

Row Directory

2.2.1.3 行目录区

028 This portion of the data block contains information about the actual rows in the block (including addresses for each row piece in the row data area).
 
此区域包含数据块中存储的数据行的信息(每个数据行片断(row piece) 在行数据区(row data area)中的地址)。[一个数据块中可能保存一个完整的数据行,也可能只保存数据行的一部分 ,所以文中使用row piece]
 
029 After the space has been allocated in the row directory of a data block's overhead, this space is not reclaimed when the row is deleted. Therefore, a block that is currently empty but had up to 50 rows at one time continues to have 100 bytes allocated in the header for the row directory. Oracle reuses this space only when new rows are inserted in the block.
 
当一个数据块(data block)的行目录区(row directory)空间被使用后,即使数据行被删除(delete),行目录区空间也不会被回收。举例来说,当一个曾经包含50条记录的数据块被清空后,其块头(header)的行目录区仍然占用100字节(byte)的空间。只有在数据块中插入(insert)新数据时,行目录区空间才会被 重新利用。
030

Overhead

2.2.1.4 管理开销

031 The data block header, table directory, and row directory are referred to collectively as overhead. Some block overhead is fixed in size; the total block overhead size is variable. On average, the fixed and variable portions of data block overhead total 84 to 107 bytes.
 
数据块头(data block header),表目录区(table directory),行目录区(row directory)被统称为管理开销(overhead)。其中 有些开销的容量是固定的;而有些开销的总容量是可变的。数据块中固定及可变管理开销的容量平均在84到107字节(byte)之间。
032

Row Data

2.2.1.5 行数据

033 This portion of the data block contains table or index data. Rows can span blocks.
 
数据块(data block)中行数据区(row data)包含了表或索引的实际数据。一个数据行可以跨多个数据块。
 
034
See Also:

"Row Chaining and Migrating"
另见:

行链接(Row Chaining)及行迁移(Row Migrating)
035

Free Space

2.2.1.6 可用空间区

036 Free space is allocated for insertion of new rows and for updates to rows that require additional space (for example, when a trailing null is updated to a nonnull value).
 
在插入新数据行,或在更新数据行需要更多空间时(例如,原来某行最后一个字段为空(trailing null),现在要更新为非空值),将 使用可用空间区(free space)中的空间。
037 In data blocks allocated for the data segment of a table or cluster, or for the index segment of an index, free space can also hold transaction entries. A transaction entry is required in a block for each INSERT, UPDATE, DELETE, and SELECT...FOR UPDATE statement accessing one or more rows in the block. The space required for transaction entries is operating system dependent; however, transaction entries in most operating systems require approximately 23 bytes.
 
如果一个数据块(data block)属于表或簇表的数据段(data segment),或属于索引的索引段(index segment),那么在其可用空间区中还可能会存储事务条目(transaction entry)。如果一个数据块中的数据行(row)正在由 INSERTUPDATEDELETE,及 SELECT...FOR UPDATE 语句访问,此数据块中就需要保存事务条目。事务条目所需的存储空间依据操作系统而定。在常见的操作系统中事务条目大约需要占用23字节(byte)。
038

Free Space Management

2.2.2 可用空间管理

039 Free space can be managed automatically or manually.
 
可用空间可以被手动或自动管理。[注意本节中的“可用空间”与“可用空间区”的区别]
 
040 Free space can be managed automatically inside database segments. The in-segment free/used space is tracked using bitmaps, as opposed to free lists. Automatic segment-space management offers the following benefits:
  • Ease of use
  • Better space utilization, especially for the objects with highly varying row sizes
  • Better run-time adjustment to variations in concurrent access
  • Better multi-instance behavior in terms of performance/space utilization
数据库中,每个段(segment)的可用空间可以被自动管理。段内的可用/已用空间以位图(bitmap)形式记录,这与可用块以列表方式的管理不同。段空间自动管理(Automatic segment-space management)具备以下优势:
  • 易于使用
  • 空间利用效率更高,尤其针对每行数据容量差异大的表(或其他对象)
  • 能够更好地针对当前数据的情况实时调整
  • Better multi-instance behavior in terms of performance/space utilization
041 You specify automatic segment-space management when you create a locally managed tablespace. The specification then applies to all segments subsequently created in this tablespace.
 
用户可以在创建一个本地管理的表空间(locally managed tablespace)时选择自动段空间管理(automatic segment-space management)功能。这样在此表空间内创建的段都将默认地设置为自动段空间管理。
042

See Also:

Oracle Database Administrator's Guide

另见:

Oracle 数据库管理员指南

043

Availability and Optimization of Free Space in a Data Block

2.2.2.1 数据块可用空间的有效性及优化

044 Two types of statements can increase the free space of one or more data blocks: DELETE statements, and UPDATE statements that update existing values to smaller values. The released space from these types of statements is available for subsequent INSERT statements under the following conditions:
  • If the INSERT statement is in the same transaction and subsequent to the statement that frees space, then the INSERT statement can use the space made available.
  • If the INSERT statement is in a separate transaction from the statement that frees space (perhaps being run by another user), then the INSERT statement can use the space made available only after the other transaction commits and only if the space is needed.
有两种SQL语句可以增加数据块中的可用空间:分别是 DELETE 语句,和将现有数据值更新为占用容量更小值的 UPDATE 语句。在以下两种条件下,上述两中操作释放的空间可以被后续的 INSERT 语句使用:
  • 如果 INSERT 语句与上述两种操作在同一事务(transaction)中,且位于释放空间的语句之后,那么 INSERT 语句可以使用被释放的空间。
  • 如果 INSERT 语句与释放空间的语句在不同的事务中(比如两者是由不同的用户提交的),那么只有在释放空间的语句提交后,且插入数据必需使用此数据块时,INSERT 语句才会使用被释放的空间。
045 Released space may or may not be contiguous with the main area of free space in a data block. Oracle coalesces the free space of a data block only when (1) an INSERT or UPDATE statement attempts to use a block that contains enough free space to contain a new row piece, and (2) the free space is fragmented so the row piece cannot be inserted in a contiguous section of the block. Oracle does this compression only in such situations, because otherwise the performance of a database system decreases due to the continuous compression of the free space in data blocks.
 
数据块(data block)中被释放出的空间未必与可用空间区(free space)相连续。Oracle在满足以下条件时才会将释放的空间合并到可用空间区:(1)INSERTUPDATE 语句选中了一个有足够可用空间容纳新数据的数据块,(2)但是此块中的可用空间不连续,数据无法被写入到数据块中连续的空间里。Oracle只在 满足上述条件时才对数据块中的可用空间进行合并,这样做是为了避免过于频繁的空间合并工作影响数据库性能。
046

Row Chaining and Migrating

2.2.2.2 行链接(Row Chaining)及行迁移(Row Migrating)

047 In two circumstances, the data for a row in a table may be too large to fit into a single data block. In the first case, the row is too large to fit into one data block when it is first inserted. In this case, Oracle stores the data for the row in a chain of data blocks (one or more) reserved for that segment. Row chaining most often occurs with large rows, such as rows that contain a column of datatype LONG or LONG RAW. Row chaining in these cases is unavoidable.
 
有两种情况会导致表中某行数据过大,一个数据块(data block)无法容纳。第一种情况,当一行数据被插入时一个数据块就无法容纳。在这种情况下Oracle将这行数据存储在段内的一个数据块(chain)中。在插入数据量大的行时常会发生行链接(row chaining),例如一个包含数据类型为 LONGLONG RAW 列的数据行。此时行链接不可避免。
048 However, in the second case, a row that originally fit into one data block is updated so that the overall row length increases, and the block's free space is already completely filled. In this case, Oracle migrates the data for the entire row to a new data block, assuming the entire row can fit in a new block. Oracle preserves the original row piece of a migrated row to point to the new block containing the migrated row. The rowid of a migrated row does not change.
 
第二种情况,原本存储在一个数据块(data block)内的数据行,因为更新操作导致长度增长,而所在数据块的可用空间也不能容纳增长后的数据行。在这种情况下,Oracle将此行数据迁移(migrate)到新的数据块中。Oracle在被迁移数据行原来所在位置保存一个指向新数据块的指针。被迁移数据行的 rowid 保持不变。
049 When a row is chained or migrated, I/O performance associated with this row decreases because Oracle must scan more than one data block to retrieve the information for the row.
 
当数据行发生链接(chain)或迁移(migrate)时,对其访问将会造成 I/O 性能降低,因为Oracle为获取这些数据行的数据时,必须访问更多的数据块(data block)。
050

See Also:

另见:

051

PCTFREE, PCTUSED, and Row Chaining

2.2.3 PCTFREE,PCTUSED,及行链接(Row Chaining)

052 For manually managed tablespaces, two space management parameters, PCTFREE and PCTUSED, enable you to control the use of free space for inserts and updates to the rows in all the data blocks of a particular segment. Specify these parameters when you create or alter a table or cluster (which has its own data segment). You can also specify the storage parameter PCTFREE when creating or altering an index (which has its own index segment).
 
在手动管理的表空间(manually managed tablespaces)中,用户可以使用 PCTFREEPCTUSED 这两个存储管理参数来控制对某段(segment)进行插入和更新操作时,如何利用属于此段的数据块(data block)中的可用空间。用户也可以在创建或修改索引时为其设定 PCTFREE 参数(索引存储在索引段(index segment)中)。
053

Note:

This discussion does not apply to LOB datatypes (BLOB, CLOB, NCLOB, and BFILE). They do not use the PCTFREE storage parameter or free lists.

See "Overview of LOB Datatypes" for information.

提示:

本节的内容并不适用于 LOB 数据类型(BLOBCLOBNCLOB,及 BFILE)。 这些类型的数据存储时不使用 PCTFREE 参数及可用块列表(free list)。

详见“LOB 数据类型概述”。
054

The PCTFREE Parameter

2.2.3.1 PCTFREE 参数

055 The PCTFREE parameter sets the minimum percentage of a data block to be reserved as free space for possible updates to rows that already exist in that block. For example, assume that you specify the following parameter within a CREATE TABLE statement:
 
PCTFREE 参数用来设置一个数据块(data block)中至少需要保留(reserve)多少可用空间(百分比值),为数据块中已有数据更新时可能发生的数据量增长做准备。例如,当用户用 CREATE TABLE 语句创建表时指定了以下参数:
056

PCTFREE 20

PCTFREE 20
057 This states that 20% of each data block in this table's data segment be kept free and available for possible updates to the existing rows already within each block. New rows can be added to the row data area, and corresponding information can be added to the variable portions of the overhead area, until the row data and overhead total 80% of the total block size. Figure 2-3  illustrates PCTFREE.
 
这个参数设定了此表对应的数据段(data segment)中的每个数据块(data block)至少保留20%的可用空间,以备块中已有数据更新时使用。只要数据块中行数据区与数据块头的容量之和不超过数据块总容量的80%,用户就可以向其中插入新数据,数据行被放入行数据区(row data area),相关信息被写入数据块头(overhead area)。图 2-3 说明了 PCTFREE 的作用。
058 Figure 2-3 PCTFREE
 
图 2-3 PCTFREE
059

Description of Figure 2-3 follows
 

060

The PCTUSED Parameter

2.2.3.2 PCTUSED 参数

061 The PCTUSED parameter sets the minimum percentage of a block that can be used for row data plus overhead before new rows are added to the block. After a data block is filled to the limit determined by PCTFREE, Oracle considers the block unavailable for the insertion of new rows until the percentage of that block falls beneath the parameter PCTUSED. Until this value is achieved, Oracle uses the free space of the data block only for updates to rows already contained in the data block. For example, assume that you specify the following parameter in a CREATE TABLE statement:
 
PCTUSED 参数用于决定一个数据块(data block)是否可被用于插入新数据,她的依据是数据区(row data)与数据块头(overhead)的容量之和占数据块全部容量的最大百分比。当一个数据块中的可用空间比例小于 PCTFREE 参数的规定时,Oracle就认为此数据块无法被用于插入新数据,直到数据块中的占用容量比例小于 PCTUSED 参数的限定。在占用容量比例大于 PCTUSED 参数的限定之前,Oracle只在更新数据块内已有数据时才会使用此数据块的可用空间。例如,当用户用 CREATE TABLE 语句创建表时指定了以下参数:
062

PCTUSED 40

PCTUSED 40
063 In this case, a data block used for this table's data segment is considered unavailable for the insertion of any new rows until the amount of used space in the block falls to 39% or less (assuming that the block's used space has previously reached PCTFREE). Figure 2-4 illustrates this.
 
在例子中,当此表的某数据块占用容量比例高于40%时,Oracle不会将此数据块用于插入新数据行(假设此数据块的可用空间曾经低于 PCTFREE 的限定)。图 2-4 说明了 PCTUSED 的作用。
064 Figure 2-4 PCTUSED
 
图 2-4 PCTUSED
065

Description of Figure 2-4 follows
 

066

How PCTFREE and PCTUSED Work Together

2.2.3.3 PCTFREE 和 PCTUSED 如何协同发挥作用

067 PCTFREE and PCTUSED work together to optimize the use of space in the data blocks of the extents within a data segment. Figure 2-5 illustrates the interaction of these two parameters.
 
PCTFREEPCTUSED 共同作用可以优化数据块(data block)的空间使用。图 2-5 说明了这两种参数的交互作用。
068 Figure 2-5 Maintaining the Free Space of Data Blocks with PCTFREE and PCTUSED
 
2-5 使用 PCTFREE 和 PCTUSED 参数管理数据块的可用空间
069

Description of Figure 2-5 follows
 

070

This image shows how PCTFREE and PCTUSED work together to maintain free space in a data block.

In step 1, rows are inserted up to 80% only, because PCTFREE specifies that 20% of the block must remain open for updates of existing rows.

In step 2, updates to existing rows use the free space reserved in the block. No new rows can be inserted into the block until the amount of used space is 39% or less.

In step 3, after the amount of used space falls below 40%, new rows can again be inserted into this block.

In step 4, rows are inserted up to 80% only, because PCTFREE specifies that 20% of the block must remain open for updates of existing rows. This cycle continues.

上图说明了 PCTFREEPCTUSED 如何共同作用以管理数据块(data block)可用空间的使用。

在第一步中,数据块占用空间比例小于80%时才能插入新数据,因为 PCTFREE 参数限定必须保留20%的可用空间用于块内已有数据的更新。

在第二步中,对数据块中已有数据的更新操作可以使用数据块中的保留空间。只有当数据块内的占用空间比例低于40%时才能向其中插入新数据。

在第三步中,当数据块内的占用空间比例低于40%时,此数据块再次可以被用于插入新数据。

在第四步中,数据块占用空间比例小于80%时才能插入新数据,因为 PCTFREE 参数限定必须保留20%的可用空间用于块内已有数据的更新。此过程如此往复循环。
071 In a newly allocated data block, the space available for inserts is the block size minus the sum of the block overhead and free space (PCTFREE). Updates to existing data can use any available space in the block. Therefore, updates can reduce the available space of a block to less than PCTFREE, the space reserved for updates but not accessible to inserts.
 
在新分配的数据块中(data block),可用于插入(insert)数据的空间等于数据块总容量减去数据块头(block overhead)再减去预留可用空间(PCTFREE)。而更新(update)数据块内已有数据可使用数据块中的所有可用空间。因此,更新操作能够使数据块内的可用空间低于的 PCTFREE 限制,因为这些空间是专为更新操作而预留的。
072 For each data and index segment, Oracle maintains one or more free lists—lists of data blocks that have been allocated for that segment's extents and have free space greater than PCTFREE. These blocks are available for inserts. When you issue an INSERT statement, Oracle checks a free list of the table for the first available data block and uses it if possible. If the free space in that block is not large enough to accommodate the INSERT statement, and the block is at least PCTUSED, then Oracle takes the block off the free list. Multiple free lists for each segment can reduce contention for free lists when concurrent inserts take place.
 
在每个数据段(data segment)与索引段(index segment)中,Oracle管理着一个或多个可用块列表(free list)--其中列出了所有属于此段的数据扩展(extent),且可用空间比例大于 PCTFREE 限定的数据块。这些块可以被插入(insert)操作使用。当用户提交了 INSERT 语句后,Oracle从可用块列表中选择第一个有效的数据块使用。如果此数据块的可用空间不够容纳 INSERT 语句提交的数据,且此块的占用容量已经超过PCTUSED 的限定,Oracle就将其从可用块列表中移出。一个段可以同时使用多个可用块列表,以减少对一个表进行并发插入(concurrent insert)时产生的竞争。
073 After you issue a DELETE or UPDATE statement, Oracle processes the statement and checks to see if the space being used in the block is now less than PCTUSED. If it is, then the block goes to the beginning of the transaction free list, and it is the first of the available blocks to be used in that transaction. When the transaction commits, free space in the block becomes available for other transactions.
 
当用户提交了 DELETEUPDATE 语句后,Oracle处理语句并检查相关数据块中的占用空间比例是否小于 PCTUSED 的规定。如果满足,那么这个数据块就被放入当前事务(transaction)正在使用的可用块列表(free list)的头部,如果当前事务还需要写入数据,此块将被首先使用。当事务提交后,此数据块中的可用空间还可被其他事务使用。
074

Overview of Extents

2.3 数据扩展概述

075 An extent is a logical unit of database storage space allocation made up of a number of contiguous data blocks. One or more extents in turn make up a segment. When the existing space in a segment is completely used, Oracle allocates a new extent for the segment.
 
数据扩展(extent)是由一组连续的数据块(data block)构成的数据库逻辑存储分配单位。而段(segment)则是由一个或多个数据扩展构成。当一个段中已有空间已经用完,Oracle为这个段分配新的数据扩展。
076

When Extents Are Allocated

2.3.1 数据扩展何时被分配

077 When you create a table, Oracle allocates to the table's data segment an initial extent of a specified number of data blocks. Although no rows have been inserted yet, the Oracle data blocks that correspond to the initial extent are reserved for that table's rows.
 
当用户创建数据表时,Oracle为此表的数据段(data segment)分配一个包含若干数据块(data block)的初始数据扩展(initial extent)。虽然此时数据表中还没有数据,但是在此初始数据扩展中的数据块已经为插入新数据做好了准备。
078 If the data blocks of a segment's initial extent become full and more space is required to hold new data, Oracle automatically allocates an incremental extent for that segment. An incremental extent is a subsequent extent of the same or greater size than the previously allocated extent in that segment.
 
如果一个段(segment)的初始数据扩展(initial extent)中的数据块(data block)都已装满,且有新数据插入需要空间时,Oracle自动为这个段分配一个增量数据扩展(incremental extent)。增量数据扩展是一个段中继已有数据扩展之后分配的后续数据扩展,她的容量大于或等于之前的数据扩展。
079 For maintenance purposes, the header block of each segment contains a directory of the extents in that segment.
 
为了管理的需要,每个段(segment)的段头(header block)中包含一个记录此段所有数据扩展(extent)的目录。
080
Note:

This chapter applies to serial operations, in which one server process parses and runs a SQL statement. Extents are allocated somewhat differently in parallel SQL statements, which entail multiple server processes.
提示:

本章的内容适用于串行执行(serial operation)的情况,即只有一个服务进程解析、执行 SQL 语句。对于并行执行(parallel)的 SQL 语句(即一个语句由多个服务进程执行),数据扩展(extent)的分配情况有所不同。
081

Determine the Number and Size of Extents

2.3.2 如何决定数据扩展分配时的数量与容量

082 Storage parameters expressed in terms of extents define every segment. Storage parameters apply to all types of segments. They control how Oracle allocates free database space for a given segment. For example, you can determine how much space is initially reserved for a table's data segment or you can limit the number of extents the table can allocate by specifying the storage parameters of a table in the STORAGE clause of the CREATE TABLE statement. If you do not specify a table's storage parameters, then it uses the default storage parameters of the tablespace.
 
每个段(segment)的定义中都包含了数据扩展(extent)的存储参数(storage parameter)。存储参数适用于各种类型的段。这个参数控制着Oracle如何为段分配可用空间。例如,用户可以在 CREATE TABLE 语句中使用 STORAGE 子句设定存储参数,决定创建表时为其数据段(data segment)分配多少初始空间,或限定一个表最多可以包含多少数据扩展。如果用户没有为表设定存储参数,那么表在创建时使用所在表空间(tablespace)的默认存储参数。
083 You can have dictionary managed tablespaces, which rely on data dictionary tables to track space utilization, or locally managed tablespaces, which use bitmaps (instead of data dictionary tables) to track used and free space. Because of the better performance and easier manageability of locally managed tablespaces, the default for non-SYSTEM permanent tablespaces is locally managed whenever the type of extent management is not explicitly specified.
 
用户既可以使用数据字典管理的表空间(dictionary managed tablespace)(依赖数据字典表监控空间的利用情况),也可以使用本地管理的表空间(locally managed tablespace)(使用位图(bitmap)来标记可用与已用空间)。由于本地管理的表空间性能较好且易于管理,当用户没有显式地设定数据扩展(extent)管理参数时,除了 SYSTEM 之外的所有永久表空间(permanent tablespace)默认使用本地管理方式。
084 A tablespace that manages its extents locally can have either uniform extent sizes or variable extent sizes that are determined automatically by the system. When you create the tablespace, the UNIFORM or AUTOALLOCATE (system-managed) clause specifies the type of allocation.
  • For uniform extents, you can specify an extent size or use the default size, which is 1 MB. Ensure that each extent contains at least five database blocks, given the database block size. Temporary tablespaces that manage their extents locally can only use this type of allocation.
  • For system-managed extents, Oracle determines the optimal size of additional extents, with a minimum extent size of 64 KB. If the tablespaces are created with 'segment space management auto', and if the database block size is 16K or higher, then Oracle manages segment size by creating extents with a minimum size of 1M. This is the default for permanent tablespaces.
在一个本地管理的表空间中,其中所分配的数据扩展(extent)的容量既可以是用户设定的固定值,也可以是由系统自动决定的可变值。当用户创建表空间(tablespace)时可以使用 UNIFORM (用户指定)或 AUTOALLOCATE (由系统管理)子句设定数据扩展的分配方式。
  • 对于固定容量(UNIFORM)的数据扩展,用户可以为数据扩展设定容量或使用默认大小(1 MB)。用户须确保每个数据扩展的容量至少能包含5个数据块(database block)。本地管理(locally managed)的临时表空间(temporary tablespace)在分配数据扩展时只能使用此种方式。
  • 对于由系统管理(AUTOALLOCATE)的数据扩展,由Oracle决定新增数据扩展的最佳容量,其最小容量为 64 KB。如果创建表空间时使用了“segment space management auto”子句,且数据块容量大于等于 16 KB,Oracle扩展一个段时(segment)所创建的数据扩展的最小容量为 1 MB。对于永久表空间(permanent tablespace)上述参数均为默认值。
085 The storage parameters INITIAL, NEXT, PCTINCREASE, and MINEXTENTS cannot be specified at the tablespace level for locally managed tablespaces. They can, however, be specified at the segment level. In this case, INITIAL, NEXT, PCTINCREASE, and MINEXTENTS are used together to compute the initial size of the segment. After the segment size is computed, internal algorithms determine the size of each extent.
 
在本地管理的表空间(locally managed tablespace)中,INITIALNEXTPCTINCREASE,和 MINEXTENTS 这四个存储参数可以作用于段(segment),但不能作用于表空间。INITIALNEXTPCTINCREASE,和 MINEXTENTS 相结合可以用于计算段的初始容量。当段容量确定后,Oracle使用内部算法确定其中每个初始数据扩展(extent)的容量。
086

See Also:

另见:

087

How Extents Are Allocated

2.3.3 数据扩展如何被分配

088 Oracle uses different algorithms to allocate extents, depending on whether they are locally managed or dictionary managed.
 
Oracle依据表空间管理方式的不同(本地管理(locally managed)或数据字典管理(dictionary managed)),选择不同的算法分配数据扩展(extent)。
089 With locally managed tablespaces, Oracle looks for free space to allocate to a new extent by first determining a candidate datafile in the tablespace and then searching the datafile's bitmap for the required number of adjacent free blocks. If that datafile does not have enough adjacent free space, then Oracle looks in another datafile.
 
对于本地管理的表空间(locally managed tablespace),Oracle在为新的数据扩展(extent)寻找可用空间时,首先选择一个属于此表空间的数据文件(datafile),再搜索此数据文件的位图(bitmap)查找连续的数据块(free block)。如果此数据块中没有足够的连续可用空间,Oracle将查询其他数据文件。
090

Note:

Oracle strongly recommends that you use locally managed tablespaces.

提示:

Oracle 强烈建议用户使用本地管理的表空间(locally managed tablespace)。
091

When Extents Are Deallocated

2.3.4 数据扩展如何被回收

092 The Oracle Database provides a Segment Advisor that helps you determine whether an object has space available for reclamation based on the level of space fragmentation within the object.
 
Oracle 数据库提供了 Segment Advisor 工具,她依据方案对象(schema object)存储空间中的碎片程度来判断此对象中是否包含可回收的空间。
093

See Also:

另见:

094 In general, the extents of a segment do not return to the tablespace until you drop the schema object whose data is stored in the segment (using a DROP TABLE or DROP CLUSTER statement). Exceptions to this include the following:
  • The owner of a table or cluster, or a user with the DELETE ANY privilege, can truncate the table or cluster with a TRUNCATE...DROP STORAGE statement.
  • A database administrator (DBA) can deallocate unused extents using the following SQL syntax:

    ALTER TABLE table_name DEALLOCATE UNUSED;
  • Periodically, Oracle deallocates one or more extents of a rollback segment if it has the OPTIMAL size specified.
一般来说,在用户将一个段(segment)对应的方案对象(schema object)移除(使用 DROP TABLEDROP CLUSTER 语句)之前,此段的数据扩展(extent)不会被回收到表空间(tablespace)中,但是以下情况例外:
  • 表,簇表的所有者(owner)或拥有 DELETE ANY 权限的用户, 可以使用 TRUNCATE...DROP STORAGE 语句将表,簇表的数据清除
  • DBA 可以使用以下语法收回一个段中未使用的数据扩展:

    ALTER TABLE table_name DEALLOCATE UNUSED;
  • 如果用户为回滚段(rollback segment)设定了 OPTIMAL 参数,Oracle将周期性地从其中回收数据扩展。
095 When extents are freed, Oracle modifies the bitmap in the datafile (for locally managed tablespaces) or updates the data dictionary (for dictionary managed tablespaces) to reflect the regained extents as available space. Any data in the blocks of freed extents becomes inaccessible.
 
当数据扩展(extent)被释放后,Oracle修改数据文件(datafile)中的位图(bitmap)(对于本地管理的表空间)或更新数据字典(对于数据字典管理的表空间),将回收的数据扩展视为可用空间。被释放的数据扩展中的数据无法继续访问。
096

See Also:

另见:

097

Extents in Nonclustered Tables

2.3.4.1 非簇表中的数据扩展

098 As long as a nonclustered table exists or until you truncate the table, any data block allocated to its data segment remains allocated for the table. Oracle inserts new rows into a block if there is enough room. Even if you delete all rows of a table, Oracle does not reclaim the data blocks for use by other objects in the tablespace.
 
只要用户没有移除(drop)或清空(truncate)非簇表(nonclustered table),那么对应数据段(data segment)内的任何数据块(data block)都不会被回收。Oracle会在插入数据时使用这些数据块(只要数据块有足够的空间)。即使用户删除(delete)了表的所有行,Oracle也不会回收此表的数据块供表空间内的其他方案对象(schema object)使用。
099 After you drop a nonclustered table, this space can be reclaimed when other extents require free space. Oracle reclaims all the extents of the table's data and index segments for the tablespaces that they were in and makes the extents available for other schema objects in the same tablespace.
 
当用户移除(drop)了一个非簇表(nonclustered table),Oracle将在需要空间时回收此表对应的数据扩展(extent)。Oracle将此表对应的数据段(data segment)、索引段(index segment)中包含的数据扩展(extent)全部收回,并供存储于此表空间(tablespace)的其他方案对象(chema object)使用。
100 In dictionary managed tablespaces, when a segment requires an extent larger than the available extents, Oracle identifies and combines contiguous reclaimed extents to form a larger one. This is called coalescing extents. Coalescing extents is not necessary in locally managed tablespaces, because all contiguous free space is available for allocation to a new extent regardless of whether it was reclaimed from one or more extents.
 
对于数据字典管理的表空间(dictionary managed tablespace),当一个段(segment)需要的数据扩展(extent)比现有可用扩展的都大时,Oracle通过寻找、合并连续的被回收的数据扩展来形成更大的数据扩展。这个过程被称为数据扩展合并(coalescing)。本地管理的表空间(locally managed tablespace)没有必要进行数据扩展合并,因为所有连续空间都可以被用于分配新数据扩展,而无需关心所用空间是如何被回收的。
101

Extents in Clustered Tables

2.3.4.2 簇表中的数据扩展

102 Clustered tables store information in the data segment created for the cluster. Therefore, if you drop one table in a cluster, the data segment remains for the other tables in the cluster, and no extents are deallocated. You can also truncate clusters (except for hash clusters) to free extents.
 
簇表(clustered table)的数据存储在为整个簇(cluster)分配的数据段(data segment)中。因此当用户移除(drop)簇中的一个表时,数据段还要为簇中的其他表保留,因此没有数据扩展(extent)被回收。用户可以通过清空(truncate)整个簇来回收属于簇的数据扩展(哈希簇(hash cluster)除外)。
103

Extents in Materialized Views and Their Logs

2.3.4.3 物化视图及其日志的数据扩展

104 Oracle deallocates the extents of materialized views and materialized view logs in the same manner as for tables and clusters.
 
Oracle回收属于物化视图(materialized view)及其日志(materialized view log)的数据扩展的方式与表或簇相同。
105

See Also:

"Overview of Materialized Views"

另见:

物化视图概述
106

Extents in Indexes

2.3.4.4 索引的数据扩展

107 All extents allocated to an index segment remain allocated as long as the index exists. When you drop the index or associated table or cluster, Oracle reclaims the extents for other uses within the tablespace.
 
只要索引存在,那么对应索引段(index segment)的所有数据扩展(extent)都不会被回收。当用户移除(drop)索引或相关的表、簇表时,Oracle才将索引使用的数据扩展回收。
108

Extents in Temporary Segments

2.3.4.5 临时段(temporary segment)的数据扩展

109 When Oracle completes the execution of a statement requiring a temporary segment, Oracle automatically drops the temporary segment and returns the extents allocated for that segment to the associated tablespace. A single sort allocates its own temporary segment in a temporary tablespace of the user issuing the statement and then returns the extents to the tablespaces.
 
当Oracle执行完一个 SQL 语句,且在执行过程中使用的临时段(temporary segment)时,Oracle自动地移除(drop)临时段,并将属于此临时段的数据扩展(extent)空间还给临时段所在的表空间(tablespace)。当用户执行单一排序(single sort)语句时,Oracle将在此用户的临时表空间(temporary tablespace)中创建为这个排序服务的临时段(temporary segment),并在执行后将此临时段使用的数据扩展回收。
110 Multiple sorts, however, can use sort segments in temporary tablespaces designated exclusively for sorts. These sort segments are allocated only once for the instance, and they are not returned after the sort, but remain available for other multiple sorts.
 
但是对于多重排序(multiple sort),可能会使用临时表空间(temporary tablespace)中专为排序而创建的排序段(sort segment)。在每个实例中只会分配一个排序段,在排序操作使用后也不会被回收,而是继续供以后的多重排序使用。
111 A temporary segment in a temporary table contains data for multiple statements of a single transaction or session. Oracle drops the temporary segment at the end of the transaction or session, returning the extents allocated for that segment to the associated tablespace.
 
同一会话或同一事务的多个 SQL 语句,有可能共同使用一个临时表(temporary table)的临时段(temporary segment)存储临时数据。在这种情况下,Oracle在会话或事务结束后 才移除(drop)临时段,并为临时段所在表空间回收使用的数据扩展(extent)。
112

See Also:

另见:

113

Extents in Rollback Segments

2.3.4.6 回滚段(rollback segment)的数据扩展

114 Oracle periodically checks the rollback segments of the database to see if they have grown larger than their optimal size. If a rollback segment is larger than is optimal (that is, it has too many extents), then Oracle automatically deallocates one or more extents from the rollback segment.
 
Oracle周期性地检查数据库回滚段(rollback segment)的容量是否超过了最优值(optimal size)。如果回滚段超过了这个最优值(即回滚段中的数据扩展(extent)过多),Oracle将自动地从回滚段回收一些数据扩展。
115

Overview of Segments

2.4 段概述

116 A segment is a set of extents that contains all the data for a specific logical storage structure within a tablespace. For example, for each table, Oracle allocates one or more extents to form that table's data segment, and for each index, Oracle allocates one or more extents to form its index segment.
 
段(segment)由一组数据扩展(extent)构成,其中存储了表空间(tablespace)内各种逻辑存储结构的数据。例如,Oracle能为每个表的数据段(data segment)分配数据扩展,还能为每个索引的索引段(index segment)分配数据扩展。
117 This section contains the following topics: 本节包含以下主题:
118

Introduction to Data Segments

2.4.1 数据段简介

119 A single data segment in an Oracle database holds all of the data for one of the following:
  • A table that is not partitioned or clustered
  • A partition of a partitioned table
  • A cluster of tables
在Oracle数据库中,一个数据段(segment)可以供以下方案对象(或方案对象的一部分)容纳数据:
  • 非分区表或非簇表
  • 分区表的一个分区
  • 一个簇表
120 Oracle creates this data segment when you create the table or cluster with the CREATE statement.
 
当用户使用 CREATE 语句创建表或簇表时,Oracle创建相应的数据段(data segment)。
121 The storage parameters for a table or cluster determine how its data segment's extents are allocated. You can set these storage parameters directly with the appropriate CREATE or ALTER statement. These storage parameters affect the efficiency of data retrieval and storage for the data segment associated with the object.
 
表或簇表的存储参数(storage parameter)用来决定对应数据段(data segment)的数据扩展(extent)如何被分配。用户可以使用 CREATEALTER 语句直接设定这些存储参数。这些参数将会影响与方案对象(object)相关的数据段的存储与访问效率。
122
Note:

Oracle creates segments for materialized views and materialized view logs in the same manner as for tables and clusters.
提示:

Oracle 为物化视图(materialized view)及物化视图日志(materialized view log)创建数据段(data segment)的方式与表或簇表相似。
123

See Also:

另见:

124

Introduction to Index Segments

2.4.2 索引段简介

125 Every nonpartitioned index in an Oracle database has a single index segment to hold all of its data. For a partitioned index, every partition has a single index segment to hold its data.
 
Oracle 数据库中每个非分区索引(nonpartitioned index)使用一个索引段(index segment)来容纳其数据。而对于分区索引(partitioned index),每个分区使用一个索引段来容纳其数据。
126 Oracle creates the index segment for an index or an index partition when you issue the CREATE INDEX statement. In this statement, you can specify storage parameters for the extents of the index segment and a tablespace in which to create the index segment. (The segments of a table and an index associated with it do not have to occupy the same tablespace.) Setting the storage parameters directly affects the efficiency of data retrieval and storage.
 
用户可以使用 CREATE INDEX 语句为索引或索引的分区创建索引段(index segment)。在创建语句中,用户可以设定索引段(index segment)的数据扩展(extent)的存储参数(storage parameter)以及此索引段应存储在哪个表空间(tablespace)中。(表的数据段和与其相关的索引段不一定要存储在同一表空间中。)索引段的存储参数将会影响数据的存储与访问效率。
127

Introduction to Temporary Segments

2.4.3 临时段简介

128 When processing queries, Oracle often requires temporary workspace for intermediate stages of SQL statement parsing and execution. Oracle automatically allocates this disk space called a temporary segment. Typically, Oracle requires a temporary segment as a database area for sorting. Oracle does not create a segment if the sorting operation can be done in memory or if Oracle finds some other way to perform the operation using indexes.
 
当Oracle处理一个查询时,经常需要为SQL语句的解析与执行的中间结果(intermediate stage)准备临时空间。Oracle会自动地分配被称为临时段(temporary segment)的磁盘空间。例如,Oracle在进行排序操作时就需要使用临时段。当排序操作可以在内存中执行,或Oracle设法利用索引就执行时,就不必创建临时段。
129

Operations that Require Temporary Segments

2.4.3.1 需要使用临时段的操作

130 The following statements sometimes require the use of a temporary segment:
  • CREATE INDEX
  • SELECT ... ORDER BY
  • SELECT DISTINCT ...
  • SELECT ... GROUP BY
  • SELECT . . . UNION
  • SELECT ... INTERSECT
  • SELECT ... MINUS
以下语句的执行过程中可能会使用临时段(temporary segment):
  • CREATE INDEX
  • SELECT ... ORDER BY
  • SELECT DISTINCT ...
  • SELECT ... GROUP BY
  • SELECT . . . UNION
  • SELECT ... INTERSECT
  • SELECT ... MINUS
131 Some unindexed joins and correlated subqueries can require use of a temporary segment. For example, if a query contains a DISTINCT clause, a GROUP BY, and an ORDER BY, Oracle can require as many as two temporary segments.
 
有些不能使用索引的关联操作(unindexed join),或者需要在子查询间建立相互关系(correlated subqueries),也可能需要使用临时段(temporary segment)。所以当查询包含DISTINCTGROUP BY,或 ORDER BY 子句时,Oracle有可能使用两个临时段。
132

Segments in Temporary Tables and Their Indexes

2.4.3.2 临时表及其索引使用的段